Trying to figure out my exceptions handling C#. Any idea? - c#

I am trying to figure out what is wrong with my code. New to C#. For some reason when I enter a number in for input when the question is asked.. it just ends and says "exited with code 0". My exceptions handling DOES work but actually entering in the correct numbers doesn't seem to work. Any idea?
using System;
namespace MealInputLayton
{
class Program
{
static void Main(string[] args)
{
decimal mealOne = 0;
decimal mealTwo = 0;
decimal dessertOne = 0;
decimal dessertTwo = 0;
decimal subtotal = 0;
const decimal TIP_PERCENTAGE = 0.2m;
const decimal TAX_RATE = 0.07m;
decimal total = 0;
decimal numPeople = 2;
decimal totalPerPerson = 0;
decimal tip = 0;
decimal tax = 0;
Console.WriteLine("How much did your first meal cost?");
try
{
mealOne = Convert.ToDecimal(Console.ReadLine());
}
catch (System.FormatException)
{
Console.WriteLine("Error for 1st meal price. Input string was not in a correct format.");
Console.WriteLine("How much did your second meal cost?");
try
{
mealTwo = Convert.ToDecimal(Console.ReadLine());
}
catch (System.FormatException)
{
Console.WriteLine("Error for 2nd meal price. Input string was not in a correct format.");
Console.WriteLine("How much did your first dessert cost?");
try
{
dessertOne = Convert.ToDecimal(Console.ReadLine());
}
catch (System.FormatException)
{
Console.WriteLine("Error for 1st dessert price. Input string was not in a correct format.");
Console.WriteLine("How much did your second dessert cost?");
try
{
dessertTwo = Convert.ToDecimal(Console.ReadLine());
}
catch (System.FormatException)
{
Console.WriteLine("Error for 2nd dessert price. Input string was not in a correct format.");
subtotal = mealOne + mealTwo + dessertOne + dessertTwo;
tip = subtotal * TIP_PERCENTAGE;
tax = subtotal * TAX_RATE;
total = subtotal + tax + tip;
totalPerPerson = total / numPeople;
Console.WriteLine("Subtotal: " + "$" + "{0:0.00}", subtotal);
Console.WriteLine("Tax: " + "$" + "{0:0.00}", tax);
Console.WriteLine("Tip: " + "$" + "{0:0.00}", tip);
Console.WriteLine("Total: " + "$" + "{0:0.00}", total);
Console.WriteLine("Per Person: " + "$" + "{0:0.00}", totalPerPerson);
}
}
}
}
}
}
}

