You will write, compile, and debug a program in the file one.cxx. Write your nam
ID: 3572742 • Letter: Y
Question
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.
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.
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
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.
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 look into the comments to understand the code. I have 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 insuranceRate;
}reservations[100001];
//max variable defines the index till which reservations are saved
int maxReservations = 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].insuranceRate = insuranceRate;
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.