So I have to write a program that serves as a simple 4 function calculator. Looking up an example gave me the code I have below, but it won't build successfully and I have no idea where to go from here. Sorry if it's messy.
I reworked a bit while waiting and I got it to compile successfully, but now it won't give me the answer. I imagine there's something I screwed up towards the end.
Line 76 of your most recent post - that is an infinite loop (it is not related to a do statement).
Line 79 of your most recent post - that does not do what you expect. Instead of comparing for equality between OP and all those character literals, it simply assigns '/' to OP and results in an infinite do-while loop. Consider instead:
79 80 81 82 83 84 85 86
} while(OP == 'X'
|| OP == 'x'
|| OP == 'C'
|| OP == 'c'
|| OP == '+'
|| OP == '-'
|| OP == '*'
|| OP == '/');
int main()
{
// Declaring variables
s: // (LB) you could start a while loop here instead
char OP;
char Answer;
int Num1;
int Num2;
// Start loop2
cout << "Enter a number > " << endl;
cin >> Num1; // Read the first number
do
{ // Start loop1
p: // (LB) this is useless because of the 'continue' statement
cout << "OP> ";
cin >> OP; // Read an operator
cout << "Enter a second number > " << endl;
cin >> Num2;
switch (OP) //Is operator valid
{
case'X':
case'x':
do
{
cout << "Are you sure? [Y/N] ";
cin >> Answer;
if(toupper (Answer) == 'Y') // toupper says to treat Answer as if it were upper case
{
cout << "Have a nice day" << endl;
exit (0); // terminate the program right now (turn off the calculator
}
else; // (LB) this line doesn't do anything
} while (toupper(Answer) != 'N');
break;
case'C':
case'c':
do
{
cout << "Clearing Calculator." << endl;
goto s; // (LB) why is this in a loop? The loop is pointless with this line
} while (OP = 'C','c'); // (LB) this is an infinite loop (but the goto prevents it)
break;
case'+':
Answer=Num1+Num2;
break;
case'-':
Answer=Num1-Num2;
break;
case'*':
Answer=Num1*Num2;
break;
case'/':
Answer=Num1/Num2;
if (Num2 == 0) // (LB) this condition is too late, the program will have already crashed
cout << "Please enter another number. Cannot divide by 0." << endl;
goto p; // (LB) this goto is unconditional
break;
default:
cout << "You have entered an invalid operator." << endl; // (LB) you tell them this AFTER they enter the second number?
cout << "Please enter + - * / C or X" << endl;
goto p; // (LB) this could be replaced with a 'continue'
break;
} while (true); // (LB) this is an infinite loop no attached to a 'do'
} while (OP = 'X', 'x', 'C', 'c', '+', '-', '*', '/'); // (LB) this is an infinite loop
cout << "The result is " << Answer << endl;
}
Thanks for the corrections, but it seems like I'll have to start the whole thing from scratch. I used a sort of template that my professor did in class and I seem to be misunderstanding what he did in it. I applied your corrections and am still getting the same error as above.
I removed the infinite while towards the end, applied your correction of the last while statement, removed the goto's, moved the second number input prompt below the switch, etc. I don't think do while loops are working out for me very well.
I wouldn't want you to write it for me, so I appreciate the help up to this point.
int main()
{
// Declaring variables
// (LB) you could start a while loop here instead
char OP;
char Answer;
int Num1;
int Num2;
// Start loop2
cout << "Enter a number > " << endl;
cin >> Num1; // Read the first number
do
{ // Start loop1
cout << "OP> ";
cin >> OP; // Read an operator
switch (OP) //Is operator valid
{
case'X':
case'x':
do
{
cout << "Are you sure? [Y/N] ";
cin >> Answer;
if(toupper (Answer) == 'Y') // toupper says to treat Answer as if it were upper case
{
cout << "Have a nice day" << endl;
exit (0); // terminate the program right now (turn off the calculator
}
} while (toupper(Answer) != 'N');
break;
case'C':
case'c':
do
{
cout << "Clearing Calculator." << endl;
} while (OP == 'C'
||OP == 'c');
break;
case'+':
Answer=Num1+Num2;
break;
case'-':
Answer=Num1-Num2;
break;
case'*':
Answer=Num1*Num2;
break;
case'/':
Answer=Num1/Num2;
if (Num2 == 0) // (LB) this condition is too late, the program will have already crashed
cout << "Please enter another number. Cannot divide by 0." << endl;
break;
default:
cout << "You have entered an invalid operator." << endl; // (LB) you tell them this AFTER they enter the second number?
cout << "Please enter + - * / C or X" << endl;
continue; // (LB) this could be replaced with a 'continue'
break;
} while(OP == 'X'
|| OP == 'x'
|| OP == 'C'
|| OP == 'c'
|| OP == '+'
|| OP == '-'
|| OP == '*'
|| OP == '/');
cout << "Enter a second number > " << endl;
cin >> Num2;
cout << "The result is " << Answer << endl;
}
}