This answer summarizes what everyone mentioned in the comments - the best way to handle exceptions is to avoid them (which is a bit facetious - but true here).
Exceptions should be used for exceptional circumstances, and particularly when you need to bubble an error state up through a deep stack. Users fat-fingering a number is hardly exceptional; it's something to be expected.
As #DiplomacyNotWar pointed it, it makes sense to push all the UI into a separate function. That way, you can re-prompt if the user makes an error (again, that's something to be expected). You could also put validation in there (for example, optionally specifying min and max values (I didn't do that).
So, here's a function that prompts users for a decimal value, and doesn't let them go further until they enter a valid number (if they really want to quit, they can press <Ctrl>+C)
private static decimal PromptForDecimal(string prompt, string? onError = null)
{
if (onError == null)
{
onError = $"You must enter a valid decimal value: {prompt}";
}
Console.Write($"{prompt}: ");
while (true)
{
var response = Console.ReadLine();
if (decimal.TryParse(response, out decimal value))
{
return value;
}
//otherwise:
Console.Write($"{onError}: ");
}
}
Note that you can optionally have a separate prompt that shows up if there's an error. Otherwise, you get a canned version based on the original prompt. It's up to you what you want to do.
Also note that the string? onError = null parameter expression describes onError as a Nullable String. You don't need the question mark if you aren't using Nullable Reference Types
With that in place, your code gets a whole lot simpler:
const decimal TIP_PERCENTAGE = 0.2m;
const decimal TAX_RATE = 0.07m;
decimal numPeople = 2.0m;
var mealOne = PromptForDecimal("How much did your first meal cost?");
var mealTwo = PromptForDecimal("How much did your second meal cost?");
var dessertOne = PromptForDecimal("How much did your first dessert cost?");
var dessertTwo = PromptForDecimal("How much did your second dessert cost?");
var subtotal = mealOne + mealTwo + dessertOne + dessertTwo;
var tip = subtotal * TIP_PERCENTAGE;
var tax = subtotal * TAX_RATE;
var total = subtotal + tax + tip;
var totalPerPerson = total / numPeople;
Console.WriteLine($"Subtotal: {subtotal:0.00}");
Console.WriteLine($"Tax: {tax:0.00}");
Console.WriteLine($"Tip: {tip:0.00}");
Console.WriteLine($"Total: {total:0.00}");
Console.WriteLine($"Per Person: {totalPerPerson:0.00}");
Note that I removed all your variable declarations from the top, and put them where they are used. It makes the code easier to read. Don't worry, var in these declarations means decimal since PromptForDecimal returns a decimal value.
If you do want to pre-declare your variables, consider not initializing them. If you had left all your declarations (of mealOne, dessertTwo, tip, etc.) uninitialized, and you tried to use them before they had values, the compiler would alert your. If you forgot to use one of them, the editor would put a squiggly line under the variable name (try it out).
Finally, an expression like $"Subtotal: {subtotal:0.00}" is known as an Interpolated String. They make your code easier to read.

Related

Subtraction between multiple random input numbers

i want to make user input random number example : 5-3-10-50
, system will split " - " and then the result 5 3 10 50
, how to make subtraction from first number minus second number and so on,
like this 5 - 3 = 2 , 2 - 10 = -8 , -8 - 50 = -58
and then system will print the final answer -58
my code :
bool Subtraction = true;
int AskSubtraction = 0;
while (Subtraction)
{
Console.Write("\n" + "input number ( example : 1 - 2 - 3 - 4 ) : ");
var InputNumber = Console.ReadLine();
double Answer = 0;
foreach (var result in InputNumber.Split('-'))
{
if (double.TryParse(result, out _))
{
double NumberResult = Convert.ToDouble(result);
Answer -= NumberResult;
}
else
{
Console.WriteLine("\n" + "Wrong input !");
AskSubtraction++;
}
}
Console.WriteLine("\n" + "subtraction result : " + Answer);
}
i know my code is wrong, im beginner i already try to fix this but i cant fix it until now, i hope someone tell me what's wrong with my code and fix it too, thank you.
The reason yours doesn't work is because you set Answer = 0.
And you used foreach. On the first iteration of the loop, the first number is subtracted from Answer which results in -5.
Use for (int i=1; i<arr.Length; i++)
instead of foreach
Start from index 1, and then subtract the values.
Example:
var arr = InputNumber.Split('-');
double Answer = 0;
if (double.TryParse(arr[0], out _))
{
// We set Answer to the first number, since nothing is subtracted yet
Answer = Convert.ToDouble(arr[0]);
}
// We start index from 1, since subtraction starts from 2nd number on the String array
for (int i=1; i<arr.Length; i++)
{
if (double.TryParse(arr[i], out _))
{
double NumberResult = Convert.ToDouble(arr[i]);
Answer -= NumberResult;
}
}
Tested on Online C# Compiler
You would need a condition inside the foreach loop to check for the first parsed double before you begin subtraction. Also there is no need to call Convert.ToDouble() since the double.TryParse() function already returns the parsed double value, All you would need is a variable to contain the out value of the double.TryParse() function, See example below
bool Subtraction = true;
int AskSubtraction = 0;
while (Subtraction)
{
Console.Write("\n" + "input number ( example : 1 - 2 - 3 - 4 ) : ");
var InputNumber = Console.ReadLine();
double Answer = 0;
double numResult;
foreach (var result in InputNumber.Split('-'))
{
if (double.TryParse(result, out numResult))
{
if(Math.Abs(Answer)>0){
Answer -= numResult;
}
else{
Answer=numResult;
}
}
else
{
Console.WriteLine("\n" + "Wrong input !");
AskSubtraction++;
}
}
Console.WriteLine("\n" + "subtraction result : " + Answer);
}

