Logic Error in Basic Code

So we had a lab today in my CS class at the end of the period - for the last hour, we had to write a program that caculated the cost of renting a car based on the car type (express or luxury), age of the driver, and the number of days the car is rented. Also, we had to account for a 10% discount if the car is rented for more then 7 days.
the monetary rates were given to us, which you can see in the code - my code compiled, but the problem is only the first equation seems to be going. I already submitted it and feel the defeat of not fully compiling code, and was wondering if anyone could take a look at it and tell me if they see the glaring issue.
https://gyazo.com/cddc56384a9b4ba788737bbf119bf3cc <- a gyazo of the problem
https://www.mediafire.com/?5859nar7zhuwne8 <- .cpp mediafire link
http://pastebin.com/k4PmL7Y9

<- Pastebin for source code
Thank you in advance!
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>
#include<iomanip>
using namespace std;
 
int main()
{
   
    int resLength; //Reservation length declared
    char carType; //cartype (e or l)
    int age; // self explanatory
    double cost; //the final cost - age * car type, later will put if statement for if (resLength > 7)
   
   
    cout << "Please enter length of reservation: ";
    cin >> resLength;
    cout << "Please enter car type: ";
    cin >> carType;
    cout << "How old is the driver? ";
    cin >> age;
 
    //made four if statements under to calculate cost based on all possible situations
    if (carType == 'e' || 'E'  && age <= 25)
        cost = resLength * 29.95;
    else if (carType == 'e' || 'E' && age > 25)
        cost = resLength * 25.95;
    else if (carType == 'l' || 'L' && age <= 25)
        cost = resLength * 49.95;
    else if (carType == 'l' || 'L' && age > 25)
        cost = resLength * 42.95;
   
     if (resLength > 7)
        cost = cost - (.1 * cost);
 
 
    if (carType == 'e' || carType == 'E')
        cout << "The cost for an economy car for " << resLength << " days is $" << fixed << setprecision(2) << cost << endl;
    else if (carType == 'l' || carType ==  'L')
        cout << "The cost for a Luxury car for " << resLength << " days is $" << fixed << setprecision(2) << cost << endl;
 
 
    return 0;
}
The glaring problems are your if statements, let's just look at the first one:

if (carType == 'e' || 'E' && age <= 25)

This will always evaluate to true, so it will always execute first and ignore the other branches.
It's always true because 'E' is an expression which will evaluate to a non-zero value, and therefore true. Computers can't read your mind or make assumptions on your behalf - they will do exactly what you tell them to do. You need to be as explicit as possible:

if((carType == 'e' || carType == 'E') && age <= 25)

Do this for all the other branches as well. Also, note the use of parentheses.
Last edited on
Topic archived. No new replies allowed.