how to use the do while loop to only accept a valid number and go back to the original question if not a valid number

I was given an assignment that asked this "Write a program that accepts a positive integer between 2 to 15, and then using the number, calculate its factorial (n!). Using do-while statement, make sure it only accepts a valid number (between 2 and 15) – if an invalid number is entered, program should display the original instruction again.
Hint: all variables should be integer
Hint: After do-while loop for input, use for-loop to get the factorial

*** Sample output

Enter a number between 2 and 30: 15
Factorial of 15 is 2004310016."

Well I got most of the code but all i need to figure out is how to use the do-while statement, and make sure it only accepts a valid number (between 2 and 15) – if an invalid number is entered, program should display the original instruction again. Could some one help me please. I'm a noob to this coding and Im struggling . This is my code :

#include <iostream>
using namespace std;
int main () {

int number;
int factor = 1;

do
{ cout << "Enter a number between 2 and 30 : " << endl;
cin >> number ;

for (int i=1; i<= number; i++)
factor *= i; cout << "Factorial of " << number << " is " << factor << endl;
}
while (number =<2 && number => 30);

system ("Pause");

return 0;

}
Last edited on
Break the problem down into 3 chunks.

1) gather input
2) do the calculation
3) display the results

Also, =< is not valid in c++. <= is.
Topic archived. No new replies allowed.