While Loop Confusion (help)

Nov 7, 2013 at 3:21pm
I'm trying to do a calculator, I have done the functions for the operators(+ , - , / , *) ..

My question lies in my main() function in the while loop.
AT the end if I enter a wrong option, for example '6', then it asks me to eneter again but if I enter a wrong option again, it just exits the loop.



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
  int main()
{
	//cout << "Hello World";
	//cin.get();

	int a, b, option;
	cout << "Please enter two number: ";
	cin >> a;
	cin >> b;

	cout << "To add press 1 \nTo subtract press 2\nTo divide press 3\nTo multiply press 4: \n";
	cin >> option;
	cout << endl << endl;

	while (option != 1 || option != 2 || option != 3 || option != 4)
	{
		switch (option)
		{
		case 1:
			cout << "The answer is: " << sum(a, b) << endl;
			break;
		case 2:
			cout << "The answer is: " << sub(a, b) << endl;
			break;
		case 3:
			cout << "The answer is: " << divide(a, b) << endl;
			break;
		case 4:
			cout << "The answer is: " << mult(a, b) << endl;
			break;

		default:
			cout << "Incorrect Value, Please try again: ";
			cin >> option;


		}
		if (option >= 1 || option <= 4)
		{
			break;
		}
	}
	system("Pause");
}
Nov 7, 2013 at 3:39pm
if (option >= 1 || option <= 4)

|| means "OR", so you're asking if option is bigger/equal to 1 OR lesser/equal to 4. 6 is bigger or equal than 1, so it calls the break. note that every single number in the universe is included in this statement (anything bigger than 1 and lower than 4)

i think you wanted if (option >= 1 && option <= 4)
which means "if number is bigger/equal to 1 AND lesser/equal to 4".
Last edited on Nov 7, 2013 at 3:40pm
Nov 7, 2013 at 4:06pm
That fixed it, but now at this point that while loop is all messed up.

The "if" statement exits the loop if the option is correct and doesn't calculate the data.

I think I got to replace the while statement with a do, while

advise?
Nov 7, 2013 at 5:07pm
sure. put the cin >> at the loop start:

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(){
	//cout << "Hello World";
	//cin.get();

	int a, b, option;
	cout << "Please enter two number: ";
	cin >> a;
	cin >> b;

	cout << "To add press 1 \nTo subtract press 2\nTo divide press 3\nTo multiply press 4: \n";
	//cin >> option;
	//cout << endl << endl;

	while (option != 1 || option != 2 || option != 3 || option != 4){
                cin >> option;
		switch (option){
		case 1:
			cout << "The answer is: " << a+b << endl;
			break;
		case 2:
			cout << "The answer is: " << a-b << endl;
			break;
		case 3:
			cout << "The answer is: " << a/b << endl;
			break;
		case 4:
			cout << "The answer is: " << a*b << endl;
			break;

		default:
			cout << "Incorrect Value, Please try again: ";
			//cin >> option;
			break;
		}
		if (option >= 1 && option <= 4){
			break;
		}
	}
}


so every time the loop runs, it asks for the input.


hint: use floats instead of ints, or you will not get correct results when dividing numbers.
hint2: do not use system("pause"). i know that you probably just wanted to test and study and stuff, but it's a bad habit. search around for alternatives.
Last edited on Nov 7, 2013 at 5:11pm
Nov 7, 2013 at 5:24pm
Thank for the reply! I fixed it by doing it like this:

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
	while (option != 1 && option != 2 && option != 3 && option != 4)
	{
		cout << "Please enter correct option: ";
		cin >> option;
	}


		switch (option)
		{
		case 1:
			cout << "The answer is: " << sum(a, b) << endl;
			break;
		case 2:
			cout << "The answer is: " << sub(a, b) << endl;
			break;
		case 3:
			cout << "The answer is: " << divide(a, b) << endl;
			break;
		case 4:
			cout << "The answer is: " << mult(a, b) << endl;
			break;

		default:
			cout << "Incorrect Value, Please try again: ";
			cin >> option;

		}



Thanks a lot guys ! :-)
Topic archived. No new replies allowed.