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.
case 3:
r = rand(); // <--- Need to call every time to get a new number saved in variable r.
cout << " Random number: " << r;
cout << "\n";
break;
2) The return product on line 56 should be removed, that is why it is exiting. Also, you want to print out product not integer on line 59.
cout << "Factorial is " << product << endl;
Notes: You might want to make product a double, 13! is larger than a 32-bit signed or unsigned number. Same thing for the result of the power function (make double).
I forgot you have to move the scope to include the cout of the product.
Don't make it a double. It's a bad practice, and practicing it will turn into a bad habit. You are doing integer arithmetic, double loses precision. Make it a unsignedlonglong, if you need to go higher then that, you will need to make an integer class that uses an arbitrary amount of bits. longlong is guaranteed to be at least 64 bits
True, but at 21! you have gone beyond the size of a 64-bit number. Just wanted to highlight how fast factorial grows.
Check me, but given the format of a double 11 bits exp, 1 bit sign, 52 bits mantissa would give you the same answer as an integer with no lost error with integer up to 52 bits. And if your calculation when over 52 bits you would then get an answer that would start to have non-integer answer with errors.
With the double you wouldn't have total overflow until 1.7976931348623157 x 10^308