Only running if statement, what to do

Hey guys, I'm almost done with my assignment but it seems as though it's only running the If statement in the function, what can I do to fix this, and are there any other pointers for this program you can give me.

Thanks a ton!

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//Type of Assignment: Homework Assignment 6
//Problem Number: 6
//Section Number: 100
//Author: 
//Date Assigned: 10/30/2014
//Program Name: Assignment6
//File Name: Assignment6.cpp
//Time Spent on Program: 

//Purpose of Program: 


#include <iostream>
#include <iomanip>
#include <ctype.h>
using namespace std;

void insurance_cost(int value_of_car, int actual_age, int gender_choice, int ticket_history);

int main()
{
	char gender, quit_choice;
	int age, tickets;
	double car_value;

	cout.setf(ios::fixed | ios::showpoint);
	cout << setprecision(2);

	do
	{

		cout << "This program will tell you the insurance rate of the driver" << endl;
		cout << "Enter the value of the car:" << endl;
		cin >> car_value;
		cout << "Enter the age of the driver:" << endl;
		cin >> age;
		cout << "Enter the sex of the driver (M or F):" << endl;
		cin >> gender;
		cout << "Enter the number of tickets on the drivers record:" << endl;
		cin >> tickets;

		insurance_cost(car_value, age, gender, tickets);

		cout << "Would you like to run this program again? (Y or N):" << endl;
		cin >> quit_choice;
	} 
	while (toupper(quit_choice) == 'Y');
	return 0;

}

void insurance_cost(int value_of_car, int actual_age, int gender_choice, int ticket_history)
{
	double base_rate = value_of_car*0.06;
	double cost;
	double ticket_fee = 100.00;

	if (toupper(gender_choice) == 'M' || 'F')
	{
		cost = base_rate;
		cout << "The insurance rate of the driver is $ " << cost << endl;
	}

	else if (toupper(gender_choice) == 'M' && actual_age < 25)
	{
		cost = (base_rate * 0.17) + base_rate;
		cout << "The insurance rate of the driver is $ " << cost << endl;
	}

	else if (toupper(gender_choice) == 'M' && actual_age < 25 && ticket_history >= 3)
	{
		cost = (base_rate * 0.17) + base_rate + ticket_fee;
		cout << "The insurance rate of the driver is $ " << cost << endl;
	}

	else if (toupper(gender_choice) == 'F' && actual_age < 21)
	{
		cost = (base_rate * 0.04) + base_rate;
		cout << "The insurance rate of the driver is $ " << cost << endl;
	}

	else if (toupper(gender_choice) == 'F' && actual_age < 21 && ticket_history >= 3)
	{
		cost = (base_rate * 0.04) + base_rate + ticket_fee;
		cout << "The insurance rate of the driver is $ " << cost << endl;
	}

}
Last edited on
Line 57 is your problem. That does not check if gender_choice is M or F- it checks if it is M, or whether F is non-zero. What you want is this:

if(toupper(gender_choice) == 'M' || toupper(gender_choice) == 'F')

Also, you want to change the order of that if-else tree. The general case (if gender is M or F) should be the last, not the first. The more specific each if statement, the closer to the top it should be. So for it to work, the order should be (by line number of if statements):

69
81
63
75
57
Last edited on
Thank you so much, I love this forum.
Really helps me to understand the stupid mistakes I am making.
Would you perhaps recommend an easier way to solve this problem other than using if else statements?
Since you started a new thread and thus we do not have the original problems statement, it is hard to tell.

This should probably be solved using if ... else if ... else (may be several else if) and possibly imbedded additional if statements. However, it does not look like you are taking advantage of the chain of the ifs. So for example:
1
2
3
4
5
6
7
8
    if (conditionA) {
        /* do something assuming conditionA is true (not zero) */
    } else
        /* Now I know that !conditionA and I do not need to retest it. */ 
           if (conditionB)
               /* Now do something assuming !conditionA && condition B */
    /* and so on */
Last edited on
Topic archived. No new replies allowed.