HELP WITH RAND and FACTORIALS PLEASE

Jan 19, 2012 at 11:20pm
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);
}
Jan 19, 2012 at 11:34pm
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.
Last edited on Jan 19, 2012 at 11:34pm
Jan 19, 2012 at 11:38pm
return product;

That statement is gonna kick you out of the program. The cout is never going to execute.
Jan 19, 2012 at 11:41pm
closed account (ENb7ko23)
Thanks a lot for the help guys
Jan 19, 2012 at 11:42pm
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;


Last edited on Jan 19, 2012 at 11:49pm
Topic archived. No new replies allowed.