Function in program not printing out a list of arrays when being called

Hello, I'm writing a program which saves 'reservations' of sports and is navigated via a simple text menu that is printed each iteration of the program. It prints an insurance rate for customers based on their age and desired sport and saves their age and desired sport at the same indices within an array of no more than 100 patrons.

So far, I've gotten the program as far as saving the values of the patrons age, sport type they are making a reservation for, and printing the corresponding insurance rate, but I cannot get it to run the second menu choice, which is responsible for printing all of the patrons using a for loop that is dictated by a variable, int size. The variable is innately set to 0 and I have it programmed to increment each time a new reservation is added.

I am fairly sure that the issue I am having is that I am not referencing the size variable properly in my print_all_reservations function to print the necessary statement. However, I have really no idea where to go from here.

UPDATE: I have the print_all_reservations function working properly. Now I need to figure out someway to 1) Say that a session of 'f' is actually a session of flying instead for example and 2) I need to make my print_by_sport function work

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
using namespace std;
void print_menu(); //Prototypes
int input_choice();
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate);
char input_type();
int input_age();
double compute_rate(int patron_age[], char sport_type[], int index);
void print_all(int patron_age[], char sport_type[], int index, int size);
void print_by_sport(int patron_age[], char sport_type[], int index, int size);

int main() //Main Function
{
	int patron_age[100];
	char sport_type[100];
	int index = 0;
	int size = 0;
	double insurance_Rate;
	int menu_choice; //Variable Declaration
	do
	{
		cout << "\tIndex is currently equal to " << index << endl; //cout debugging
		print_menu();
		menu_choice = input_choice();
		if (menu_choice == 1)
		{
			input_reservation(patron_age, sport_type, index, insurance_Rate);
			size++;
			index++;
		}
		if (menu_choice == 2)
		{
			print_all(patron_age, sport_type, index, size);
		}
		if (menu_choice == 3)
		{
			print_by_sport(patron_age, sport_type, index, size);
		}
		if (menu_choice == 4)
		{
			cout << "The program will now end " << endl;
		}
	} while (menu_choice != 4);
	return 0;
}
void print_menu() //Function that prints the program 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() //Function to get menuchocie from user
{
	int menu_selection;
	cin >> menu_selection;
	while (menu_selection > 4 || menu_selection < 1) //Validates input
	{
		cout << "\tError: Invalid input, please try again: ";
		cin >> menu_selection;
	}
	return menu_selection; //Returns the menuchocie
}
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate) //Working
{
	double insurance;
	sport_type[index] = input_type();
	patron_age[index] = input_age();
	cout << sport_type[index] << endl; //cout debugging
	cout << patron_age[index] << endl; //cout debugging
	insurance = compute_rate(patron_age, sport_type, index);
	cout << "The insurance rate is $" << insurance << endl;

} 
char input_type() //Reads and validates the sport being reserved
{
	char sport_type_entry;
	cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding: "; //Working
	cin >> sport_type_entry;
	while (sport_type_entry != 'f' && sport_type_entry != 'F' && sport_type_entry != 'g' && sport_type_entry != 'G' && sport_type_entry
		!= 'h' && sport_type_entry != 'H')
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> sport_type_entry;
	}
	return sport_type_entry;
}
int input_age() //Reads and validates the age of the patron
{
	int patron_age_entry;
	cout << "Please enter the age of the patron, minimum 16: ";
	cin >> patron_age_entry;
	while (patron_age_entry < 16 || patron_age_entry > 112)
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> patron_age_entry;
	}
	return patron_age_entry;
}
double compute_rate(int patron_age[], char sport_type[], int index) //Computes the insurance rate of the patron
{
	double insurance_Rate;
	if (sport_type[index] == 'f' || sport_type[index] == 'F') //If sport index is flying, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 68.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 55.95;
		}
	}
	else if (sport_type[index] == 'g' || sport_type[index] == 'G')//If sport index is gliding, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 73.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 65.95;
		}
	}
	else if (sport_type[index] == 'h' || sport_type[index] == 'H') //If sport index is hand gliding, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 99.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 92.95;
		}
	}
	return insurance_Rate;
}
void print_all(int patron_age[], char sport_type[], int index, int size) //Function to print all reservations
{
	cout << "Size is equal to " << size << endl; //cout debugging
	index = 0;
	for (int i = 0; i < size; i++)
	{
		cout << "A patron aged " << patron_age[index] << " reserved a session of " << sport_type[index] << endl;
		index++;
	}
}
void print_by_sport(int patron_age[], char sport_type[], int index, int size) //Function to print all reservations based on sport type
{
	for (int i = 0; i < size; i++)
	{
		
	}
}
Last edited on
@mgala

line 145 should not be couting the variable 'index', which is set to 0 on line 142, but to 'i' as in the loop you're using in line143.

cout << "A patron aged " << patron_age[i] << " reserved a session of " << sport_type[i] << endl;
Say that a session of 'f' is actually a session of flying

use string sport_type[100] instead of char sport_type[100] or, if you can't use std::string for some reason, const char* sport_type[100]

need to make my print_by_sport function work