system.formatexception in while(true) loop

The subject is a little problem:
Write a program and continuously ask the user to enter a number or "ok" to exit. Calculate the sum of all the previously entered numbers and display it on the console.
Here is my code:
var sum = 0;
while (true)
{
Console.WriteLine("Enter a number or ok to exit:");
if (Console.ReadLine() == "ok") break;
sum += Convert.ToInt32(Console.ReadLine());
Console.WriteLine(sum);
}
When I tap ok, it terminate.
When I tap number and enter, it shows system.formatexception:The input string is not in the correct format.
I know one of the solution is
var sum = 0;
while (true)
{
Console.Write("Enter a number (or 'ok' to exit): ");
var input = Console.ReadLine();
if (input.ToLower() == "ok")
break;
sum += Convert.ToInt32(input);
}
Console.WriteLine("Sum of all numbers is: " + sum);
Maybe My code looks a little weired, But Why is my code wrong?
Reason is input will be "ok". Can not convert that into an integer.
first you have to store the first input value into other variable.
then convert that string into integer and get summation.
var sum = 0;
while (true)
{
Console.Write("Enter a number (or 'ok' to exit): ");
var input = Console.ReadLine();
int newVariable = 0;
if (input.ToLower() != "ok")
{
newVariable = Convert.ToInt32(input);
}
input = Console.ReadLine();
if (input.ToLower() == "ok"){
break;
sum += newVariable;
}
}
Console.WriteLine("Sum of all numbers is: " + sum);
If there any problem here please let me know.
Try this:
var sum = 0;
while (true)
{
Console.WriteLine("Enter a number or ok to exit:");
String ans = Console.ReadLine();
if (ans == "ok" || ans.ToLower() == "ok") break;
sum += Convert.ToInt32(ans);
Console.WriteLine(sum);
}
Here I've just store input entered by user in one variable and use that variable in further process.
In your first code you have take input two times, first one is in IF condition and second in parsing, that may cause the problem.
The correct way to do this is to use int.TryParse for your conversion from a string to a number. TryParse attempts to convert the string to a number, but if it cannot do so (for example, the string contains more than just numeric digits) it will fail gracefully instead of causing an exception. The other answers so far will all cause an unhandled FormatException if something non-numeric is entered other than "ok". By using int.TryParse you can handle the case where it's a valid number, as well as the case where it is invalid, and then alert the user. Here's an example within the context of your code:
// I prefer using concrete types for numbers like this, so if anyone else
// reads it they know the exact type and numeric limits of that type.
int sum = 0;
int enteredNumber = 0;
while (true)
{
Console.Write("Enter a number (or 'ok' to exit): ");
var consoleInput = Console.ReadLine();
if (consoleInput.ToLower() == "ok")
break;
if(int.TryParse(consoleInput, out enteredNumber))
{
sum += enteredNumber;
}
else
{
Console.WriteLine("You entered '" + consoleInput + "', which is not a number.");
}
}
Console.WriteLine("Sum of all numbers is: " + sum.ToString());
This is better because you know you have no control over the user's input other than to validate it yourself, and so it's better to speculatively convert the number and be alerted to success or failure without triggering an exception. Wrapping everything with a try/catch block is not a proper solution.
Your first code example, as rightly pointed out in the comments, reads a line, tests it for 'ok', then throws it away, reads another line, and uses that to add to the sum, which is not what you wanted.
After some quick research, I would say the most concise way to handle this in C# is probably something like your second code example. In F# I was able to come up with the following examples (one is a loop, the other uses sequences, i.e. IEnumerable<_>s) but I found no concise way to get the same with C# and LINQ…
let inputLoop () =
let rec aux sum =
match stdin.ReadLine () with
| "ok" -> sum
| s -> aux (sum + int s)
stdout.WriteLine (aux 0 |> string)
let inputSeq () =
fun _ -> stdin.ReadLine ()
|> Seq.initInfinite
|> Seq.takeWhile (fun s -> s <> "ok")
|> Seq.sumBy int
|> string
|> stdout.WriteLine
Try it :)
var sum = 0;
while (true)
{
Console.Write("Enter a number: or ok to exit : ");
String input = Console.ReadLine();
if (input == "ok" || input.ToLower() == "ok") break;
if(string.IsNullOrWhiteSpace(input))
continue;
sum += Convert.ToInt32(input);
}
Console.WriteLine("Total Result: " + sum);
Write a program and continuously ask the user to enter a number or "ok" to exit. Calculate the sum of all the previously entered numbers and display it on the console. Happy Coding
var sum = 0;
while (true)
{
Console.Write("Write number or write \"ok\" for exit: ");
var input = Console.ReadLine();
if (input.ToLower() != "ok")
{
sum += Convert.ToInt32(input);
continue;
}
break;
}
Console.WriteLine("All sum: " + sum + ".");
This is one way to do it. I'm just learning to do this!
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number to know the sum or press ok to exit and display the sum");
int sum = 0;
while (true) // to run the program continously asking user input
{
Console.WriteLine("Enter Number: ");
var input = Console.ReadLine(); // takes user input
if (input.ToLower() == "ok") // compares user input to string ok
break; //if user input is ok, breaks the loop and sum is displayed
var inputInInt = Convert.ToInt32(input); // if user input is int, continues to convert it to integer
sum += inputInInt; // user input in interger is added to sum
}
Console.WriteLine("The sum of entered numbers is: " + sum);
Console.ReadLine();
}
}

