So I'm making a calculator and I've got it done so far but I'm having trouble making the code loop back through so the user can type new numbers to Add, Subtract, Divide, Multiply. Simple stuff so far because I'm new to C# but anyways here's what I have so far.
I keep getting two errors saying "Operator '||' cannot be applied to operands of type 'bool' and 'string' "
if (userinput == "+")
{
Console.Clear();
Console.WriteLine("Enter The First Number You Would Like To Add: ");
Add1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Number You Would Like To Add: ");
Add2 = Int32.Parse(Console.ReadLine());
Console.WriteLine("The Answer Is: {0} ", Add1 + Add2);
while (true)
{
Console.WriteLine("Would You Like To Do Another Calculation? (Y) Yes, (N) No.");
Console.ReadLine();
if (userinput == "y" || "Y") // This if and else if is where the errors are.
{
continue;
}
else if (userinput == "n" || "N")
{
break;
}
}
}
|
Suthish NairPosted Feb 7, 2011, 7:44 AM
I suggest download this book form Mahesh Chand, this covering almost all basics.
Free Book: C# Programming for Beginners
Sam HobbsPosted Feb 7, 2011, 1:14 AM
A switch statement has an expression and then case clauses; the case clauses are enclosed in curly braces ("{}"). There are multiple case clauses followed by an optional default clause. Each case clause must end with a "break"; see my sample code. The reason why "break" is necessary is because C# is so close to C++ but C# improved the switch statement so that it is a little easier to use and it won't allow programming mistakes to happen that could be difficult to diagnose during execution.
FrankPosted Feb 7, 2011, 12:20 AM
Anyhow I'm getting this error from the swtich statement.
"Control cannot fall through from one case label ('case "+":') to another"
The same goes for the other operators.
Sam HobbsPosted Feb 6, 2011, 8:31 PM
Sam HobbsPosted Feb 6, 2011, 7:34 PM
FrankPosted Feb 6, 2011, 2:50 PM
private static void Addition()
{
int Add1 = 0;
int Add2 = 0;
string userinput = "";
if (userinput == "+")
{
Console.Clear();
Console.WriteLine("Enter The First Number You Would Like To Add: ");
Add1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Number You Would Like To Add: ");
Add2 = Int32.Parse(Console.ReadLine());
Console.WriteLine("The Answer Is: {0} ", Add1 + Add2);
}
}
And then add a while loop inside the Main() function so it will loop through?
Oh and also now once I put all of the (Addition, Subtraction, Division, Multiplaction) into a seperate function it wouldn't continue with the program. It would just end once the user chooses an operator. Do you want to see the full code or is that not needed?
It's not a class assignment anymore but I just thought to myself that I wanted to add on to the one we made in class and make it a little better. So I'm learning in class as well as at home so I can learn more I guess you could say.
Sam HobbsPosted Feb 5, 2011, 10:40 PM
What I would do is to put most of the programming for a calculation in a separate function. Then I might execute that function once, then do a loop. The loop would be like your loop where you ask if they want to do another. If they answer no, then the loop would end. If they asnwer yes, then it would do the other function again. The function could even return a boolean value indicating whether to continiue. So your Main function just needs to have in it "while (Calculate());". Can you figure that out?