HELP WITH RAND and FACTORIALS PLEASE

closed account (ENb7ko23)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
int choice;
int i,r;
int integer;
int result;
int factorial(int n);

srand (time(0));
r = rand();

int factorial(int integer);

   do
   {
        cout << "Please select the function desired and press Enter: ";
        cout << "\n\n";
        cout << "       1) Square the number provided.\n";
        cout << "       2) Factorial the number provided.\n";
        cout << "       3) I just need a random number.\n";
        cout << "\n";
        cout << "       9) Exit program, please.\n";
        cout << "Your selection:\n";
        cin  >> choice;
        cout << "\n";

        switch (choice)
        {

                case 1:
                        cout << "Please enter an integer number:\n";
                        cin  >> integer;
                        result = pow(integer,2);
                        cout << integer << " squared is " << result;
                        cout << "\n";

                        break;
                case 2:
                        cout << "Please enter an integer number:\n";
                        cin  >> integer;

   {
                                int product = 1;
                                while (integer > 0)
                                {
                                        product = integer * product;
                                        integer--;
                                }

                                return product;
                        }

                        cout << "Factorial is " << integer;
                        break;
                case 3:
                        cout << " Random number: " << r;
                        cout << "\n";
                        break;
         case 9:
                        cout << " End of Program.\n";
                        break;
                default:
                        cout << "Not a valid choice.\n";
                        cout << "Choose again.\n";
        }
   }while (choice != 9);

return(0);
}
1) You need to call r = rand() in the case 3:
1
2
3
4
5
                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.
Last edited on
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 unsigned long long, if you need to go higher then that, you will need to make an integer class that uses an arbitrary amount of bits. long long 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
Last edited on
Topic archived. No new replies allowed.