I'm writing a car insurance code for class, in which the program takes into account the users sex and age and from there determines the extra percentage they have to pay. So if youre a male under 25 you have an extra 17% to pay and for a female under 21 you have an extra 4% to pay. I'm not sure if I'm on the right track. Any help/guidance would be greatly appreciated.
#include<iostream>
#include<iomanip>
#include<iostring>
using namespace std;
int main()
{
int value, age, tickets, rate;
string sex
cout << "Welcome to Tae's Insurance. Please answer the following to find out your rate today!\n";
cout << "Please enter the value of your vehicle: $" << endl;
cin >> value;
if (value < 0)
{
cout << "you have entered an invalid number." << endl;
exit(1);
}
cout << "Please enter your age: " << endl;
cin >> age;
if (age < 16)
{
cout << "You have entered an invalid number." << endl;
exit(1);
}
cout << "Please enter your sex (M or F): " << endl;
cin >> sex;
if (sex == "m" || sex == "M")
{
(age < 25 == rate * .17 + rate);
}
else {
age < 21 == rate * .04 + rate);
}
cout << "Please enter the number of tickets on your driving record: " << endl;
cin >> tickets;
if (tickets < 0)
{
cout << "You have entered an invalid input" << endl;
}
#include<iostream>
#include<iomanip>
#include<string>
usingnamespace std;
int main()
{
int value, age, tickets ;
double rate = 1;
string sex ;
cout << "Welcome to Tae's Insurance. Please answer the following to find out your rate today!\n";
cout << "Please enter the value of your vehicle: $" << endl;
cin >> value;
if (value < 0)
{
cout << "you have entered an invalid number." << endl;
exit(1);
}
cout << "Please enter your age: " << endl;
cin >> age;
if (age < 16)
{
cout << "You have entered an invalid number." << endl;
exit(1);
}
cout << "Please enter your sex (M or F): " << endl;
cin >> sex;
if (sex == "m" || sex == "M")
{
(age < 25 == rate * .17 + rate) ;
}
else
{
age < 21 == rate * .04 + rate ;
}
cout << "Please enter the number of tickets on your driving record: " << endl;
cin >> tickets;
if (tickets < 0)
{
cout << "You have entered an invalid input" << endl;
}
system("Pause");
return 0;
}
1) use code tags ;
2) rate is? not define initial value ;
3) rate is a double ;
4) 'sex' is 1 character is sufficient, so better if char ;
5) change the instruction on if/else statement with:
1 2 3 4 5 6 7 8 9 10 11
rate = 10.00 ; // example
if( (sex =='m' || sex == 'M') && ( age < 25 ) )
{
rate *= 1.17 ;
}
elseif( ( sex == 'f' || sex == 'F' ) && ( age < 21 ) )
{
rate * = 1.04 ;
}
Thank you for the article, I'll use code tags next time i post a questions. I think I resolved all the original issues, but upon completing the rest of the program I ran into another problem. Depending on whether the user of the program is a female or male of a certain age and how many tickets they have they, the price of their insurance rate is suppose to go up from the original rate which would be 6% of the cars total. When my program comes to the end, instead of outputting the rate it should be it outputs 0 or 1. Could you help me understand why I'm not getting the correct number.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
int car, age, tickets;
double rate == 1;
char sex;
cout << "Welcome to Tae's Insurance. Please answer the following to find out your rate today!\n";
cout << "Please enter the value of your vehicle: $";
cin >> car;
while(car <= 0)
{
cout << "you have entered an invalid number. Please put in a valid number: $";
cin >> car;
}
cout << "Please enter your age: ";
cin >> age;
while (age < 16)
{
cout << "You have entered an invalid number. Please enter a valid number: ";
cin >> age;
}
cout << "Please enter your sex (M or F): ";
cin >> sex;
if ((sex == 'm' || sex == 'M') && (age < 25))
{
rate * .17 + rate;
}
elseif((sex == 'f' || sex == 'F') && (age < 21))
{
rate * .04 + rate;
}
cout << "Please enter the number of tickets on your driving record: ";
cin >> tickets;
if (tickets < 0)
{
cout << "You have entered an invalid input. Please enter a valid number: ";
cin >> tickets;
}
elseif (tickets > 3)
{
100 + rate;
}
cout << "Your insurance rate is $" << rate << endl;
system("Pause");
return 0;
}
You just posted a block of code, and a question. Why not use it now?
It's really simple... just edit your post and add [code] and [/code] around your code.
I'm honestly curious, what did you think they were? Because it seems a lot of beginners get confused by this. Perhaps we shouldn't use the phrase "code tags" when explaining to newcomers.
FYI: I updated my post as you typed your last message, in case you missed it.
I see, that makes sense. Perhaps saying "please format your code by adding [code] {your code here}; [/code] tags around your code" would be a better thing to say.
Yeah I think I would have understood that a lot more!
But so for my program Im basically stating, If the user is a male under 25 he has to pay an extra 17% of the rate on top of the rate and for a female under 21 she has to pay 4%. And then if they have over 3 tickets its an extra $100. And the rate is 6% of the car value the user inputs.
None of the code inside your if statements does anything. Nothing is being assigned. The assignment operator is =. To actual set the rate to be something else, you need to assign it a new value.
That's a simple mistake that I missed. Thank you for point that our for me. Okay now I believe this is my last step, that I'm missing to complete my program. I'm getting an "uninitialized local variable 'car' used" error in line 13. I need this statement to define what the base rate of the insurance will be and I have no clue where to put it and if i'm going about it the right way.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
int car, age, tickets;
double rate;
char sex;
rate = car * .06;
cout << "Welcome to Tae's Insurance. Please answer the following to find out your rate today!\n";
cout << "Please enter the value of your vehicle: $";
cin >> car;
while(car <= 0)
{
cout << "you have entered an invalid number. Please put in a valid number: $";
cin >> car;
}
cout << "Please enter your age: ";
cin >> age;
while (age < 16)
{
cout << "You have entered an invalid number. Please enter a valid number: ";
cin >> age;
}
cout << "Please enter your sex (M or F): ";
cin >> sex;
if ((sex == 'm' || sex == 'M') && (age < 25))
{
rate = rate + rate * .17;
}
elseif((sex == 'f' || sex == 'F') && (age < 21))
{
rate = rate + rate * .04;
}
cout << "Please enter the number of tickets on your driving record: ";
cin >> tickets;
if (tickets < 0)
{
cout << "You have entered an invalid input. Please enter a valid number: ";
cin >> tickets;
}
elseif (tickets > 3)
{
rate = 100 + rate;
}
cout << "Your insurance rate is $" << rate << endl;
system("Pause");
return 0;
}