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
#include<iostream>
#include<iomanip>
usingnamespace 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;
elseif (carType == 'e' || 'E' && age > 25)
cost = resLength * 25.95;
elseif (carType == 'l' || 'L' && age <= 25)
cost = resLength * 49.95;
elseif (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;
elseif (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: