C# Variable Doesn't Change When The Calculation is Performed - c#

I'm creating a simple calculator for my C# class and having some trouble with the final calculations.
I've declared my variables and I'm using simple if then statements for the order. My problem is, after I've defined my variables, the information they contain doesn't change when the calculation is performed, they simply just output zero as defined by the original value instead of outputting the new amount from the if statement (TotalCost, Tax, TotalWithTax).
I've input different numbers in place of the zero and the formulas work, it's just that the value never changes to what I have stated in the
If statements. Is it not possible to adjust the value of a variable from within a if statement?
What is another possible way to go about this if that is the case?
Trying to keep it simple as I'm not really proficient with console programming.
string SizeName = "";
double SizeCost = 0;
string ToppingName = "";
double ToppingCost = 0;
double TotalCost = (SizeCost + ToppingCost);
double Tax = (TotalCost*0.085);
double TotalWithTax = (TotalCost + Tax);
//Print a greeting message.
Console.WriteLine("Welcome to the Central Pizza Parlor!");
//Ask if the customer would like to order a pizza.
Console.WriteLine("Would you like to order a pizza today? Enter y for Yes or n for No.");
string Order = Console.ReadLine();
//Start the order if answer is Yes, if not, then exit the program.
if (Order == "y")
{
//Continue with order.
Console.WriteLine("Great! Let's get started, please pick the size of your pizza:");
Console.WriteLine("1 - Small $5.00");
Console.WriteLine("2 - Medium $7.00");
Console.WriteLine("3 - Large $9.00");
//Get pizza size for order.
Console.WriteLine("Please enter the number for the pizza size you would like.");
string sizeAsAString = Console.ReadLine();
int size = Convert.ToInt32(sizeAsAString);
//Use If Else statement to set the variable value for SizeCost.
if (size == 1)
{
SizeCost = 5.0;
SizeName = ("Small");
}
else if (size == 2)
{
SizeCost = 7.0;
SizeName = ("Medium");
}
else if (size == 3)
{
SizeCost = 9.0;
SizeName = ("Large");
}
//Have Customer select toppings.
Console.WriteLine("Please select which topping you would like on your pizza.");
;
Console.WriteLine("1 - Pepperoni $2.00");
Console.WriteLine("2 - Ham $2.00");
Console.WriteLine("3 - Onions $1.00");
Console.WriteLine("4 - Mushrooms $1.00");
Console.WriteLine("5 - Green Peppers $1.00");
Console.WriteLine("Please enter the number for the corresponding topping you would like.");
string toppingAsAString = Console.ReadLine();
int topping = Convert.ToInt32(toppingAsAString);
//Use If Else statement to set the variable value for ToppingCost.
if (topping == 1)
{
ToppingCost = 2.0;
ToppingName = ("Pepperoni");
}
else if (topping == 2)
{
ToppingCost = 2.0;
ToppingName = ("Ham");
}
else if (topping == 3)
{
ToppingCost = 1.0;
ToppingName = ("Onions");
}
else if (topping == 4)
{
ToppingCost = 1.0;
ToppingName = ("Mushrooms");
}
else if (topping == 5)
{
ToppingCost = 1.0;
ToppingName = "Green Peppers";
}
//Display order details.
Console.WriteLine("Here are the details for your order.");
Console.WriteLine("Thank you for your business!");
Console.WriteLine("You can pick up your pizza in 25 minutes!");
//Show current time of order.
DateTime now = DateTime.Now;
Console.WriteLine("Time Ordered: "+now+" ");
//Show Current time of order with additional 25 minutes for pickup.
DateTime pickup = DateTime.Now.AddMinutes(25);
Console.WriteLine("Pick Up At: "+pickup+" ");
//Output Pizza Size.
Console.WriteLine("Size: " +SizeName+ " ");
//OutPut Topping name.
Console.WriteLine("Topping: " +ToppingName+ " ");
Console.WriteLine("---------------");
//Output total price of size and topping chosen.
Console.WriteLine("Pizza Price: $ "+TotalCost+" ");
//Output tax amount.
Console.WriteLine("Tax: $" +Tax+ " ");
//Output total price with tax.
Console.WriteLine("Total Price: $" +TotalWithTax+ " ");
}
else
{
//Exit the program because the customer does not want to order a pizza.
Console.WriteLine("Alright, have a great day!");
}
Console.ReadLine();
}
}
}

