#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int c;
int result;
int d;
int e;
int f;
int g;
int input;
cout << "Please enter a number";
cout << " ";
cin >> e;
cout << "This is number one";
cout << ". ";
cout << "Enter another number";
cout << ", ";
cin >> f;
cout << "Enter one final number";
cout << ", ";
cin >> g;
cout << "Do you want to add, subtract, multiply, or divide these numbers?";
cout << " ";
cout << "Press a for add";
cout << ", ";
cout << "Press b for subtractcing";
cout << ", ";
cout << "Press c for multiplying";
cout << ", ";
cout << "Press d for dividing";
cout << ", ";
{
if (cin, input == a)
cout << a + b + c;
cout << " ";
}
else
{if (cin, input == b)
cout << a - b - c = result;
cout << " ";}
else
{if (cin, input == c)
cout << a * b * c = result;
cout << " ";}
else
{if (cin, input == d)
cout << a / b / c = result;
}
system("pause");
return 0;
}
I get the errors
|40|error: expected primary-expression before "else"|
|40|error: expected `;' before "else"|
|44|error: expected primary-expression before "else"|
|44|error: expected `;' before "else"|
|48|error: expected primary-expression before "else"|
|48|error: expected `;' before "else"|
How do I fix these errors? Thank you for your time.
#include <iostream>
usingnamespace std;
int main()
{
int a, b, c, d, e, f, g, input, result;
cout << "Please enter a number ";
cin >> e;
cout << "This is number one. ";
cout << "Enter another number, ";
cin >> f;
cout << "Enter one final number, ";
cin >> g;
cout << "Do you want to add, subtract, multiply, or divide these numbers? ";
cout << "Press a for add, ";
cout << "Press b for subtractcing, ";
cout << "Press c for multiplying, ";
cout << "Press d for dividing, ";
cin >> input;
if (input == a)
{
cout << a + b + c;
cout << " ";
}
elseif (input == b)
{
result = a-b-c;
cout << result << " ";
}
elseif (input == c)
{
result = a*b*c;
cout << result << " ";
}
elseif (input == d)
{
result = a/b/c;
cout << result;
}
system("pause");
return 0;
}
There are still lots of errors here, but I'm assuming that you want to work those out your-self.
1st do not use cin, input.. its better to use cin>>input;
2nd your use of bracets is not valid. You use bracets in this way
if(........)
{
}
else
{
}
3nd you have logical errors.. when you say input==a, the compiler doesn't understand that you are comparing your variable input to the letter"a" ..It takes the ASCII value of a which is a number and compares it with your variable input. to compare your input with the letter a you have to put the a in ' ' like this 'a' . Also, your input should be declared of type char( which is charecter) not int.
4th, do not use result for all the cases of addition, subtraction and multiplication. Suppose for example you need the result for addition later in your program, if you type result the compiler will take the last assigned value of reslut which is in this case the division.
Your code should look something like this:
#include <iostream>
using namespace std;
int main()
{
char a;
char b;
char c;
int result;
int d;
int e;
int f;
int g;
char input;
cout << "Please enter a number";
cout << " ";
cin >> e;
cout << "This is number one";
cout << ". ";
cout << "Enter another number";
cout << ", ";
cin >> f;
cout << "Enter one final number";
cout << ", ";
cin >> g;
cout << "Do you want to add, subtract, multiply, or divide these numbers?";
cout << " ";
cout << "Press a for add";
cout << ", ";
cout << "Press b for subtractcing";
cout << ", ";
cout << "Press c for multiplying";
cout << ", ";
cout << "Press d for dividing";
cout << ", ";
cin>>input;
if (input == 'a')
{
cout << e+f+g;
cout << " ";
}
else if (input == 'b')
{cout << e - f - g;
cout << " ";}
else if (input == 'c')
{cout << e * f * g ;
cout << " ";}
else if(input == 'd')
cout << e / f / g ;