Multiplying Different Data Types in an Array in C#

I am receiving an error "Operator '*' cannot be applied to operands of type 'int' and 'decimal[]'", as I am attempting to multiply two values with different data types (one being a value located in an array). My question is how am I able to multiple numberOfMinutes * perMinuteRate in my code below? My variable is called total, which I declared a double data type (although may be incorrect).
I tried changing data types and played with formatting (like ToString), but I am not sure what to do. I also tried to google the answer with no success.
I am by no means a professional programmer; I'm not in school. I'm a data analyst who is learning to program.
Here is my code:
static void Main(string[] args)
{
int[] areaCodes = { 262, 414, 608, 715, 815, 920 };
decimal[] perMinuteRate = { .07m, .1m, .05m, .16m, .24m, .14m };
int numberOfMinutes;
int userAreaCode;
string inputString = "1";
while (inputString != "0")
{
int x;
Console.WriteLine("Enter the area code for your call (or 1 to end):");
inputString = Console.ReadLine();
userAreaCode = Convert.ToInt32(inputString);
Console.WriteLine("How many minutes will your call last?");
inputString = Console.ReadLine();
numberOfMinutes = Convert.ToInt32(inputString);
for (x = 0; x < areaCodes.Length; x++)
{
if (userAreaCode == areaCodes[x])
{
***double total = numberOfMinutes * perMinuteRate;***
Console.WriteLine("You call to {0} will cost {1} per minute for a total of {2}.", areaCodes[x], perMinuteRate[x].ToString("C"), total.ToString("C"));
x = areaCodes.Length;
}
}
if (x != areaCodes.Length)
{
Console.WriteLine("I'm sorry; we don't cover that area.");
inputString = "1";
}
else
{
Console.WriteLine("Thanks for being our customer.");
inputString = "0";
}
Console.ReadLine();
}
}
Thank you in advance.
Change:
double total = numberOfMinutes * perMinuteRate;
to
double total = (double)(numberOfMinutes * perMinuteRate[x]);
The same way you index into perMinuteRate in the line directly below.
The expression [int] * [decimal] will result in a decimal, and the cast (double) will convert it to a double
To avoid loss of precision, change it to:
decimal total = numberOfMinutes * perMinuteRate[x];

