I am new to programming and am attempting to learn c#. I am having what i think is a simple problem with my console application(the code is below). The error appears to be with my if statement which reads as follows:
//evaluate subtotals to results
if (subTotalOne == subTotalTwo)
{
Console.WriteLine("=");
}
else if (subTotalOne < subTotalTwo)
{
Console.WriteLine("<");
}
else (subTotalOne > subTotalTwo);
{
Console.WriteLine(">");
}
The error i get is: Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
Any help would be appreciated. I have read through the forums here and seen may similar questions but my understanding isn't good enough yet to map the solutions i've seen to my problem.
Full application code:
using System;
namespace itc110_a02_GroceryComparison { class Program { static void Main(string[] args) {
Console.WriteLine("Compare Grocery Stores "); //Alert user to purpose of program with title
Console.WriteLine("\n");//line break
// Store 1
Console.WriteLine("Enter the name of the first store for comparison: ");//storeName 1
String storeOne = Console.ReadLine();//ToLower for eval
//store 1, first item
Console.WriteLine("Name of First product purchased at " + storeOne + ": ");//ask item
String purchaseOne = Console.ReadLine();//collect item
Console.WriteLine("Price paid for first purchased at " + storeOne + ": ");//ask 1st price
Double price1A = Double.Parse(Console.ReadLine());//collect 1st price
//store 1, second item, repeat process -- this ought to be a method or a function
Console.WriteLine("Name of second product purchased at " + storeOne + ": ");//ask item
String purchaseTwo = Console.ReadLine();//collect Item
Console.WriteLine("Price paid for second purchased at " + storeOne + ": ");//Ask Item Price
Double price1B = Double.Parse(Console.ReadLine());//Collect Item Price
Console.WriteLine("\n");
// Store 2, repeat process -- this ought to be a method or a function
Console.WriteLine("Enter the name of the second store for comparison: ");//Store name 1
String storeTwo = Console.ReadLine();// To Evals entry, we ToLower to set to lower case
//store 2
Console.WriteLine("Price paid for " + purchaseOne + " at " + storeTwo + ": ");//ask 1st price
Double price2A = Double.Parse(Console.ReadLine());//collect 1st price
//store 2, second item
Console.WriteLine("Price paid for " + purchaseTwo + " at " + storeTwo + ": ");//Ask Item Price
Double price2B = Double.Parse(Console.ReadLine());//Collect Item Price
Console.WriteLine("\n");
// Results go here
//Store one totals
Console.WriteLine("************ " + storeOne + " ************");
Console.WriteLine(purchaseOne + ": $" + price1A);
Console.WriteLine(purchaseTwo + ": $" + price1B);
Console.WriteLine("\n \n");
// store two totals
Console.WriteLine("************ " + storeTwo + " ************");
// Result A: Where to shop
Console.WriteLine(purchaseOne + ": $" + price2A);
Console.WriteLine(purchaseTwo + ": $" + price2B);
Console.WriteLine("\n \n");
Console.WriteLine("************ After Price Comparison ************");
//merge subtotals
Double subTotalOne = (price1A + price1B);
Double subTotalTwo = (price2A + price2B);
//evaluate subtotals to results
if (subTotalOne == subTotalTwo)
{
Console.WriteLine("=");
}
else if (subTotalOne < subTotalTwo)
{
Console.WriteLine("<");
}
else (subTotalOne > subTotalTwo);
{
Console.WriteLine(">");
}
//keeps the console open
Console.Read();
}
}
}
This else
line is the problem.
else (subTotalOne > subTotalTwo);
{
Console.WriteLine(">");
}
Fixed:
else
{
Console.WriteLine(">");
}
Explanation:
Your code is equivalent to:
else
(subTotalOne > subTotalTwo);//else block
{
Console.WriteLine(">");
}
semicolon makes the condition expression as statement and so the error. However, the next console statement will also not be part of else block as the else block finishes after semicolon. So, even if the condition expression was a valid statement, ">" will always be printed which is not desired.