Problem You will write, compile, and debug a program in the file one.cxx. Write
ID: 3577050 • Letter: P
Question
Problem
You will write, compile, and debug a program in the file one.cxx. Write your name in the header at the top of the file. Remember, this is a practice for the test, which is three hours long. Try to do this with only your notes and books. Write in C++
Problem Statement
Dare-Devils Recreational Company provides three types of recreational activities: flying, gliding and hang-gliding. It mandates that all its patrons buy insurance when they reserve an activity. The insurance premium is based on the type of sport and the age of the insured person:
Type of sport
Age 25 or under
Age over 25
Flying
$ 68.95
$ 55.95
Gliding
$ 73.95
$ 65.95
Hang-gliding
$ 99.95
$ 92.95
Write a menu-driven (responsive to user input) program to track all the reservations of Dare-Devils Recreational Company for a day (assume no more than 100 reservations will be processed in a given day). For each reservation, your program should keep track of the type of sport and the age of the patron (only). The menu options are:
1. Add a new reservation: Your program should
input and save
the type of sport (flying/gliding/hang-gliding)
the age of the patron
compute the insurance rate as shown in the table earlier
output the insurance rate in the format shown in Expected Sample Run
section.
1
Print all the reservations: Your program should output (for all of the reservations of the day in the format shown in Expected Sample Run section)
the type of sport and
the age of the patron
Print all the reservations for a given sport: Your program should
input the desired type of sport and output
the details of each reservation for the selected sport, in the format shown in Expected Sample Run section;
the total number of reservations processed for the selected sport.
4. Quit: Your program should print the total number of reservations processed that
day.
Implementation Notes:
You must define the following functions:
print_menu: To print all the menu options
input_choice: To read, validate, and return the menu option selected by the
user.
It should read the menu option as numerical input and return it as a
number;
It should call print_menu() defined earlier.
input_type: To read, validate, and return the type of sport: flying (f/F), gliding (g/G) or hang-gliding (h/H).
Your function should read the type as character input, and return it as a character.
input_age: To read, validate, and return the age of the patron. The minimum age of the patron is 16 and max is 112.
compute_rate: To calculate and return the insurance rate of the patron for the given sport.
It should obtain the necessary data as parameters from the caller;
It should return the insurance rate to the caller.
input_reservation : which will
2
input and save all the information about one reservation. It must call input_type and input_age functions defined earlier;
Compute the insurance rate and print it. It must call compute_rate function defined earlier.
7. print_all: To print all the reservations of the day. In addition, make sure of the following:
Validate all inputs (i.e., loop until the user inputs a valid one).
Print all dollar amounts correct to two decimal places.
Accept all character inputs in both lower and upper case.
Use cast operators wherever necessary.
Use the appropriate data type for each variable.
Pass parameters to functions only as necessary.
Pass parameters by reference only as necessary.
Use proper indentation for your code.
Make sure the output of your program is exactly as shown in the Expected Sample Run section.
Expected Sample Run
After writing the program, test it with the data in the table below. The output of your program should resemble the sample run that follows.
Sample Data
Type of sport
Age of the patron
Flying
21
Gliding
23
Hang-gliding
25
Flying
27
Gliding
29
Hang-gliding
31
3
----------------------------- Start Sample Execution -----------------------------
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
f
Please enter the age of the patron, minimum 16
21
The insurance rate is $68.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
g
Please enter the age of the patron, minimum 16
23
The insurance rate is $73.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
h
Please enter the age of the patron, minimum 16
25
The insurance rate is $99.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
F
Please enter the age of the patron, minimum 16
4
27
The insurance rate is $55.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
G
Please enter the age of the patron, minimum 16
29
The insurance rate is $65.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
1
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
H
Please enter the age of the patron, minimum 16
31
The insurance rate is $92.95
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
3
Please enter f/F for flying, g/G for gliding and h/H for hang-gliding
G
A patron aged 23 reserved a session of gliding A patron aged 29 reserved a session of gliding There were a total of 2 reservations
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
2
A patron aged 21 reserved a session of flying
A patron aged 23 reserved a session of gliding
A patron aged 25 reserved a session of hang-gliding
5
A patron aged 27 reserved a session of flying
A patron aged 29 reserved a session of gliding
A patron aged 31 reserved a session of hang-gliding
Please pick from the following menu
1. Add a new reservation
2. Print all reservations
3. Print all reservations for a given sport 4. Quit
4
There were a total of 6 reservations
----------------------------- End Sample Execution -----------------------------
Type of sport
Age 25 or under
Age over 25
Flying
$ 68.95
$ 55.95
Gliding
$ 73.95
$ 65.95
Hang-gliding
$ 99.95
$ 92.95
Explanation / Answer
Please see comments to understand the code. There has created a structure to save the data.
#include <iostream>
using namespace std;
//structure to save all the reservations.
struct Reservation {
char sport;
int age;
float insurance Rate;
}reservations[100001];
//max variable defines the index till which reservations are saved
int max Reservations = 0;
void print_menu() {
cout<<"1. Add a new reservation"<<endl;
cout<<"2. Print all the reservations"<<endl;
cout<<"3. Print all the reservations for a given sport"<<endl;
cout<<"4. Quit"<<endl;
}
int input_choice() {
cout<<"Enter your choice."<<endl;
int input;
cin>>input;
if(input < 1 || input > 4) {
cout<<"Wrong Input. Try Again"<<endl;
return input_choice();
}
return input;
}
char input_type() {
cout<<"Enter type of sport"<<endl;
char c;
cin>>c;
if(c!='g'&& c !='G'&& c!='f'&& c !='F'&& c!='h'&& c !='H' ) {
cout<<"No such sport exists. Please try again"<<endl;
return input_type();
}
return c;
}
int input_age () {
cout<<"Enter your age"<<endl;
int age;
cin>>age;
if(age<16 || age > 112) {
cout<<"Sorry. Age is out of range. It must be between 16 and 112. Try Again"<<endl;
return input_age();
}
return age;
}
float compute_rate(char sport, int age) {
if(sport == 'g' || sport == 'G') {
if(age <=25)
return 73.95f;
else
return 65.95f;
} else if(sport == 'f' || sport == 'F') {
if(age <=25)
return 68.95f;
else
return 55.95f;
} else {
if(age <=25)
return 99.95f;
else
return 92.95f;
}
}
void input_reservation() {
char sport = input_type();
int age = input_age();
float insuranceRate = compute_rate(sport,age);
//saving the reservations in an struct array defined at the top
reservations[maxReservations].sport = sport;
reservations[maxReservations].age = age;
reservations[maxReservations].insurance Rate = insurance Rate;
maxReservations++;
cout<<"Insurance Rate : "<<insuranceRate<<endl;
}
void print_all() {
for(int i=0;i<maxReservations;i++) {
cout<<reservations[i].sport<<" "<<reservations[i].age<<" "<<reservations[i].insuranceRate<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.