Calculate cost (if else statements)

Input the age (int), gender (char) and number of credit hours (int) to be registered from the user.
Calculate the total cost the user needs to pay according to:

1) If the age is less than 18 then the user cannot register more than 12 credit hours, and each hour
costs 1500.

2) If the age is from 18 to 22 inclusive and the gender is female then the first 6 credit hours are free, and the cost of any additional credit hour will be 1000 each. The value of the gender will be ‘f’ or ‘F’ for the female.

3) If the age is from 18 to 22 inclusive and the gender is male then the cost of the first 8 credit hours is 8000, and the cost of any additional credit hour will be 500 each. The value of the gender will be ‘m’ or ‘M’ for the male.

4) If the age is more than 22 then the cost of each credit hour is 2000.


i cant figure the formula for calculating cost of 2) & 3)



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
#include<iostream>
using namespace std;
int main()
{
    int age, credits;
    double cost;
    char gender;

    cout<<"Enter age: ";
    cin>>age;

    cout<<"Enter gender: ";
    cin<<gender;

    cout<<"how many credits: ";
    cin>>credits;


    if (age < 18 ) && (credits > 12) && (gender == 'm' || gender == 'M') && (gender == 'f' || gender == 'F')
    cout<<" You cannot register more than 12 hours."<<endl;
    
    else if 
    (age <18) && (credits < 12) && (gender == 'm' || gender == 'M') && (gender == 'f' || gender == 'F')
    {
    cost = credits * 1500;
    }
    else if
    
    (age > 18 && age =22 && gender == 'f' || gender == 'F')
    {
        cost = ;
    }
    else if (age < 18 && age =22) && (gender == 'm' || gender == 'M')
    {
        cost = ;
    }
    else (age >22)  && (gender == 'm' || gender == 'M') && (gender == 'f' || gender == 'F')
    {
        cost = credits * 2000;
    }
return 0;
}
Last edited on
It is easier to separate each group by age first and then gender as a contained if statement.

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
#include<iostream>
using namespace std;
int main()
{
    int age, credits;
    double cost;
    char gender;

    cout<<"Enter age: ";
    cin>>age;

    cout<<"Enter gender: ";
    cin >> gender;

    cout<<"how many credits: ";
    cin>>credits;


    if (age < 18) {
        if (credits > 12) {
           cout<<" You cannot register more than 12 hours."<<endl;
           credits = 12;
        }
        cost = credits * 1500;   
    }
    
    if (age > 17 && age < 23) {
     if (gender == 'f' || gender == 'F') {
         if (credits > 6) {
        cost = (credits - 6) * 1000; 
         }
         else {
          cost = 0;   
         }
     }
     else {
      if (credits < 9) {
          cost = credits * 1000;
      }
      else {
          cost = (credits - 8) * 500 + 8000;
      }
     }
    }
    
    if (age > 22) {
       cost = credits * 2000; 
    }
    
    cout << "$" << cost << " for " << credits << " credit hours.";
    
return 0;
}
thanks i get it now, much appreciated.
Topic archived. No new replies allowed.