Okay so I'm new. I need to make a simple calculator, that asks the user to choose and operator, then do the math. So I thought this code would work, but it won't build because == doesn't work...? I don't understand the comments on the bottom that well. Your help is really appreciated.
Your code didn't compile because of lines 82 and 83, they were not in the scope of the main() function.
The if statement that checks for a valid operator on line 16 of your code was wrong, there really is no need for it, it's better to have an else statement at the bottom.
When comparing one variable to a set of constants prefer using a switch statement in stead of ifelseifelse statements.
Lines 15 and 79 are obsolete, you do not need a nested scope.
okay update-- got it to work. The code compiles successfully and debugs. But when I was testing it-
it showed the first cout, asking the op. I put in +. It asked for the two numbers. I typed in 10
12
and then the app just exited itself.
#include <iostream>
usingnamespace std;
int main()
{
int number1, number2;
char op;
cout << "Which arithmetic operator would "
<< " you like to use?" << endl;
cin >> op;
{
if (op == '+')
{
cout << "\nPlease enter the two operands "
<< " for addition " << endl;
cin >> number1;
cin >> number2;
cout << "\nThe sum of " << number1 << " and "
<< number2 << " is " << number1 + number2;
}
elseif (op == '-')
{
cout << "\nPlease enter the minuend" << endl;
cin >> number1;
cout << "\nPlease enter the subtrahend" << endl;
cin >> number2;
cout << "\nThe difference of " << number1 << " and "
<< number2 << " is " << number1 - number2;
}
elseif (op == '*')
{
cout << "\nPlease enter the first factor" << endl;
cin >> number1;
cout << "\nPlease enter the second factor" << endl;
cin >> number2;
cout << "\nThe product of " << number1 << " and "
<< number2 << " is " << number1 * number2;
}
elseif (op == '/')
{
cout << "\nPlease enter the dividend" << endl;
cin >> number1;
cout << "\nPlease enter the divisor" << endl;
cin >> number2;
{
if (number2 == 0)
cout << "\nThat is an invalid divisor";
else
cout << "\nThe quotient of " << number1
<< " and " << number2 << " is "
<< number1 / number2;
}
}
elseif (op == '%')
{
cout << "\nPlease enter the dividend" << endl;
cin >> number1;
cout << "\nPlease enter the divisor" << endl;
cin >> number2;
{
if (number2 == 0)
cout << "\nThat is an invalid divisor";
else
cout << endl << number1 << " modulus "
<< number2 << " is " << number1 % number2;
}
}
else
{
cout << "\nInvalid operator. Please choose (+,-,*,/,%)"
<< endl;
}
}
return 0;
}
also- about the comments. Sorry I wasn't clear earlier-- they are the ones at the bottom of microsoft visual studios. But I worked that bit out now. Fixed one thing and messed up the other.
But after the app exits the new comments say
Cannot find or open the PDB file.
The program '[6656] asgmt2.1.exe' has exited with code 0 (0x0).
That's just a normal message from Visual Studio indicating it can't open the debugging infomation file for one of the Windows DLLs. .pdb files don't come with Visual Studio. You need the Windows SDK if you want to debug inside Windows DLLs.