if/else help

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace 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.
Last edited on
The brackets { should go below or on the right of the if statement, not to the left.

read this for more info:
http://cplusplus.com/doc/tutorial/control/

Learning to format will save your life several times over as this will help you to see bad control structures. I believe this is what you meant to do:
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
37
38
39
40
41
42
43
#include <iostream>
using namespace 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 << "  ";
	}
	else if (input == b)
	{
		result = a-b-c;
		cout << result << "  ";
	}
	else if (input == c)
	{
		result = a*b*c;
		cout << result << "  ";
	}
	else if (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.
Last edited on
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 ;

return 0;
}
Thanks for all the help! It works perfectly and thank you for the references and tip!
Topic archived. No new replies allowed.