Help with factorial....not what you think

Dec 10, 2014 at 12:17pm
Below is my code. Its not the entire code but i want to concentrate on my factorial. I am a beginner and while the variables are not well organised i have a bit of cleaning up to do.
The problem is

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
76
77
78
79
80
81
82
#include <cstdlib>
#include <iostream>
#include <cmath> // For math functions. eg SIN, COS, TAN.
#define PI 3.14159 // Define the word PI and assign its value.


using namespace std;


int main()
{
   
    double Angle, Opposite, a, e, area, radius, circumference, result, radians; // Variables for cirumference, area and hypotenuse.
    int choice;
    int n, count; // Variables for factorial.
    int factorial=1;
    
    
  
    
    
    do // This is the menu. While-loop repeats statement while the expression is true.
    {
           
    cout << "WHICH CALCULATION DO YOU REQUIRE?\n\n"; // \n for a new line.
    
    cout << "Option 1 to calculate the circumference of a circle.\n";
    cout << "Option 2 to calculate the area of a circle.\n";
    cout << "Option 3 to calculate the hypotenuse of a triangle.\n";
    cout << "Option 4 to calculate the factorial of a number.\n";
    cout << "Option 5 to EXIT the program!\n\n"; // \n\n for two spaces for a new line.
    cout << " Choice:  ";
    cin >> choice;
    
    while ((choice < 1) || (choice > 5))
		{
			cout << "Incorrect choice. Please choose between 1 and 5. ";
			cin >> choice;
		}
		
		if (choice == 5)// Loads option 5.
                                { system ("cls");
                                  cout << "Terminating Program!\n";
                                  system ("pause");
                                  return (0);// Exit the program.
                                  }
    
    else if (choice == 1) // If this condition is true, the statement is executed. If choice is equal to 1.
    
                                {  //selection 1 code
                                   }
    else if (choice == 2)// Loads option 2.
                                {  selection 2 code
                                   }
    else if (choice == 3)// Loads option 3.
                                {  selection 3 code
                                   }
    else if (choice == 4)// Loads option 4.
                                {  system ("cls");
                                   cout << "Here you can calculate the factorial of a number between 1 and 30.\n";
                                   system ("pause");
                                   cout << "Please enter a number between 1 and 30:  \n";// Limit the user so it doesnt bug out with a high number.
                                   cin>>n; 
                                
                                   for( count = n; count > 1; count--)// Counts backwards, so the factorial of 5 = 5x4x3x2x1, which is 120.
                                   factorial = (factorial * count);
                                   cout << "The factorial equals:  " << factorial <<endl;
                                   system ("pause");
                                   
                                   system ("cls");
                                   }
    
 
    
                                  
}while (1);// End of loop. (1)
  
   
}// End of main.




My entire program works. So after every option it returns to the menu for another selection however if i select factorial and put in 5 it equals 120 which is right but if i select the factorial again and input 5 it gives me a much bigger number so its like its storing the last result and including that if youn get what i mean,
I can not solve this.
Dec 10, 2014 at 12:26pm
Line 16 makes factorial in-scope for all of int main(), and you never reinitialize it to 1; you should do that around line 64-65.
Last edited on Dec 10, 2014 at 12:27pm
Dec 10, 2014 at 12:27pm
add:
factorial = 1;
on line 69?

or move line 16 to line 24?
Last edited on Dec 10, 2014 at 12:28pm
Dec 10, 2014 at 12:32pm
Spot on thankyou. It now works. Could somebody give me an easy explanation as to why it was doing that before.
Dec 10, 2014 at 12:39pm
you said it yourself dude:
so its like its storing the last result
Dec 10, 2014 at 12:54pm
cool......just came across another problem......if i input a letter instead of a number as a selection it just spams an infinite "invalid choice" prompt.
Last edited on Dec 10, 2014 at 1:15pm
Dec 10, 2014 at 2:10pm
Dec 10, 2014 at 3:19pm
Thanks.
Topic archived. No new replies allowed.