I have been banging my head against the wall with this problem since yesterday. I have tried everything. The exercise is:
Write a program that lets a user enter N and that outputs N! (meaning N*(N-1)*(N-2)*...*2*1). Hint: Initialize a variable totalValue to N, and use a loop variable i that counts from N-1 down to 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main() {
int totalVal = 0;
int userInt = 0;
// FIXME: Ask user to input an integer, store in userInt
totalVal = userInt;
// FIXME: Add while loop that counts down to 1, updating totalVal
cout << userInt <<"! is " << totalVal << endl;
return 0;
}
Why would you not know that userInt is N? Didn't you write the code?
Anyway, my second post on that thread told you to change while (userInt >= 1) { to while (userInt > 1) {. That would give you the output
1! is X
Where X is whatever the computed factorial is going to be. In the first post on that thread, I told you that you were going to need a third variable in order to get the correct output (so it doesn't always say "1!").
Notes:
int will only allow from 0! -> 12!
you will need to do error checking because you can't have negative factorial or overflow ( I didn't do either because those can easily be done with an if statement and are self explanatory)
Feel free to post your solution in case someone else has the same issue in the future. Also, there is no need to feel stupid we all had to start from somewhere.