All your calcualtions are at the top(in declaration). Performing your calculations with defaults will not output desired result.
double TotalCost = (SizeCost + ToppingCost);
double Tax = (TotalCost*0.085);
double TotalWithTax = (TotalCost + Tax);
Move these calcualtions to bottom (before outputting).
public class Program
{
public static void Main()
{
string SizeName = "";
double SizeCost = 0;
string ToppingName = "";
double ToppingCost = 0;
double TotalCost =0;
double Tax = 0;
double TotalWithTax = 0;
//Print a greeting message.
Console.WriteLine("Welcome to the Central Pizza Parlor!");
//Ask if the customer would like to order a pizza.
Console.WriteLine("Would you like to order a pizza today? Enter y for Yes or n for No.");
string Order = Console.ReadLine();
//Start the order if answer is Yes, if not, then exit the program.
if (Order == "y")
{
//Continue with order.
Console.WriteLine("Great! Let's get started, please pick the size of your pizza:");
Console.WriteLine("1 - Small $5.00");
Console.WriteLine("2 - Medium $7.00");
Console.WriteLine("3 - Large $9.00");
//Get pizza size for order.
Console.WriteLine("Please enter the number for the pizza size you would like.");
string sizeAsAString = Console.ReadLine();
int size = Convert.ToInt32(sizeAsAString);
//Use If Else statement to set the variable value for SizeCost.
if (size == 1)
{
SizeCost = 5.0;
SizeName = ("Small");
}
else if (size == 2)
{
SizeCost = 7.0;
SizeName = ("Medium");
}
else if (size == 3)
{
SizeCost = 9.0;
SizeName = ("Large");
}
//Have Customer select toppings.
Console.WriteLine("Please select which topping you would like on your pizza.");
;
Console.WriteLine("1 - Pepperoni $2.00");
Console.WriteLine("2 - Ham $2.00");
Console.WriteLine("3 - Onions $1.00");
Console.WriteLine("4 - Mushrooms $1.00");
Console.WriteLine("5 - Green Peppers $1.00");
Console.WriteLine("Please enter the number for the corresponding topping you would like.");
string toppingAsAString = Console.ReadLine();
int topping = Convert.ToInt32(toppingAsAString);
//Use If Else statement to set the variable value for ToppingCost.
if (topping == 1)
{
ToppingCost = 2.0;
ToppingName = ("Pepperoni");
}
else if (topping == 2)
{
ToppingCost = 2.0;
ToppingName = ("Ham");
}
else if (topping == 3)
{
ToppingCost = 1.0;
ToppingName = ("Onions");
}
else if (topping == 4)
{
ToppingCost = 1.0;
ToppingName = ("Mushrooms");
}
else if (topping == 5)
{
ToppingCost = 1.0;
ToppingName = "Green Peppers";
}
TotalCost = (SizeCost + ToppingCost);
Tax = (TotalCost*0.085);
TotalWithTax = (TotalCost + Tax);
//Display order details.
Console.WriteLine("Here are the details for your order.");
Console.WriteLine("Thank you for your business!");
Console.WriteLine("You can pick up your pizza in 25 minutes!");
//Show current time of order.
DateTime now = DateTime.Now;
Console.WriteLine("Time Ordered: "+now+" ");
//Show Current time of order with additional 25 minutes for pickup.
DateTime pickup = DateTime.Now.AddMinutes(25);
Console.WriteLine("Pick Up At: "+pickup+" ");
//Output Pizza Size.
Console.WriteLine("Size: " +SizeName+ " ");
//OutPut Topping name.
Console.WriteLine("Topping: " +ToppingName+ " ");
Console.WriteLine("---------------");
//Output total price of size and topping chosen.
Console.WriteLine("Pizza Price: $ "+TotalCost+" ");
//Output tax amount.
Console.WriteLine("Tax: $" +Tax+ " ");
//Output total price with tax.
Console.WriteLine("Total Price: $" +TotalWithTax+ " ");
}
else
{
//Exit the program because the customer does not want to order a pizza.
Console.WriteLine("Alright, have a great day!");
}
Console.ReadLine();
}
}
Fiddler Demo

