Making user menu repeat until choose to quit

I know this is pretty novice but for the life of me I cannot get this program to repeat itself. Any help would be greatly appreciated

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
#include <iostream>

using namespace std;


int func1(int n)
{
    if (n == 0)
    return 1;
    return 5*func1(n-1) -7;
}

int func2(int n)
{
    if (n == 0)
    return 1;
    if (n == 1)
    return 0;
    return 3*func2(n-1) -2*func2(n-2);
}

int func3(int n)
{
    if (n == 2)
    return 1;
    if (n == 3)
    return 4;
    return func3(n-1) + 2*func3(n-2) - 3*n;
}

int main()

{
    int x;
    int num =0, ans = 0;
    
    cout<<"Please choose which function you would like: "<<endl;
    cout<<"1. X(n) = 5*X(n-1)-7, X(0)=1"<<endl;
    cout<<"2. F(n) = 3*F(n-1)-2*F(n-2), F(0)=1, F(1)=0"<<endl;
    cout<<"3. G(n) = G(n-1) + 2*G(n-2) - 3*n, G(2)=1, G(3)=4"<<endl;
    cout<<"4. Quit"<<endl;
    cin>>x;


if (x == 1)
{
cout<<"Enter a number: ";
cin>>num;
ans = func1(num);
cout<<ans<<endl;
}
else if (x == 2)
{
cout<<"Enter a number: ";
cin>>num;
ans = func2(num);
cout<<ans<<endl;
}
else if (x == 3)
{
cout<<"Enter a number: ";
cin>>num;
ans = func3(num);
cout<<ans<<endl;
}

if (x == 4)
{
cout<<"Good bye";
return 0;
}


return 0;
}



Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    while(true)
    {
        int x;
        int num =0, ans = 0;
        
        cout<<"Please choose which function you would like: "<<endl;
        cout<<"1. X(n) = 5*X(n-1)-7, X(0)=1"<<endl;
        cout<<"2. F(n) = 3*F(n-1)-2*F(n-2), F(0)=1, F(1)=0"<<endl;
        cout<<"3. G(n) = G(n-1) + 2*G(n-2) - 3*n, G(2)=1, G(3)=4"<<endl;
        cout<<"4. Quit"<<endl;
        cin>>x;
    
    
        if (x == 1)     {cout<<"Enter a number: "; cin>>num; ans = func1(num); cout<<ans<<endl;}
        else if (x == 2){cout<<"Enter a number: "; cin>>num; ans = func2(num); cout<<ans<<endl;}
        else if (x == 3){cout<<"Enter a number: "; cin>>num; ans = func3(num); cout<<ans<<endl;}
        else if (x == 4){cout<<"Good bye"; return 0;}
    }
    
    
    return 0;
}
Awesome. Thank you very much
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
#include <iostream>
using namespace std;

int ipow( int base, int exponent )
{
   int result = 1;
   for ( int i = 1; i <= exponent; i++ ) result *= base;
   return result;
}

int main()
{
   int x = 0;
   while ( x != 4 )
   {
      cout << "\nPlease choose which function you would like: "<<endl;
      cout << "1. X(n) = 5*X(n-1)-7, X(0)=1\n";
      cout << "2. F(n) = 3*F(n-1)-2*F(n-2), F(0)=1, F(1)=0\n";
      cout << "3. G(n) = G(n-1) + 2*G(n-2) - 3*n, G(2)=1, G(3)=4\n";
      cout << "4. Quit\n";
      cin >> x;

      if ( x >=1 && x <= 3 )
      {
         int n, ans;
         cout << "Enter n: ";
         cin >> n;
         switch( x )
         {
            case 1: ans = ( 7 - 3 * ipow( 5, n ) ) / 4;   break;
            case 2: ans = 2 - ipow( 2, n );   break;
            case 3: ans = ( -10 * ipow( 2, n ) - 29 * ipow( -1, n ) + 18 * n + 45 ) / 12;   break;
         }
         cout << "Result = " << ans << '\n';
      }
   }
}

Topic archived. No new replies allowed.