for that you'll have to pass another argument to print_by_sport() which would be the name of the actual sport. Since you're already using char let's do it with char:
1
2
3
4
5
6
7
8
9
10
11
void print_by_sport(int patron_age[], const char* sport_type[], int index, int size, const char* s) 
//Function to print all reservations based on sport type
{
	for (int i = 0; i < size; i++)
	{
        if(sport_type[i] == s)
        {
            // do stuff; 
            ...
        }
}

On a parting note, and if you still have the time and inclination, I'd suggest you use a struct to manage all you data in one place rather than juggling various

arrays. Something on the lines of:
1
2
3
4
5
struct Patron
{
	int age;
	string sport;
}

The store the patrons in a vector<Patron>, not array, which would automatically manage it's size etc

Thanks for all the help! Unfortunately I'm not allowed to use structs or classes per the assignment guidelines. Appreciate the help though! I have the code running completely, here it is if you're interested:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
using namespace std;
void print_menu(); //Prototypes
int input_choice();
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate);
char input_type();
int input_age();
double compute_rate(int patron_age[], char sport_type[], int index);
void print_all(int patron_age[], char sport_type[], int index, int size);
void print_by_sport(int patron_age[], char sport_type[], int index, int size);

int main() //Main Function
{
	int patron_age[100];
	char sport_type[100];
	int index = 0;
	int size = 0;
	double insurance_Rate;
	int menu_choice; //Variable Declaration
	do
	{
		print_menu();
		menu_choice = input_choice();
		if (menu_choice == 1)
		{
			input_reservation(patron_age, sport_type, index, insurance_Rate);
			size++;
			index++;
		}
		if (menu_choice == 2)
		{
			print_all(patron_age, sport_type, index, size);
		}
		if (menu_choice == 3)
		{
			print_by_sport(patron_age, sport_type, index, size);
		}
		if (menu_choice == 4)
		{
			cout << "There were a total of " << index << " reservations" << endl;
		}
	} while (menu_choice != 4);
	return 0;
}
void print_menu() //Function that prints the program 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() //Function to get menuchocie from user
{
	int menu_selection;
	cin >> menu_selection;
	while (menu_selection > 4 || menu_selection < 1) //Validates input
	{
		cout << "\tError: Invalid input, please try again: ";
		cin >> menu_selection;
	}
	return menu_selection; //Returns the menuchocie
}
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate) //Working
{
	double insurance;
	sport_type[index] = input_type();
	patron_age[index] = input_age();
	insurance = compute_rate(patron_age, sport_type, index);
	cout << "The insurance rate is $" << insurance << endl;
} 
char input_type() //Reads and validates the sport being reserved
{
	char sport_type_entry;
	cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding: "; //Working
	cin >> sport_type_entry;
	while (sport_type_entry != 'f' && sport_type_entry != 'F' && sport_type_entry != 'g' && sport_type_entry != 'G' && sport_type_entry
		!= 'h' && sport_type_entry != 'H')
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> sport_type_entry;
	}
	return sport_type_entry;
}
int input_age() //Reads and validates the age of the patron
{
	int patron_age_entry;
	cout << "Please enter the age of the patron, minimum 16: ";
	cin >> patron_age_entry;
	while (patron_age_entry < 16 || patron_age_entry > 112)
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> patron_age_entry;
	}
	return patron_age_entry;
}
double compute_rate(int patron_age[], char sport_type[], int index) //Computes the insurance rate of the patron
{
	double insurance_Rate;
	if (sport_type[index] == 'f' || sport_type[index] == 'F') //If sport index is flying, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 68.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 55.95;
		}
	}
	else if (sport_type[index] == 'g' || sport_type[index] == 'G')//If sport index is gliding, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 73.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 65.95;
		}
	}
	else if (sport_type[index] == 'h' || sport_type[index] == 'H') //If sport index is hand gliding, do this insurance calculation
	{
		if (patron_age[index] <= 25)
		{
			insurance_Rate = 99.95;
		}
		else if (patron_age[index] > 25)
		{
			insurance_Rate = 92.95;
		}
	}
	return insurance_Rate;
}
void print_all(int patron_age[], char sport_type[], int index, int size) //Function to print all reservations
{
	for (int i = 0; i < size; i++)
	{
		cout << "A patron aged " << patron_age[i] << " reserved a session of " << sport_type[i] << endl;
	}
}
void print_by_sport(int patron_age[], char sport_type[], int index, int size) //Function to print all reservations based on sport type
{
	char sport_type_identifier;
	cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding: ";
	cin >> sport_type_identifier;
	for (int i = 0; i < size; i++)
	{
		if (sport_type_identifier == sport_type[i])
		{
			if (sport_type[i] == 'f')
			{ 
				cout << "A patron aged " << patron_age[i] << " reserved a session of flying " << endl;
			}
			else if (sport_type[i] == 'g')
			{
				cout << "A patron aged " << patron_age[i] << " reserved a session of gliding " << endl;
			}
			else if (sport_type[i] == 'h')
			{
				cout << "A patron aged " << patron_age[i] << " reserved a session of hang-gliding ";
			}
		}
	}
}
Topic archived. No new replies allowed.