Hi all,
I'm new here to the forums but I've taken a look at the references and tutorials in the past. I was hoping to avoid going to the forums for help but I'm overdue on two assignments and the semester is almost over. I just need help completing these two assignments so I can clean them off my plate, so to speak.
As to the reason they're overdue - I was out with the flu a month ago for two weeks. I haven't been able to catch up both homework wise and worse, information wise. I'm not understanding what I've missed, which thus far has been functions and iostream (file input/output). For this topic I will focus on project 5.
Project: Write a function that reads all information from the user, and a function (or functions) that calculate insurance charge rates based on this information.
Indepth details: Problem Description: The HealthCare Insurance Agency determines auto insurance rates based in the driver's age, ticket history, and the value of the car. The base rate is 6% of the car value.
Male drivers under the age of 25 pay an extra 17%, and female drivers under the age of 21 pay an extra 4% of the base charge. A driver with more than three tickets pays an extra $100.
Write a function that reads all information from the user, and a function/functions calculates insurance charge rates based on this information. To test these functions write a program that uses these function to calculate insurance rates for information inputted by the user. Output answers in decimal form to two places.
Your program should allow the user to repeat this calculation as often as the user wishes
I'd like to add something before showing the code. Previously ask a friend for assistance but what he showed me is a little over my head. It doesn't help that I am not fully understanding functions. I suppose my main issue with understanding functions is their order in the logic tree.
This is the code line my friend suggested: int percent = 0.06 + (age <25 && g=="M" ? 0.17 : 0) + (age <21 && g=="F" ? 0.04 : 0);
He explained that it's a condensed version of a true-false statement. However, I am not sure where I would put this in the coding itself so I will put it to the side for now. I'd rather not use something I'm not fully understanding and learn about this first.
The Question: What sort of function would I use to call on multiple pieces of information?
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
|
// Include Section
#include <iostream>
using namespace std;
// Functions
//Main body
int main()
{
// Variables
int age,
int car,
int ticket,
char g, //g is for gender
char r, //r is for run
float gender_rate,
float rate;
//Gather information
cout << "Enter the value of the car: " << endl;
cin >> car;
cout << "Enter the age of the driver: " << endl;
cin >> age;
cout << "Enter the sex of the driver (M or F): " << endl;
cin >> g;
cout << "Enter the number of tickets on the drivers record: " << endl;
cin >> ticket;
//gender
while(g=='m'||g=='M')
if (g<25)
gender_rate = rate + 17/100;
else (g = 0);
while(g=='f'||g=='F')
if (g<21)
gender_rate = rate + 4/100;
else (g = 0);
//ticket
if (ticket>3)
ticket = 100;
else
ticket = 0;
//calculations
int base = car * 60 / 100;
rate = base + ticket;
//Display results
cout << "The insurance rate is " << rate;
//run program again?
cout <<"Would you like to run the program again (Y or N)?"<<endl;
cin>>r;
while(r=='y'||r=='Y');
}
|
TL;DR: I need help understanding functions. I don't want this code solved for me, I want to know where I'm going wrong and what I can do to improve.
Thanks,
-- DMW
EDIT - To try and give a better and more concise question.
EDIT2 - Changed title to fit question better