I have most of the code for the program but hit a snag. If anyone can, I would greatly appreciate someone to look over the following and see what I may be missing please. Thanks in advance for any assistance.
The program is used to calculate what the six-month policy for vehicle insurance would be for a particular car's value, the number of accidents and residence surcharges.
The number of accidents will increase the premium as follows:
0 to 1 accident(s): No increase
2 to 3 accidents: 1% per accident
4 to 6 accidents: 3% per accident
7 or more accidents: Uninsurable
and the residence surcharge is as follows:
Class A: $20.00 surcharge
Class B: $30.00 surcharge
Class C: $50.00 surcharge
Class D: $100.00 surcharge
Class E: $250.00 surcharge
#include <iostream>
#include <cstdlib>
usingnamespace std;
double getBase (double premium);
void accidentAdjust (double accidents, double& premium);
double getRiskFactor (double premium, char riskFactor,char a,char b,char c,char d,char e);
int main()
{
double carValue, premium;
int accidents;
char riskFactor;
cout << "National Farm Insurance Company" << endl;
cout << "Automotive Policy Six-Month premium Calculator" << endl << endl;
cout << "Enter the value of the car to be insured: $";
cin >> carValue;
if (carValue<0)
{
cout<<"You have entered an invalid number."<<endl;
exit(1);
}
double getBase(double carValue, double premium);
cout << "How many accidents has the policyholder caused in the last three years? ";
cin >> accidents;
if (accidents<0)
{
cout<<"You have entered an invalid number."<<endl;
exit(1);
}
void accidentAdjust(double& accidents, double premium);
cout << "Enter the geographical risk factor (Class a - e): ";
cin >> riskFactor;
double getRiskFactor(riskFactor);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The premium for this policyholder will be $" << premium << endl << endl;
cout << "Call the neighbor that's on your side. Call National Farm." << endl;
return 0;
}
double getBase(double carValue, double premium)
{
premium=carValue/20;
return premium;
}
void accidentAdjust(double accidents, double& premium)
{
if (accidents<2)
premium=premium;
if (accidents>1 && accidents<=3)
premium=premium*(accidents*.01);
if (accidents>3 && accidents<=6)
premium=premium*(accidents*.03);
if (accidents>6)
{
cout<<"We will not be able to insure your vehicle."<<endl;
exit(1);
}
}
double getRiskFactor(double premium, char riskFactor,char a,char b,char c,char d,char e)
{
if (riskFactor = a)
premium=premium+20;
if (riskFactor = b)
premium=premium+30;
if (riskFactor = c)
premium=premium+50;
if (riskFactor = d)
premium=premium+100;
if (riskFactor = e)
premium=premium+250;
return premium;
}
I am very uncertain with the riskFactor functions working properly, but the program doesn't even reach that point.
It exits when the car value or number of accidents is negative as coded (which I would also want to extend to program exiting for any letter other than lower case a-e in the residency function).
When I run the program, it allows user to enter value for the car, number of accidents and a letter for residency class but stops when enter is pressed at that point with an error stating that "The variable 'premium' is being used without being initialized." but I have tried multiple ideas and still can't get it to run full through. It doesn't state which time premium isn't being initialized.
Thus the sum of the problem is:
Getting pass the error of not initializing 'premium'
including an exit statement for letters other than lower-case a-e in residency
possibly adjusting code to make sure calculations are correct.
As you can see, I've finished a good bit of the code but hit a snag. Thanks for any help offered.
Error 1: Geeting past the error of not initializing 'premium'
-You declared the variable as a type double by typing double premium;. I'm guessing you know what that means, then later in the program you have this:
double getBase(double carValue, double premium);
Just as the debugger says, you have not initialized it, in other words, you must set some value to premium in order to pass it in. If you just declare it as double premium; it will assign premium a value, but you have no idea what it is and it makes no sense to the compiler so it fails. You need to assign premium some sort of value before you try to pass it into the getBase function. The most simple way about doing this is say double premium = 0; at the top of your program.
Another thing that I will attach here that is causing your program to fail quickly is because at the top of your program, above the main() function, you have your function declarations. Once you declare these up there, you do not need to reinstate them in your program. Also, when passing in the variables to your functions do not declare them inside the parentheses, declare them earlier in the program.
For example: double getBase(double carValue, double premium);
Should be: getBase(carValue, premium);
2nd example: void accidentAdjust(double& accidents, double premium);
Should be: accidentAdjust(accidents, premium);
--Furthermore with the 2nd example, when you want to pass by reference using the '&' symbol, do that in your function declaration above the main function. You don't need to put it in your function call, that's bad syntax and the compiler won't have it :) Also after you change it above your main function, make sure you don't forget to change it below where you actually define the function.
I'm not sure if you know this but in your function declaration for riskFactor function, you have many different passed in variables, however when you incorrectly called the function, which I described how to fix above, you only passed in one variable. You can't do that, you must pass in as many variables as you said you were going to in the function declaration. That part of the code is: double getRiskFactor(need, riskFactor, need, need, need, need, need);
Try to fix these errors before you post again, but I believe these couple of problems will solve a lot of the overall problems.
If you have any issues, or you want to get this done quickly my AIM: mbittel08 or reply with your skype name, I'd be more than happy to help your more thoroughly.
I believe I have the complete code working through and through now (only 25 min after most recent post asking for assistance. lol). If anyone see's anything that looks out of place in this, mainly worried about the calculations part but don't have time to calculate out on both the program and a calculator, please let me know.
It's always around the "been up and moving for 20 hours" mark when I work on the code due to school and two jobs so I wouldn't be surprised if its something simple. I'm going to keep looking at it till I leave for work.
Thanks for the help mbittel12. Thanks to any help provided after this post.
// Steven Standridge
// CIS 251 (Fall 2009
// Project 2
// This program is used to calculate what the six-month policy for vehicle insurance would
// be for a particular car's value, the number of accidents and residence surcharges.
#include <iostream>
#include <cstdlib>
usingnamespace std;
double getBase (double carValue, double& premium);
double accidentAdjust (double accidents, double& premium);
double getRiskSurcharge (double& premium, char riskSurcharge);
int main()
{
double carValue, premium=0;
double accidents;
char riskSurcharge;
cout << "National Farm Insurance Company" << endl;
cout << "Automotive Policy Six-Month premium Calculator" << endl << endl;
cout << "Enter the value of the car to be insured: $";
cin >> carValue;
if (carValue<0)
{
cout<<"You have entered an invalid number. The program will now terminate."<<endl;
exit(1);
}
else getBase(carValue, premium);
cout << "How many accidents has the policyholder caused in the last three years? ";
cin >> accidents;
if (accidents<0)
{
cout<<"You have entered an invalid number. The program will now terminate."<<endl;
exit(1);
}
else accidentAdjust(accidents, premium);
cout << "Enter the geographical risk factor (Class a - e): ";
cin >> riskSurcharge;
getRiskSurcharge(premium, riskSurcharge);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The premium for this policyholder will be $" << premium << endl << endl;
cout << "Call the neighbor that's on your side. Call National Farm." << endl;
return 0;
}
double getBase(double carValue, double& premium)
{
premium = carValue / 20;
return premium;
}
double accidentAdjust(double accidents, double& premium)
{
if (accidents<2)
premium=premium;
if (accidents>1 && accidents<=3)
premium=premium*(accidents*.01)+premium;
if (accidents>3 && accidents<=6)
premium=premium*(accidents*.03)+premium;
if (accidents>6)
{
cout<<"We will not be able to insure your vehicle."<<endl;
exit(1);
}
}
double getRiskSurcharge(double& premium, char riskSurcharge)
{
switch (riskSurcharge)
{
case'A':
case'a':
premium=premium+20;
break;
case'B':
case'b':
premium=premium+30;
break;
case'C':
case'c':
premium=premium+50;
break;
case'D':
case'd':
premium=premium+100;
break;
case'E':
case'e':
premium=premium+250;
break;
default:
cout<<"You have entered an invalid number. The program will now terminate."<<endl;
exit(1);
}
return premium;
}
In double accidentAjust(double accidents, double& premium), control reaches the end of a non-void function - you don't return anything even though you specify a return type. You can change that to void and it would still work, but you can add a return premium; at the end.
Why is accidents a double? I would change it to an int to hold whole numbers. It's kind of hard to get 2.6235 accidents.