While loop conditions

Hi there,

I have been trying to add two conditions to a while loop - please see my code below, for a simple program which adds 2 numbers between 1 and 20.

How do I add the two conditions correctly - ie only repeat the loop when a and b are not in the range of 1-20? My code gives an incorrect result. Replacing a ' for the ; in the condition bracket doesn't work.

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
//Program which adds to numbers input by user

#include <iostream>
using namespace std;

int main()
{
	int a=0, b=0, result;

	cout << "Program which adds to numbers between 1 and 20 input by user.\n\n";
		
	while (a<1 || a>20; b<1 || b>20) 
	{
		cout << "Inpute first number: \na = ";
			cin >> a;
		cout << "Inpute second number: \nb = ";
			cin >> b;
	}
	
	result = a + b;

	cout << "Result is: " << result << endl;
	system ("pause");
	return 0;
}


Many thanks!
Last edited on
As in
1
2
3
while ((a<1 || a>20) && (b<1 || b>20))
or
while ((a<1 || a>20) || (b<1 || b>20))
Last edited on
Thanks for the quick and helpful reply!
No problem :)
Topic archived. No new replies allowed.