Help Looping these functions/Array Help

So I've been working on this code, and the whole point of it is to loop at the end and allow the user to input 1,2,3, or 4 and then I print the options. I'm having trouble when they type in their choice: Whatever number they type in, it continues to "Please enter f/F for flying..." Even though I have a do while loop at the top.


I assume it is because choice isn't initialized in the main function - how do I fix that?


Also, how could I save their input and print it out if they type in 2 or 3?(If they type in 2 I ask them the type of sport, then print the ages of the people who made reservations)

Thank you!
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    #include<iostream>
    #include<iomanip>
    #include<string>

    using namespace std;
    // my prototypes
    
    void print_menu();
    int input_choice();
    char input_type();
    int input_age();
    double compute_rate();
    int input_reservation();
    void print_all();

    int main()
    {
        
        int choice;    
        int userAge;
        
       
    
         if (choice == 4)
        {
            return 0;
        }
        
        do
        {
            input_choice();
            compute_rate();
            
        } while (choice == 1);
        
       
    
        return 0;
    }

    void print_menu() // Printed as a void since nothing to return for this
    {
    cout << "Please pick from the following menu" << endl;
    cout << "1. Add a new reservation" << endl;
    cout << "2. Print all reservations" << endl;
    cout << "3. Print all reservations for a given sport" << endl;
    cout << "4. Quit" << endl;
    
    }

    int input_choice()
    {
        int choice = 0;
        
        print_menu();
        cin >> choice;
        
        return choice;
    }
    
    char input_type()
    {       
        char sport;
        
        cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding" << endl;
        cin >> sport;
        
        return sport;
    }
    
    int input_age()
    {
       
        int userAge;
        
        cout << "Please enter the age of the patron, minimum 16" << endl;
        cin >> userAge;
        
        return userAge;
    }
    
    double compute_rate()
    {
       
       double patronRate;
       char sport = input_type();
       int userAge = input_age();
        
        if (((userAge <= 25) && (sport == 'F')) || ((userAge <= 25) && (sport == 'f')))
            patronRate = 68.95;
        if (((userAge > 25) && (sport == 'F')) || ((userAge > 25) && (sport == 'f')))
            patronRate = 55.95;
        if (((userAge <= 25) && (sport == 'g')) || ((userAge <= 25) && (sport == 'G')))
            patronRate = 73.95;
        if (((userAge > 25) && (sport == 'g')) || ((userAge > 25) && (sport == 'G')))
            patronRate = 65.95;
        if (((userAge <= 25) && (sport == 'h')) || ((userAge <= 25) && (sport == 'H')))
            patronRate = 99.95;
        if (((userAge > 25) && (sport == 'h')) || ((userAge > 25) && (sport == 'H')))
            patronRate = 92.95;
            
            
        cout << "The insurance rate is $" << patronRate << "\n" << endl;
           
        
        return patronRate;
    }
Last edited on
Topic archived. No new replies allowed.