sex+age

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;
}




system("Pause");

return 0;
}
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
#include<iostream>
#include<iomanip>
#include<string>
using namespace 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 ;
}
else if( ( sex == 'f' || sex == 'F' ) && ( age < 21 ) )
{
        rate * = 1.04 ;
}


6) declare double 'Total' = rate * ticket ;

7) print on the screen 'total' ;

8) delete #include <iostring>


Last edited on
What do you mean use code tags? And then could you further explain 2, 6 and 7?
Code tags: read this

http://www.cplusplus.com/forum/beginner/1/

2,6,7 :

I just tried to interpret your code. It seems to me the only possibility of using your code.
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.

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
66
67
68
69
#include<iostream>
#include<iomanip>
using namespace 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;
	}
	else if((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;
	}
	else if (tickets > 3)
	{
		100 + rate;
	}


	cout << "Your insurance rate is $" << rate << endl;

	

	system("Pause");

	return 0;
}
Last edited on
I'll use code tags next time i post a questions

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.

double rate == 1;
I don't know how this compiles.

1
2
3
4
5
6
7
8
if ((sex == 'm' || sex == 'M') && (age < 25))
{
rate * .17 + rate;
}
else if((sex == 'f' || sex == 'F') && (age < 21))
{
rate * .04 + rate;
}

This doesn't do anything.

1
2
3
4
else if (tickets > 3)
{
    100 + rate;
}

This also doesn't do anything.

You are never making any assignments in those if statements.
Perhaps you want, e.g. rate = rate * 0.4 + rate;
Last edited on
Oh I think misinterpreted what code tags were. So its basically just adding "" to the beginning and "" to the end?
Last edited on
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.
Last edited on
I thought code tags were like categories you put in the descriptions. Almost like hastags. To narrow down your question/topic.

Ex: I need help with if else statements #IfElse #Beginner #bool
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.
Last edited on
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.
Last edited on
So, to re-iterate:

1
2
3
4
5
6
7
8
if ((sex == 'm' || sex == 'M') && (age < 25))
{
    rate * .17 + rate;
}
else if((sex == 'f' || sex == 'F') && (age < 21))
{
    rate * .04 + rate;
}


1
2
3
4
else if (tickets > 3)
{
    100 + rate;
}


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.

ex:
 
rate = rate + 0.04 * rate; // increase rate by 4% 
Last edited on
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.

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
66
67
68
69
70
71
#include<iostream>
#include<iomanip>
using namespace 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;
	}
	else if((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;
	}
	else if (tickets > 3)
	{
		rate = 100 + rate;
	}


	cout << "Your insurance rate is $" << rate << endl;

	

	system("Pause");

	return 0;
}
Topic archived. No new replies allowed.