Need help combining while statements

I am taking my very first programming class and I hit a snag on my lab. The questions asks me to make a program to display the factorial of a number. That part seemed to go fine for me, but my professor also asks us to always validate our inputs. When I try to make two while statements it only considers the first one which is the input validation. I have tried a few different ways but I always get the same problem. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int number, factorial = 1, i = 1;
	cout << "Enter a positive integer: ";
	cin >> number;
	while (cin.fail() || (!(number < 0)))
	{
		cout << "Invalid Input. Try again.";
		cin.clear();
		cin.ignore(10, '\n');
		cin >> number;
	}
	while (i <= number)
	{
		factorial *= i;
		++i;

	}
	cout << "Factorial of " << number << " is " << factorial << "\n";
	return 0;
}
You have an error in your condition. Your input validation executes if input failed or input not less than 0. So it will take only negative numbers. Also your validation is unortodox.

Use:
1
2
3
4
5
6
7
8
9
//...
cout << "Enter a positive integer: ";
while (!(cin >> number) || number < 0) {
	cout << "Invalid Input. Try again.";
	cin.clear();
	cin.ignore(10, '\n');
}
while (i <= number) {
/...
Damn you for making it seem so easy! But seriously thanks for the help.
Topic archived. No new replies allowed.