Problems with nested if else loop

Hi, I'm a newbie learning C++ on my own
So far I've traveled to this if else nested loop topic
I've even created a program on my own
But it seems to have an error in it
It is executable, but it does not run as I intended it to


Here's the code:

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
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
using namespace std;

int main()
{
	cout << "Welcome to Math Solving Program" << endl;
	cout << "Please choose your category:(1,2,3,4)" << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;

	int input, a, b, total;

	cin >> input;

		if (input = 1)
		{
			cout << "Please enter your value A" << endl;
			cin >> a;
			cout << "Please enter your value B" << endl;
			cin >> b;

			total = a + b;

			cout << "The answer of " << a << " add " << b << " is equal to " << total << endl;
		}
		else if (input = 2)
		{
			cout << "Please enter your value A" << endl;
			cin >> a;
			cout << "Please enter your value B" << endl;
			cin >> b;

			total = a - b;

			cout << "The answer of " << a << " subtract " << b << " is equal to " << total << endl;
		}
		else if (input = 3)
		{
			cout << "Please enter your value A" << endl;
			cin >> a;
			cout << "Please enter your value B" << endl;
			cin >> b;

			total = a * b;

			cout << "The answer of " << a << " multiply " << b << " is equal to " << total << endl;
		}
		else if (input = 4)
		{
			cout << "Please enter your value A" << endl;
			cin >> a;
			cout << "Please enter your value B" << endl;
			cin >> b;

			total = a / b;

			cout << "The answer of " << a << " divide " << b << " is equal to " << total << endl;
		}
		else if (input != 1 && 2 && 3 && 4)
		{
			cout << "Please enter a valid selection" << endl;
		}
	system("pause");
}


The output of the program will only be the result from the first if statement
Anyone care to enlighten me? Thanks!
Last edited on
(input = 1) should be (input == 1)

Likewise elsewhere.

else if (input != 1 && 2 && 3 && 4) should be else if (input != 1 && input !=2 && input !=3 && input !=4)
Last edited on
1) Please use code tags, when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2)
But it seems to have an error in it
It is executable, but it does not run as I intended it to


That is not a very helpful description of your problem. In what way is it not working as intended? What behaviour are you seeing? How does that differ from intended behaviour?

3) Some things I've noticed:

- you seem to be confusing the equality comparison operator == with the assignment operator =

- you're doing integer division, so the results you get will almost certainly not be the one you're expecting
Last edited on
>Repeater
Ohh! Thanks for pointing out the problem!
I'll try again, thanks!

[update]
Thanks for the tips, tried this one just now, it works
else if (input != 1 && 2 && 3 && 4)

---line---

>MikeyBoy
Sorry bout that, but the rest of the question was at the end of the code
Am a newbie, so I'm not so sure how the code tags function works
Sorry for the inconvenience, and yes, I've added it into code tags, thanks for the reminder

[update]
Thanks, I've just noticed that silly mistake too
Now it seems to be working fine

---line---

Thanks for the tips
Now I'll just try to make it go back to the main page every time after it executes
which probably takes another few more hours , lol
Last edited on
Thanks for editing and putting your code into tags - it looks much better now, as I'm sure you can see :)
Topic archived. No new replies allowed.