I am making a basic calculator to help me laern the basics of C++. However, I get the message
1 2 3 4 5
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(26): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(32): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(38): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(44): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(47): error C2181: illegal else without matching if
int main()
{
usingnamespace std;
bool loop;
loop = true;
while(loop)
{
float x;
float y;
cout << "Welcome to the calculator!";
cout << "Enter 1 to add, \n2 to subtract, \n3 to multiply,\n4 to divide, \nand 0 to quit.";
int choice;
cin >> choice;
if(choice == 1)
cout << "Enter the first number";
cin >> x;
cout << "Enter the second number";
cin >> y;
cout << add(x, y);
elseif(choice == 2)
cout << "Enter the number to be subtracted from";
cin >> x;
cout << "Enter the number that will be removed from" << x;
cin >> y;
cout << subtract(x, y);
elseif(choice == 3)
cout << "Enter the first number";
cin >> x;
cout << "Enter the second number";
cin >> y;
cout << multiply(x, y);
elseif (choice == 4)
cout << "Enter the number to be divided";
cin >> x;
cout << "Enter the number that " << x << " will be divided by";
cin >> y;
cout << divide(x, y);
elseif(choice == 0)
cout << "Goodbye";
loop = false;
else
cout << "That is not a valid choice";
}
}
I have other files but these work fine. I am not trying to make a GUI calculator (like the one in the "Accessories" section of the Start Menu in Windows", just a simple calculator with text input. I a using Visual C++ 2010 Express for my Compiler/IDE.
Eventhough other people have already helped you, you could also make a switch case, witch would make it so much easier (in my opinion)...
Making a switch case would create this instead of all the if statements:
switch(choice)
{
case 1:
cout << "Enter the first number";
cin >> x;
cout << "Enter the second number";
cin >> y;
cout << add(x, y);
break;
case 2:
cout << "Enter the number to be subtracted from";
cin >> x;
cout << "Enter the number that will be removed from" << x;
cin >> y;
cout << subtract(x, y);
break;
case 3:
cout << "Enter the first number";
cin >> x;
cout << "Enter the second number";
cin >> y;
cout << multiply(x, y);
break;
case 4:
cout << "Enter the number to be divided";
cin >> x;
cout << "Enter the number that " << x << " will be divided by";
cin >> y;
cout << divide(x, y);
break;
default:
cout << "That is not a valid choice";
}
I haven't put in the "goodbye" part, but you probably get the point.
By the way, what library are you using for the divide(x, y), multiply(x, y) etc?
I'd guess the #include <cmath> but i'm not sure. Knowing would make stuff a lot better :)