You set TotalCost before changing the Values of SizeCost or ToppingCost.
Move these lines
double TotalCost = (SizeCost + ToppingCost);
double Tax = (TotalCost*0.085);
double TotalWithTax = (TotalCost + Tax);
to after your input section.

Related

Receipt like output in c#

int choice, quanti, decide, total, cash;
double change;
string dcount;
while (true)
{
Console.Clear();
string w = "WELCOME ";
Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop); // for setting string output on center top
Console.WriteLine(w);
Console.WriteLine("");
System.Threading.Thread.Sleep(2000); //time delay
string p = "HERE'S OUR MERCHANDISES! ";
Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop); // for setting string output on center top
Console.WriteLine(p);
System.Threading.Thread.Sleep(2000);//time delay
string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings",
"[3]RED VELVET Summer Package"};
for (int g = 0; g < products.Length; g++)
{
Console.WriteLine(products[g]);
}
Dictionary<int, int> ProductList = new Dictionary<int, int>();
while (true)
{
{
Console.Write("Pick your product: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("BLACKPINK Lightstick 1500php");
break;
case 2:
Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");
break;
case 3:
Console.WriteLine("RED VELVET Summer Package 980php");
break;
}
Console.WriteLine("Quantity of product: ");
quanti = int.Parse(Console.ReadLine());
if (!ProductList.ContainsKey(choice))
ProductList.Add(choice, quanti);
else
ProductList[choice] += quanti;
System.Threading.Thread.Sleep(2000);//time delay
Console.WriteLine("[1] Add more products \t [2] Pay: ");
decide = int.Parse(Console.ReadLine());
if (decide == 2)
break;
}
}
total = 0;
foreach (int key in ProductList.Keys)
switch (key)
{
case 1:
total += ProductList[key] * 1500;
break;
case 2:
total += ProductList[key] * 920;
break;
case 3:
total += ProductList[key] * 980;
break;
default:
total += 0;
break;
};
Console.WriteLine("To Pay: " + total);
Console.Write("Cash: ");
cash = Convert.ToInt32(Console.ReadLine());
Console.Write("Discount[s]suki [v]voucher: ");
dcount = Console.ReadLine();
if (dcount == "s")
{
change = (cash - total) - 0.7;
Console.WriteLine("Change: " + change);
}
else if (dcount == "v")
{
change = cash -(total - 0.5) ;
Console.WriteLine("Change: " + change);
}
else
{
change = cash- (total - 0.7) ;
Console.WriteLine("Change: " + change);
}
Console.WriteLine("Another shopping? [1]Yes [2] No");
choice = int.Parse(Console.ReadLine());
if (choice == 2)
break;
}
}
}
}
Good day, I'm doing a project for school. I've already asked earlier about my code and someone help me, thanks to him, unfortunately I still have a problem with my code. After the users inputs and giving her a change, it was supposed to output a receipt like, wherein it contains users purchased products. I've tried outputting the variables that have been used in acquiring users input but to my dismay, it can only output the user's first input product, it can't Output all the users purchase. The receipt like output was supposed to contain the user's products purchased, quantity of each product, total, cash, discount and change in horizontal line/tabular.
Quick way of printing out products and quantity for each would be to put after dcount = Console.ReadLine(); something like:
foreach(var entry in ProductList)
{
Console.WriteLine(products[entry.Key].Substring(3) + "......." + entry.Value);
}
I'll leave the amount of dots and cursor position to You.
Also, when dealing with currency it's better to use decimal due to higher precision (maybe not needed in this example.) And try to use Math.Round() function when displaying money amount - limit decimal places.

