Help with Functions/Array Code: calling a function but won't let me enter input?

Hi guys! I'm a CS major who got assigned our most difficult homework yet, and was wondering if I could get any help.

So we have to make a menu driven program to track all the reservations at a hotel, and what sport and age each patron are.

this is what I have so far: http://pastebin.com/EtnMFgab

We have to use all the functions I have at the top of the code(I made prototypes for them), and call them when necessary.

These are some example inputs: https://gyazo.com/16235f28ceacf9e0b484e36c41d3410b
https://gyazo.com/16235f28ceacf9e0b484e36c41d3410b



I was wondering why my input breaks after I enter the type of sport(and what I could do to fix it):
https://gyazo.com/1327cdfa602035d878661dea1420dc30


Also, how would I be able to save the inputs? I assume I would use an array, but does anybody have any advice to point me in the right direction?


Thank you so 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
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
      #include<iostream>
    #include<iomanip>
    #include<string>

    using namespace std;

    void print_menu();
    int input_choice(int menuChoice);
    char input_type(char sport);
    int input_age(int userAge);
    double compute_rate(double patronRate);
    int input_reservation();
    void print_all();

    int main()
    {
    int menuChoice;
    int userAge;
    
    input_choice(menuChoice);
    input_age(userAge);
    
    
    return 0;
    }

    void print_menu()
    {
    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)
    {
        print_menu();
        cin >> choice;
        
        if (choice == 1)
        {
            cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding" << endl;
            cin >> choice;
            
        }
        
        
        
        return choice;
    }
    char input_type(char 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;
        int userAge;
        
        input_age(userAge);
        input_type(sport);
        
        if (((userAge >= 21) && (sport == 'F')) || ((userAge >= 21) && (sport == 'f')))
        {
            patronRate = 68.95;
            cout << "The insurance rate is $" << patronRate << endl;
        }        
        
        return patronRate;
    }
I'm just going to start with the first thing I see that I don't like.

Your function input_choice has a parameter that makes no sense (you just write over it), and also it seems to return either an int OR a char. While you can cast a char to an int, don't. Pick one. What does this function do? PICK ONE THING THIS FUNCTION DOES.
Hi,

Sorry I think I fixed that - I saw that problem but now I'm stuck in an infinite loop



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
    #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 menuChoice;
    int userAge;
    
    input_choice();
    input_age();
    
    
    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;
        
        print_menu();
        cin >> choice;
        
        if (choice == 1)
        {
            input_type();
        }   
       
        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;
        
        input_age();
        
        return sport;
    }
    
    int input_age()
    {
       
        int userAge;
        
        cout << "Please enter the age of the patron, minimum 16" << endl;
        cin >> userAge;
        
        compute_rate(); 
           
        return userAge;
    }
    
    double compute_rate()
    {
       
       double patronRate;
       char sport = input_type();
       int userAge = input_age();
        
        if (((userAge >= 21) && (sport == 'F')) || ((userAge >= 21) && (sport == 'f')))
        {
            patronRate = 68.95;
            cout << "The insurance rate is $" << patronRate << endl;
        }        
        
        return patronRate;
    }


Is this better? I think I fixed that problem
This is pretty bad design. Your functions are calling each other in an infinite loop.

input_type() calls input_age() calls compute_rate() calls input_type() calls input_age() calls compute_rate() calls input_type() calls input_age() calls compute_rate() input_type() calls input_age() calls compute_rate()...

Do not have your functions calling each other.
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
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
    #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 = input_choice();    
        int userAge;
    
         if (choice == 4)
        {
            return 0;
        }
        
        do
        {
            input_choice();
            compute_rate();
            
        } while (choice != 4);
        
       
    
        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;
        
        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;
            
            
        cout << "The insurance rate is $" << patronRate << "\n" << endl;
           
        
        return patronRate;
    }



I think I fixed the aforementioned design flaws.


My problem is now, the initial menu prints twice for some reason - how can I fix that?


Thank you again!
Your menu prints out every time you call the function input_choice()

You are calling it on line 19. And then again on line 29. So twice.

I note that once the while loop is entered, the value of choice never changes so that while loop will never end.
I fixed the first problem, but I'm having trouble with how to fix the while loop - I've tried adding an if statement into input_choice and it didn't work


Do you have any ideas as to why choice isn't being initialized?


Thank you again!
Topic archived. No new replies allowed.