I am trying to create a simple menu where option 1, squares an integer, option 2 finds the factorial, option 3 gives you a random number and option 9 exits the program. So far all of it works except for two problems:
1) I get the same random number every time I use it and
2) whenever I type in an integer to find the factorial (after choosing option two)the programs just ends after I hit enter. Please help me! I cannot figure out what I am doing wrong. Thanks a lot, much appreciated. Here is what I have:
P.S. I am sorry about the code layout I am new to this site.
rand does not produce a random number. It produces numbers in a sequence. You get to set the start of the sequence using srand. If you feed the same value into srand each time you use it, you'll always get the same sequence.
You are always putting 0 into srand, so you will always get the same sequence.
1) I get the same random number every time I use it and
You're not calling rand() in you case statement, you just called it once at the top of your program. Try this:
1 2 3 4
case 3:
cout << " Random number: " << rand();
cout << "\n";
break;
2) whenever I type in an integer to find the factorial (after choosing option two)the programs just ends after I hit enter. Please help me! I cannot figure out what I am doing wrong. Thanks a lot, much appreciated.
Remove the return statement and print product.
1 2 3 4 5 6 7 8 9 10 11 12 13
case 2:
{
cout << "Please enter an integer number:\n";
cin >> integer;
int product = 1;
while (integer > 0)
{
product = integer * product;
integer--;
}
cout << "Factorial is " << product;
}
break;