How to Loop and Sum Until to Get Enough Money?

Example code:
//money deposit
Console.Write("Enter said amount of money: ");
moneyres = Convert.ToDouble(Console.ReadLine());
if (moneyres < ticket)
{
double dif = ticket - moneyres;
Console.Write("Oh, sorry you are " + dif + " dollar(s) sort.\nPlease enter more money so you can purchase the ticket: ");
double m2 = Convert.ToDouble(Console.ReadLine());
double m3 = dif - m2;
while (m3 > 0)
{
Console.Write("Oh, sorry you are " + m3 + " dollar(s) sort.\nPlease enter more money so you can purchase the ticket: ");
}
}
else if (moneyres > ticket)
{
change = moneyres - ticket;
Console.WriteLine("So your change is: " + change + " dollar(s).\nHere you go. Have fun!");
}
else
{
Console.WriteLine("Here you go. Have fun!");
}
So let's say the ticket is 10 bucks and someone puts 5. Then the difference is going to be 5. Then lets say he adds 2 more.
How can I do a loop till the amount hits 0? (I am new to coding btw).
You can do all of this in a while loop.
double totalValue = 0.0;
while(totalValue < ticket)
{
Console.Write("Enter said amount of money: ");
moneyres = Convert.ToDouble(Console.ReadLine());
totalValue += moneyres;
if (totalValue > ticket)
{
// Enough
}
else
{
// Not enough
}
}
You should look into a while loop. Something like while(UserInput < ticket). UserInput in this case is the amount that the user pays.
So I did this. Thanks a lot for the suggestions!
double tot = 0.0;
while (tot < ticket)
{
string sp = "Enter money: ";
Console.Write(sp);
moneyres = Convert.ToDouble(Console.ReadLine());
tot += moneyres;
if (tot > ticket)
{
change = tot - ticket;
Console.WriteLine("Alright here is your change. That's " + change + " dolars.And
here is your ticket!\nEnjoy your ride!");
}
else if (tot==ticket)
{
Console.WriteLine("That's just perfect! Here you go!\nEnjoy your ride!");
}
else
{
Console.WriteLine("You did not enter enough money.");
}
}
Console.ReadKey();

I need help retrieving user input from my code

I've only been learning C# for about a week, and I have this personal project that I am working on. I am building a program to calculate a salesperson's monthly bonus. At the end of my code, I need to tell the user the total bonus amount and the bonus cannot exceed $230.
My question is, how do I retrieve the user inputs to get a total and how do I set a limit of $230?
Any help will be appreciated.
I tried using more if statements to retrieve what the user already input.
Console.WriteLine("What is the total number of items sold?");
int itemsSold = Convert.ToInt16(Console.ReadLine());
int itemBonus = 50;
if (itemsSold > 20)
{
Console.WriteLine("Your items sold bonus is {0} dollars" ,itemBonus);
}
else
Console.WriteLine("You have not sold enough items to recieve the item bonus");
Console.WriteLine("What is the total dollar value of all items sold?");
int bonus1 = 100;
int bonus2 = 200;
int dollarValue = Convert.ToInt16(Console.ReadLine());
double totalEarned1 = (dollarValue * bonus1 + itemBonus);
double totalEarned2 = (dollarValue * bonus2 + itemBonus);
if (dollarValue >= 1000 && dollarValue < 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus1);
}
else if (dollarValue >= 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus2 );
}
else
{
Console.WriteLine("You have not recieved a dollar value bonus");
}
Console.ReadLine();
You have to refactorize your code. Since if the user did not sell more than 20 items, it is of no use to continue the calculation:
Console.WriteLine("What is the total number of items sold?");
int itemsSold = Convert.ToInt16(Console.ReadLine());
int itemBonus = 50;
if (itemsSold > 20)
{
int bonus1 = 100;
int bonus2 = 200;
Console.WriteLine("Your items sold bonus is {0} dollars" ,itemBonus);
Console.WriteLine("What is the total dollar value of all items sold?");
int dollarValue = Convert.ToInt16(Console.ReadLine());
double totalEarned1 = (dollarValue * bonus1 + itemBonus);
double totalEarned2 = (dollarValue * bonus2 + itemBonus);
totalEarned1 = Math.Max( totalEarned1, 230 );
totalEarned2 = Math.Max( totalEarned2, 230 );
if (dollarValue >= 1000 && dollarValue < 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus1);
}
else if (dollarValue >= 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus2 );
}
else
{
Console.WriteLine("You have not recieved a dollar value bonus");
}
}
else {
Console.WriteLine("You have not sold enough items to recieve the item bonus");
}
Console.ReadLine();
Hope this helps.