Validating multiple user input in console application

I'm pretty new to C# and am wondering how I'd go about validating user input to meet the following requirements:
has to be a decimal type. if it isn't it should ask the user to enter a decimal value. (Which i believe i have covered in my code below)
also has to be within a specific range (1 - 1,000,000). If it isn't it should ask the user to enter a number within the correct range
What's the most efficient way of doing this considering i will have multiple user input to validate in the same sort of way.
decimal balance;
Console.Write("Starting Balance: $");
while (!decimal.TryParse(Console.ReadLine(), out balance))
{
Console.Write("Please enter a valid decimal value: $");
}
EDITED BELOW
How about this?
decimal balance;
Console.Write("Starting Balance: $");
while(true)
{
if (!decimal.TryParse(Console.ReadLine(), out balance))
Console.Write("Please enter a valid decimal value: $");
else if (balance < 1 || balance > 100)
Console.Write("Please enter an amount between 1 and 100: ");
else
break;
}
Console.WriteLine("Balance entered is: " + balance.ToString("n"));
return val; line gave me an error so i left it out but the above seems to work?
I'd try something like:
decimal GetUserInput(string inputQuery, decimal min, decimal max)
{
Console.Write(inputQuery);
decimal val;
while(true)
{
if(!decimal.TryParse(Console.ReadLine(), out val))
Console.Write("Please enter a valid decimal value: $");
else if(val < min || val > max)
Console.Write("Please enter an amount between " + min + " and " + max + ": $");
else // the value is a decimal AND it's correct
break;
}
return val;
}
Then use it like:
var startingBalance = GetUserInput("Starting Balance: $", 1, 100000);
var endingBalance = GetUserInput("Ending Balance: $", 1, 100000);
//...
If your min and max are fixed, then you could not pass them as arguments and use a fixed check. And you could also avoid having the query : $ passed in if needed, but I'll leave that to you
Update
The reason why the return val line was giving you an error was because you were inlining it (probably in a void returning function). What I was doing was making a function since you specified it needed to be reusable.
So in your program, you need to make a separate function... your program would look something like this:
class Program
{
// We're declaring this function static so you can use it without an instance of the class
// This is a function, so it can be called multiple times, with different arguments
static decimal GetUserInput(string inputQuery, decimal min, decimal max)
{
// Write the argument "inputQuery" to console
Console.Write(inputQuery);
decimal val;
// Loop indefinitely
while(true)
{
// Read from console into a decimal "val"
if(!decimal.TryParse(Console.ReadLine(), out val))
// It was not a correct decimal, so write the prompt
Console.Write("Please enter a valid decimal value: $");
// It was a correct decimal
else if(val < min || val > max)
// But not in range, so write a different prompt
Console.Write("Please enter an amount between " + min + " and " + max + ": $");
// It was a decimal and within range
else
// so we break the infinite loop and exit after the "}"
break;
// If we have got to this point (we didn't hit the "break"),
// it was either not a decimal or it wasn't within range,
// so it'll loop again and ask for a value from console again.
// The prompt was already written above (in the "ifs")
}
// We got out of the while(true){} loop, so it means we hit "break"
// above, and that means "val" contains a correct value (decimal and
// within range), so we return it to the caller
return val;
}
static void Main()
{
// Your original code went here, but see how my function is *outside* function Main()
// You use my function (GetUserInput) here:
var startingBalance = GetUserInput("Starting Balance: $", 1, 100000);
var endingBalance = GetUserInput("Ending Balance: $", 1, 100000);
// Then with the returned values (stored in "startingBalance"
// and "endBalance"), you can do what you want:
Console.WriteLine("Starting balance was: " + startingBalance.ToString("n"));
}
}
I've made a fiddle with the whole program so you can test online and make changes: https://dotnetfiddle.net/HiwwIP
If I were you, I would do such:
bool isInvalid, isOutOfRange;
decimal balance = 0;
isOutOfRange = true;
do
{
string input = Console.ReadLine();
isInvalid = !Decimal.TryParse(input, out balance);
if (!isInvalid)
{
// use balance<=1 if 1 should not be included
// use balance>=1000000 if 1000000 should not be included
isOutOfRange = (balance < 1 || balance > 1000000);
}
if (isInvalid)
{
Console.WriteLine("Please enter a valid decimal value: $");
}
else if (isOutOfRange)
{
Console.WriteLine("Please enter value between 1 and 1000000: $");
}
} while (isInvalid || isOutOfRange);
Console.WriteLine("{0}, That is a valid value!", balance.ToString());
Console.ReadKey();
Of course you can shortcut by eliminating bool definitions and directly calling functions instead; but I wrote in detail for clarity as you indicated that you are "pretty new".

