My Loop is not working - it exits before it should - c#

I'm new to programming. I tried this code and the loop is not working. The console application just ends after one operation, and I would like to continue doing more until I choose to say "no." Where am I making a mistake here? I want the program to keep going until I say so. Thanks in advance.
static void Main(string[] args)
{
Console.WriteLine("Program to convert USA Dollars to five different currencies,");
Console.WriteLine();
double dollars, euros, turkisLira, yen, britishPounds, mexicanPeso;
char option;
euros = 0.72;
turkisLira = 2.09;
yen = 101.73;
britishPounds = 0.59;
mexicanPeso = 13.03;
string answer = "Y";
do
{
Console.Write("Enter the Amount of USA Dollars to Convert:");
dollars = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("1.Convert to Euros");
Console.WriteLine("2.Convert to Turkis Lira");
Console.WriteLine("3.Convert to Japanes Yen");
Console.WriteLine("4.Convert to British Pounds");
Console.WriteLine("5.Convert to Mexican Pesos");
Console.Write("Enter the option of Currency you wish to convert : ");
option = Convert.ToChar(Console.ReadLine());
switch (option)
{
case '1':
euros = dollars * euros;
Console.WriteLine("The amount of Euros given is : {0}", euros);
break;
case '2':
turkisLira = dollars * turkisLira;
Console.WriteLine("The amount of Turkis Liras given is : {0}", turkisLira);
break;
case '3':
yen = dollars * yen;
Console.WriteLine("The amount of Japanes Yen given is : {0}", yen);
break;
case '4':
britishPounds = dollars * britishPounds;
Console.WriteLine("The amount of British Pounds given is : {0}", britishPounds);
break;
case '5':
mexicanPeso = dollars * mexicanPeso;
Console.WriteLine("The amount of Mexican Pesos given is : {0}", mexicanPeso);
break;
}
Console.WriteLine(" Do you wish to do more conversions? (yes/no)");
answer = Console.ReadLine();
if (answer.Equals("Y"))
{
Console.WriteLine("Yes");
}
else if (answer.Equals("N"))
{
Console.WriteLine("No");
}
} while (answer.ToLower() == "y'");

Your requirement is
I will like to continue doing more until I choose to say No
while your exist condition is
(answer.ToLower() == "y'");
So, if somebody clicks anything other than y, the loop would exit. You would want the exist condition to be
(answer.ToLower() != "n'");-

I think you have miss type ' in "y'" where you just want receive "y" to continue.

Related

How to accept the user response either as an int, or a char or a string. C#

int number;
do
{
DisplayMenu();
number = Convert.ToInt32(Console.ReadLine()); //here need int, string, and char
if (number < 0 || number > 6)
{
Console.WriteLine("An error occured!");
break;
}
} while (number != 0);
static void DisplayMenu()
{
Console.WriteLine("Main Menu");
Console.WriteLine("1) Calculate Sum ");
Console.WriteLine("2) Calculate Average");
Console.WriteLine("3) Display Numbers");
Console.WriteLine("4) Display Poem");
Console.WriteLine("5) Create Numbers Array");
Console.WriteLine("0) To Exit");
Console.WriteLine();
Console.WriteLine("Enter the number that corresponds to your choice: ");
}
I need to take the user input in integer, string, and character. Moreover, this code should then still work properly.
I was searching on other boards but there is no such thing I could find.
Reading your code, it appears that you are asking the user to enter a single digit from 0-5 to select the next function. One straightforward way of doing this is to substitute the ReadKey() method and switch on the result. As a bonus, the menu will execute immediately without having to wait for the [Enter] key.
// Loop until a valid int is received.
bool exit = false;
while (!exit)
{
DisplayMenu();
switch (Console.ReadKey().Key)
{
case ConsoleKey.D0: exit = true; break;
case ConsoleKey.D1: calculateSum(); break;
case ConsoleKey.D2: calculateAverage(); break;
case ConsoleKey.D3: displayNumbers(); break;
case ConsoleKey.D4: displayPoem(); break;
case ConsoleKey.D5: createNumbersArray(); break;
default:
Console.Clear();
Console.WriteLine("Please enter a number 0-5");
Thread.Sleep(1000);
break;
}
}

Pseudocode for switch statement in c#

How do I write the pseudocode for a switch (case) statement in C#?
switch (option)
{
case 1:
Console.Write("Enter First Number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Second Number: ");
num2 = Convert.ToDouble(Console.ReadLine());
result = num1 + num2;
Console.WriteLine(result);
break;
case 2:
Console.Write("Enter First Number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Second Number: ");
num2 = Convert.ToDouble(Console.ReadLine());
result = num1 - num2;
Console.WriteLine(result);
break;
case 3:
Console.Write("Enter First Number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Second Number: ");
num2 = Convert.ToDouble(Console.ReadLine());
result = num1 * num2;
Console.WriteLine(result);
break;
default:
Console.WriteLine("\n Next time follow instructions. You can only choose numbers 1 - 4");
break;
}
So, if I was going to write this, I'd start with an enumerated type for the operations:
public enum ArithmeticOperation
{
Add,
Subtract,
Multiply,
Divide,
}
I'd write a little helper function:
private static string ShowEnumOptions<T>() where T : struct
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException($"Type: {typeof(T).ToString()} must be an enumerated type");
}
var options = Enum.GetNames(typeof(T));
return string.Join("/", options);
}
(the newest version of C# (which I don't use yet) allows a System.Enum constraint on a generic type parameter which would simplify this)
Then I'd write my main program to look like this:
static void Main(string[] args)
{
while (true)
{
ArithmeticOperation operation = default(ArithmeticOperation);
var goodOperation = false;
while (!goodOperation)
{
Console.Write(
$"Enter operation (one of [{ShowEnumOptions<ArithmeticOperation>()}] or \"Quit\"): ");
var response = Console.ReadLine();
if (string.Equals(response, "Quit", StringComparison.InvariantCultureIgnoreCase))
{
return; //quit the app
}
if (Enum.TryParse<ArithmeticOperation>(response, true, out operation))
{
goodOperation = true;
}
}
double value1 = 0.0;
double value2 = 0.0; //initialize them to keep the compiler happy
var goodDouble = false;
while (!goodDouble)
{
Console.Write("Enter the first number: ");
var response = Console.ReadLine();
if (double.TryParse(response, out value1))
{
goodDouble = true;
}
}
goodDouble = false;
while (!goodDouble)
{
Console.Write("Enter the second number: ");
var response = Console.ReadLine();
if (double.TryParse(response, out value2))
{
goodDouble = true;
}
}
//ok, got an operation and two numbers
double result = 0.0;
switch (operation)
{
case ArithmeticOperation.Add:
result = value1 + value2;
break;
case ArithmeticOperation.Subtract:
result = value1 - value2;
break;
case ArithmeticOperation.Multiply:
result = value1 * value2;
break;
case ArithmeticOperation.Divide:
if (value2 == 0.0)
{
Console.WriteLine("Division by zero is invalid");
result = double.NaN; //NaN means "not a number"
break;
}
result = value1 / value2;
break;
}
Console.WriteLine($"Result is {result}");
}
}
Note that I check all input for validity. Always assume your users will enter bad data. Also note that I check my double for equality with zero. Checking for floating point equality is usually a bad idea, but it's the right thing to do here.
Then, as pseudo code, all I'd write would be:
// Get the operation (one of add/subtract/multiply or divide) - or allow the user to quit
// Get each of value1 and value2 as doubles
// Based on the operation, calculate the result (pay attention to division by zero)
// Show the result
// Loop back and let the user try again (or quit)
Pseudocode is basically writing what you're trying to do in comments. What your professor was probably trying to teach you is to make a bunch of comments to plan out the structure of your code, and then write your code. What you have above is already functional code. At the risk of answering your homework question, I'd say it goes something like this:
switch(option)
{
case 1:
//do something
break;
case 2:
//do something else
break;
default:
//what to do if none of the cases are met
break;
}
I don't know what you mean with pseudocode but this code is less repetitive:
double result = 0;
Console.Write("Enter First Number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Second Number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter a number from 1 to 3");
string input = Console.ReadLine();
switch (input) {
case "1" :
result = num1 + num2;
break;
case "2":
result = num1 - num2;
break;
case "3":
result = num1 * num2;
break;
default:
Console.WriteLine("\n Next time follow instructions. You can only choose numbers 1 - 4");
break;
}
Console.WriteLine("Result = " + result);

Display Console.ReadLine() in switch statement

I'm building console applications to sharpen my c# skills. I'm having two issues that I can't seem to find online.
Issue 1: I want to display whatever the user inputs in the output (user enters a numeric value.. when the program runs and displays an answer, I want the user input to be displayed as well)..
Issue 2: Currently you have to press the enter key twice to continue after the first input. Is there a way to only have to press the enter key once?
Thanks in advance!
Here is the existing code (I'll include both classes just incase you need the second for some reason):
namespace StaticDemo {
class Program
{
static void Main(string[] args)
{
string selection = string.Empty;
while (selection != "q" && selection != "Q")
{
Console.Write("Enter C)elsius to Fahrentheit or F(ahrenheit to Celsius or Q(uit. \nSelect C, F, or Q then press the enter key twice to continue: ");
selection = Console.ReadLine();
double fahrenheit = 0, celsius = 0;
string line = Console.ReadLine();
switch (selection)
{
case "C":
case "c":
Console.Write("Please enter the Celsius temperature that you want converted to a Fahrenheit temperature: ");
fahrenheit = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine($"The temperature you provided in celsius: {line}, would be: {fahrenheit} in fahrentheit");
break;
case "F":
case "f":
Console.Write("Please enter the Farentheit temperature that you want converted to a Celsius temperature: ");
celsius = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine($"The temperature you provided in Fahrentheit: {line}, would be: {celsius} in celsius");
break;
case "Q":
case "q":
break;
default:
Console.WriteLine("Enter C F or a Q, Moron!");
break;
}
Console.WriteLine();
Console.WriteLine();
}
}
} }
The second class code is below if you need it for whatever reason:
namespace StaticDemo
{
class TemperatureConverter
{
public static double CelsiusToFahrenheit(string tempCelsius)
{
double celcius = double.Parse(tempCelsius);
double fahrenheit = (celcius * 9 / 5) + 32;
return fahrenheit;
}
public static double FahrenheitToCelsius(string tempFahrenheit)
{
double fahrenheit = double.Parse(tempFahrenheit);
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
}
This line is useless and it is the reason for the two input required to process the choice
string line = Console.ReadLine();
You have already read the selection variable two lines up.
To get the value for the temperature provided by the user, you need to get the input without passing it directly to the conversion class
Console.Write("Please enter the Celsius temperature that you want converted to a Fahrenheit temperature: ");
string input = Console.ReadLine();
fahrenheit = TemperatureConverter.CelsiusToFahrenheit(input);
Console.WriteLine($"The temperature you provided in Celsius: {input}, would be: {celsius} in Fahrenheit");
Of course the same applies to the other block of code that converts a Fahrenheit to Celsius. Notice also that I think you have inverted the message.
You are asking for the user input twice basically.
Console.Write("Enter C)elsius to Fahrentheit or F(ahrenheit to Celsius or Q(uit. \nSelect C, F, or Q then press the enter key twice to continue: ");
selection = Console.ReadLine().ToUpper();
Console.WriteLine("Please enter the temperature that you want converted: ");
string tempToConvert = Console.ReadLine();
switch (selection)
{
case "C":
fahrenheit = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine($"The temperature you provided in celsius: {tempToConvert }, would be: {fahrenheit} in fahrentheit");
break;
Consider using ToUpper or ToLower on user inputs so you don't have to worry about the formatting of the input when you make your cases.
It is because of line
string line = Console.ReadLine();
Change it to
string line;
I think you previously wanted that line to read value to convert.
Change the line
selection = Console.ReadLine();
to
selection = Convert.ToString(Console.ReadKey());
also i think after making the above change you do not need this line.
string line = Console.ReadLine();

Float/Double type input validation in C#

This is literally my first program I've ever written (started learning this past Monday); I am a total newbie.
My question is, how can I prevent exceptions from being thrown when a user enters an invalid character when the program prompts the user for fahreinheit or celsius entry (expecting a number)??? So for example, when a user enters "asfasd", the program throws an exception.
I did a lot of searching on the site before posting this, and I was successfully able to find other input validation questions, however, they were all concerning C and C++ and since I am such a newbie, I have a hard time with understanding those languages and how they relate to C#. Thank you. Please see code:
using System;
namespace Converter
{
class Program
{
static void Main()
{
float? FahrenheitInput = null;
double? CelsiusInput = null;
float? KilogramInput = null;
float? PoundsInput = null;
int UserChoice = 0;
do
{
Console.WriteLine("What would you like to convert? Enter the corresponding number.\n1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit\n3. Pounds to Kilograms\n4. Kilograms to pounds\n5. Exit program");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
Console.WriteLine("Enter the temperature in Fahreinheit, number only:");
FahrenheitInput = float.Parse(Console.ReadLine());
Console.Clear();
Console.WriteLine(FahrenheitInput + " degrees fahrenheit in Celsius is " + Program.FahrenheitToCelsius(FahrenheitInput) + "\n\n");
break;
case 2:
Console.WriteLine("Enter the temperature in Celsius, number only:");
CelsiusInput = double.Parse(Console.ReadLine());
Console.Clear();
Console.WriteLine(CelsiusInput + " degrees Celius in fahrenheit is " + Program.CelsiusToFahrenheit(CelsiusInput) + "\n\n");
break;
case 5:
break;
default:
Console.WriteLine("This is not a valid entry. Please enter 1, 2, 3, 4, or 5.");
break;
}
} while (UserChoice != 5);
}
public static float? FahrenheitToCelsius(float? INPUT)
{
return (INPUT - 32) * 5 / 9;
}
public static double? CelsiusToFahrenheit(double? INPUT)
{
return INPUT * 1.8 + 32;
}
}
}
You can either put it in Try-Catch block or use a while loop to validate the user input.
below is your code with a while loop which validates users input.
class Program
{
static void Main(string[] args)
{
double FahrenheitInput = 0;
double CelsiusInput = 0;
double KilogramInput = 0;
double PoundsInput = 0;
int UserChoice = 0;
do
{
Console.WriteLine("What would you like to convert? Enter the corresponding number.\n1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit\n3. Pounds to Kilograms\n4. Kilograms to pounds\n5. Exit program");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
Console.WriteLine("Enter the temperature in Fahreinheit, number only:");
while (!double.TryParse(Console.ReadLine(), out FahrenheitInput))
{
Console.WriteLine("Invalid format, please input again!");
};
Console.Clear();
Console.WriteLine(FahrenheitInput + " degrees fahrenheit in Celsius is " + Program.FahrenheitToCelsius(FahrenheitInput) + "\n\n");
break;
case 2:
Console.WriteLine("Enter the temperature in Celsius, number only:");
while (!double.TryParse(Console.ReadLine(), out CelsiusInput))
{
Console.WriteLine("Invalid format, please input again!");
};
Console.Clear();
Console.WriteLine(CelsiusInput + " degrees Celius in fahrenheit is " + Program.CelsiusToFahrenheit(CelsiusInput) + "\n\n");
break;
case 5:
break;
default:
Console.WriteLine("This is not a valid entry. Please enter 1, 2, 3, 4, or 5.");
break;
}
} while (UserChoice != 5);
}
public static double FahrenheitToCelsius(double INPUT)
{
return (INPUT - 32) * 5 / 9;
}
public static double CelsiusToFahrenheit(double INPUT)
{
return INPUT * 1.8 + 32;
}
}
TryParse is your good friend here. In most scenarios, you should favor using TryParse than Parse. In your example, you can do something like:
int validInt;
int.TryParse(Console.ReadLine(), out validInt);
float validFloat;
float.TryParse(Console.ReadLine(), out validFloat);
Parse vs. TryParse
The easiest way, IMHO, to change the routine is to rewrite Parse into corresponding TryParse:
// UserChoice = int.Parse(Console.ReadLine());
UserChoice = int.TryParse(Console.ReadLine(), out UserChoice) ? UserChoice : -1;
...
A bit more complex (you have to convert float into float?)
// FahrenheitInput = float.Parse(Console.ReadLine());
float v;
FahrenheitInput = float.TryParse(Console.ReadLine(), out v) ? (float?) v : null;
The same scheme for CelsiusInput
// CelsiusInput = double.Parse(Console.ReadLine());
double d;
CelsiusInput = double.TryParse(Console.ReadLine(), out v) d (double?) d : null;
The underlying mechanic of the code is
We try to parse user input TryParse(Console.ReadLine()...
If parse succeeds (and thus TryParse returns true) we just return the out (parsed value).
If parse fails (and thus TryParse returns false) we return some special a value (-1 for UserChoice or null in case of FahrenheitInput or CelsiusInput)
P.S. in the first switch you have just case 1, case 2 and case 5; however, you've put "This is not a valid entry. Please enter 1, 2, 3, 4, or 5." in the error message. It seems, that you have to either implement case 3 and case 4 in the switch or edit the error message.
Use int.TryParse instead of int.Parse and float.tryParse instead of float.Parse
While all the answers provided seem to work, the question you asked was
how can I prevent exceptions from being thrown [..]
and I just want to point out that you do this by putting the part which throws the exception in an try-catch-block. What this does is it executes the code within try until an exception is beeing thrown and then passes this exceptions as a parameter to the catch-part where you can handle it:
EXAMPLE
do
{
try
{
// the code you already have
}
catch (Exception ex)
{
Console.WriteLine("This is no valid input (" + ex.Message + ")! Try again...");
}
} while (UserChoice != 5);
Of course preventing exceptions from beeing thrown at all in the first place is the better way (as all the other answers do suggest), but this approach works as well and ist more generic in case you run into a similar problem in the future. Using switch-case-statements for error-handling is quite common practice...

System.FormatException in C#

I keep getting a FormatException in each of the cases on the line where I try to assign the value for the sale variable. Anyone know what I am doing wrong? I am supposed to make this console program as homework to learn about loops, but I am finding out more about other things. It is supposed to keep a running tab of salesperson's commission based a a 10% commission of each sale. Anyways, here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TubSales
{
class Program
{
static void Main(string[] args)
{
char initial;
const double COMM_INT = 0.10;
double sale, aComm = 0, bComm = 0, eComm = 0;
Console.Write("Enter 'A' for Andrea, 'B' for Brittany,\n'E' for Eric, or 'Z' to quit >> ");
initial = Convert.ToChar(Console.Read());
while (initial != 'z' && initial != 'Z')
{
switch (initial)
{
case 'a':
case 'A':
Console.Write("Enter the sales for Andrea >> ");
sale = Convert.ToDouble(Console.ReadLine());
aComm = aComm + COMM_INT * sale;
break;
case 'b':
case 'B':
Console.Write("Enter the sales for Brittany >> ");
sale = Convert.ToDouble(Console.ReadLine());
bComm = bComm + COMM_INT * sale;
break;
case 'e':
case 'E':
Console.Write("Enter the sales for Eric >> ");
sale = Convert.ToDouble(Console.ReadLine());
eComm = eComm + COMM_INT * sale;
break;
default:
Console.WriteLine("You did not enter a valid initial");
break;
}
Console.Write("Enter 'A' for Andrea, 'B' for Brittany, or 'E' for Eric >> ");
initial = (char)Console.Read();
}
Console.WriteLine("Andrea had {0}, Brittany had {1}, and Eric had {2} in commissions.", aComm.ToString("C"), bComm.ToString("C"), eComm.ToString("C"));
Console.Write("Press any key to exit... ");
Console.ReadKey();
}
}
}
I keep getting a FormatException in each of the cases on the line where I try to assign the value for the sale variable. Anyone know what I am doing wrong?
The Convert.ToDouble method will raise a FormatException if the string (returned from Console.ReadLine()) is not a valid number.
Typically, if you want to parse user input, it's a better idea to use Double.TryParse instead, as this lets you determine whether the input was a valid number without catching the exception.
This would normally look something like:
Console.Write("Enter the sales for Andrea >> ");
while (!double.TryParse(Console.ReadLine(), out sale))
{
Console.WriteLine("Value entered was not a valid number.");
Console.Write("Enter the sales for Andrea >> ");
}
// Once you get here, "sale" will be set appropriately
While Reed's answers is great, it's not the problem here.
What really happens is the same situation as this one
Console.Read only read the "Second part of the carriage return" and returns "". This is why the Convert fails.
replace
initial = Convert.ToChar(Console.Read());
with
initial = Convert.ToChar(Console.ReadLine());
Replace
initial = Convert.ToChar(Console.Read());
with
initial = Convert.ToChar(Console.ReadLine().FirstOrDefault());

Categories

Resources