"Input string was not in a correct format" when trying to make a "menu" in windows console app

I wrote this code for class a few days ago. Everything looks correct, however, when I enter the input, I immediately get an error.
Any help on this would be greatly appreciated.
Console.WriteLine("The following are the benefit packages: ");
Console.WriteLine("Employee only (E) ");
Console.WriteLine("Employee and spouse (S) ");
Console.WriteLine("Employee and children (C) ");
Console.WriteLine("Employee and family (F) ");
Console.WriteLine();
Console.WriteLine("Please select your benefit type for medical insurance: ");
string medical = Console.ReadLine();
double subtotal = Convert.ToDouble(Console.ReadLine());
///the error is with the double above. I'm not sure how to work around this.
if (medical == "e")
{
subtotal += 0;
}else if (medical == "s")
{
subtotal += 50;
}else if (medical == "c")
{
subtotal += 100;
}else if (medical == "f")
{
subtotal += 200;
}
Console.WriteLine("Please select your benefit type for dental insurance: ");
string dental = Console.ReadLine();
if (dental == "e")
{
subtotal += 50;
}else if (dental == "s")
{
subtotal += 125;
}else if (dental == "c")
{
subtotal += 225;
}else if (dental == "f")
{
subtotal += 325;
}
Console.WriteLine("Please select your benefit type for vision insurance: ");
string vision = Console.ReadLine();
if (vision == "e")
{
subtotal += 25;
}else if (vision == "s")
{
subtotal += 60;
}else if (vision == "c")
{
subtotal += 110;
}else if (vision == "f")
{
subtotal += 185;
}
Console.WriteLine("Please enter your years in service: ");
int years = Convert.ToInt16(Console.ReadLine());
double discount = Convert.ToDouble(Console.ReadLine());
if (years >= 20)
{
discount = .2;
}else if (years >= 10 && years < 20)
{
discount = .1;
}else if (years < 10)
{
discount = 0;
}
double total = subtotal + (subtotal * discount);
Console.WriteLine("Your total out-of-pocket premium: " + total);
Console.ReadLine();
Each time the user "selects" a package, the value of the "subtotal" will increase. Later, the final total is calculated for, giving the user a final cost of these premiums.
string medical = Console.ReadLine();
double subtotal = Convert.ToDouble(Console.ReadLine());
this is asking for two inputs, the first Console.ReadLine(); sets your string medical. this is good.
lets say i enter E
now medical = "E"
now before it gets to your loop to determine what to do with "E", your asking for another input, and trying to convert it to a double. i would change the second line:
double subtotal = Convert.ToDouble(Console.ReadLine());
to
double subtotal = 0.0;
this will give you an starting value. if you know the "procedure cost" which i assume is your double, you can set it to whatever you want, but if you want to ask for the value use:
double subtotal = double.Parse(System.Console.ReadLine());
also the way your options are set up....
Console.WriteLine("Employee only (E) ");
Console.WriteLine("Employee and spouse (S) ");
Console.WriteLine("Employee and children (C) ");
Console.WriteLine("Employee and family (F) ");
is going to encourage people to enter capital letters. this is fine, but you're only checking for lower case, i suggest:
if (vision == "e"||vision == "E")
EDIT: i forgot to tell you, when working with dollar amounts, its best to use decimal not double.

The program compiler won't let me use "else" in the last 2 cases and I can't see why