Please help i have got very confused with the if statement below

I want the user to input a number, but if it is below zero I would like to show an error message and then loop round and ask the user for another number. Here is the code I have at the moment.
// this determines what the loop does.
for (int CustPos = 0; CustPos < LineNum; CustPos = CustPos + 1)
{
// this asks the user to enter the sales figures
Console.Write("enter sales figures for" + customer[CustPos] + " ");
// this is user's input is read in and stored.
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - continue
{
Console.WriteLine("");
Console.WriteLine("entry invalid");
Console.WriteLine("enter another value");
}
else//FALSE -> Go back to start of loop
{
Console.WriteLine("");
}
//this section displays the cust name, sales figure 70/30.
Console.WriteLine(" ");
fee_payable[CustPos] = (sales_figures[CustPos] / 100.0)
* licence_fee_in_percent[CustPos];
Console.WriteLine(customer[CustPos] +
" ----------- " + fee_payable[CustPos]);
Console.WriteLine("Licence fee to be paid in GBP is :" +
fee_payable[CustPos]);
seventy_percent_value = ((fee_payable[CustPos] / 10.0) * 7);
Console.WriteLine("70 percent of this fee is" +
seventy_percent_value);
thirty_percent_value = ((fee_payable[CustPos] / 10.0) * 3);
Console.WriteLine("30 percent of this fee is" +
thirty_percent_value);
Console.WriteLine(" ");
}
. Please help all advice will be greatly appreciated!
Thanks
I think a do-while loop would be better here, pseudocode:
userInput = -1
do
{
userInput = Console.ReadLine
}
while (userInput <0)
Colin E.
Youre on the right track, just look at the keyword Continue
This is the example in the link:
using System;
class ContinueTest
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
{
continue;
}
Console.WriteLine(i);
}
}
}
Note: The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.
if (sales_figures[CustPos] < MIN_SALES_FIGURE) //True - Continue //FALSE -> Go back to start of loop
You don't actually have any code here to make it go back to the start of the loop.
I recommend that you write it all out as pseudocode first, then turn it into code:
if (number entered is too low)
then restart loop
otherwise carry on
Instead of an if, you'll want a while:
while( sales_figure[CustPos] < 0 )
{
Console.Write("enter sales figures for" + customer[CustPos] + " ");
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
}
Which guarantees that it will keep prompting until they enter something greater than zero.
Continue, does NOT do what you want it to. Continue means, "move on and ignore this iteration" which means you'd have an incorrect value for that customer.
Use a WHILE loop in combination with your IF:
continueflag = 0;
while (continueflag == 0)
{
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
Console.WriteLine("");
if (sales_figures[CustPos] >= MIN_SALES_FIGURE) {
Console.WriteLine("entry invalid");
Console.WriteLine("enter another value");
} else continueflag = 1;
}

Categories

Resources