Calculator Errors
Mar 30, 2014 at 11:07am UTC
I'm trying to make a small calculator.
First I have to choose + or - and then I will enter the numbers...
When I select - using number 2 it wont go directly to - , it goes to + first.
(sais illegal else)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
int main()
{
int stop;
int n1;
int n2;
int subadd;
int n3;
int n4;
std::cout << "Enter 1 for addition or 2 for substraction" << std::endl;
std::cin >> subadd;
if (subadd==1)
std::cout << "Please enter the first number" << std::endl;
std::cin >> n1;
std::cout << "Please enter the second number" << std::endl;
std::cin >> n2;
std::cout << n1 << " + " << n2 << " = " << n1+n2 << std::endl;
else
std::cout << "Please enter the first number" << std::endl;
std::cin >> n3;
std::cout << "Please enter the second number" << std::endl;
std::cin >> n4;
std::cout << n3 << " - " << n4 << " = " << n3-n4 << std::endl;
std::cin >> stop;
}
Last edited on Mar 30, 2014 at 11:08am UTC
Mar 30, 2014 at 11:20am UTC
You need to put braces around the contents of your if statements, otherwise they only go for one line. Do it like this:
1 2 3 4 5 6 7 8 9 10
// ...
if (subadd == 1) {
std::cout << "..." << std::endl;
// ...
} else {
std::cout << "..." << std::endl;
// ...
}
std::cin >> stop;
Topic archived. No new replies allowed.