I can't find why cases 2,3 wont let me use else for the category selection but it lets me use it in case 1. Where is it falling apart? What code needs editing?
I fixed the elses I think but I got a new error that I've never seen before control cannot fall through from one case label('case3:') to another. What does this mean?
using System;
class Program
{
enum Numbers { standard = 1, express = 2, same = 3 };
const int A = 1, B = 2;
const int Y = 3, N = 4;
static void Main()
{
double cost, LB;
int Number_of_items;
int myNumbers;
char catagory;
char surcharge = 'Y';
Console.WriteLine("please enter the type of shiping you want");
Console.WriteLine("Enter 1:standard shipping.");
Console.WriteLine("Enter 2:express shipping.");
Console.WriteLine("Enter 3:same day shipping.");
myNumbers = int.Parse(Console.ReadLine());
switch ((Numbers)myNumbers)
{
case Numbers.standard:
Console.WriteLine("thankyou for chooseing standerd shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
catagory = char.Parse(Console.ReadLine());
{
if (catagory == 'A')
{
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 3 * Number_of_items;
Console.Write("is this shipment going to alaska or Hawaii? (Y or N)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
cost = cost + 2.50;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
}
else
Console.Write("please enter the weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 1.45 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
} surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
cost = cost + 2.50;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
break;
case Numbers.express:
Console.WriteLine("thankyou for chooseing Express Shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
catagory = char.Parse(Console.ReadLine());
if (catagory == 'A')
{
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 4 * Number_of_items;
{
Console.Write("is this shipment going to alaska or Hawaii? (Y or N)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
cost = cost + 5.00;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
}
}
else
{
{
Console.Write("please enter the weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 2.50 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
}
}
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
cost = cost + 5.00;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
break;
case Numbers.same:
Console.WriteLine("thankyou for chooseing Same Day Shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
catagory = char.Parse(Console.ReadLine());
if (catagory == 'A')
{
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 5.50 * Number_of_items;
Console.Write("is this shipment going to alaska or Hawaii? (Y or N)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}.", cost);
}
}
else
{
Console.WriteLine("total cost is {0}.", cost);
}
}
else
{
{
Console.Write("please enter the weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 3.00 * LB;
surcharge = char.Parse(Console.ReadLine());
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
}
if (surcharge == 'Y')
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
break;
}
Console.ReadLine();
}//End Main()
}
}
I tried to run your code through Lindent to fix the horrible formatting and it reported unmatched else. You have too many else statements in your code.:
indent: foo.c:93: Error:Unmatched 'else'
indent: foo.c:139: Error:Unmatched 'else'
indent: foo.c:164: Error:Unexpected end of file
(Yes, indent(1) is not intended for C#, but it works surprisingly well on most C-like languages, and in this case has pointed out at least two else statements that aren't properly matched. And don't worry about those specific line numbers -- this routine needs to be properly formatting and broken apart into smaller pieces -- trying to just bodge this together with a few correctly-placed {} isn't the right answer.)
Break apart your gigantic main() routine into smaller functions. (Your code duplicates the Alaska / Hawaii question six times -- that should be a separate routine right there. Maybe all yes / no questions should be handled through one routine: pass in the question and it'll do the prompt and return true or false for you.)
I strongly recommend using braces on your else clauses if you used them on your if clause:
This is fine:
if (foo)
/* statement */
else
/* single statement */
But if you ever need to use braces on the first block, then use them on the else block too:
if (foo) {
/* stuff */
/* more stuff */
} else {
/* use those braces! */
/* you will find reading your code far easier if you do */
}
Good luck.
You've got two problems here. The first is a couple of braces that shouldn't be there. i.e. these:
catagory = char.Parse(Console.ReadLine());
{ // <------- THIS ONE
if (catagory == 'A')
//
// skipped code
//
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
} /* <------- THIS ONE */ surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
The second problem (the one that is causing your control cannot fall through error) is the fact that the break statement for the Numbers.same case is inside a conditioned block of statements (in the else part of your if (catagory == 'A')). This means that it will be executed only when category is different from 'A', and not for every value of your variable. You just have to move the break after the brace.

Categories