I managed to fix most problems. But Im still having issues with this. When I run the program both if/else is outputted. I tried moving brackets, changing if statements and everything.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string Cartype, rateoption;
double total, miles, days;
constdouble CdailyRate=30;
constdouble PdailyRate=40;
constdouble FdailyRate=50;
constdouble CmileRate=0.25;
constdouble PmileRate=0.35;
constdouble FmileRate=0.45;
cout<<"Thank you for choosing Car Rite Rental for your rental needs!\n"
<<"\a Before we get started calculating your total owed please remember\n"
<<"that here at Car Rite Rental we havea MINIMUM PAYMENT OF $30.\n\n"
<<"Please enter the type of car you have rented: \n\n"
<<"[please enter corresponding letter] \n"
<<"C-Chevrolet\n"<<"P-Pontiac\n"<<"F-Ford\n";
cin>>Cartype;
cout<<"Please choose your payment option from the following: \n\n"
<<"[please enter corresponding number] \n"
<<"D-Daily Rate\n"<<"M-Mileage Rate\n";
cin>>rateoption;
if(rateoption=="D"||rateoption=="d"){
cout<<"Please enter the number of days you have rented this vehicle: \n";
cin>>days;
}
else{
cout<<"Please enter the number of miles traveled in your rental car:\n";
cin>>miles;
}
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d")
{
total=CdailyRate*days;
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
}
elseif (Cartype=="C"||Cartype=="c" && rateoption=="M"||rateoption=="m")
{
total=CmileRate*miles;
if (total>=30)
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
else
cout<<"Your total owed today is: $30.00\n"<<"Thank you again for choosing Car Rite Rental!\n";
}
if (Cartype=="P"||Cartype=="p" && rateoption=="D"||rateoption=="d")
{
total=CdailyRate*days;
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
}
elseif(Cartype=="P"||Cartype=="p" && rateoption=="M"||rateoption=="m")
{
total=PmileRate*miles;
if (total>=30)
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
else
cout<<"Your total owed today is: $30.00\n"<<"Thank you again for choosing Car Rite Rental!\n";
}
return 0;
You code does both if statements because you have 2 if statements.
I think it would be better if you made use of some functions. Get the info for the type of rate and type of car (that could be it's own function), then call 1 of the 2 functions for DailyRate or MileageRate, with the CarType as an argument. Then have each function do it's processing from there. The rates could be stored in arrays, or the car and it's rate could be in a struct - but I am not sure whether you have gone that far in your study yet.
Also, there is the tolower or toupper function (look in the reference section) which you can apply to the input to make your logic easier later. Your input could be a char (rather than std::string), which you turn into upper case, then:
if(rateoption=='D'{
With if statements, always provide an else part to catch bad input. The same applies to switch statements - provide a default: case. Although if you do this, you should put them inside a loop, until the correct value is entered. This is slightly more complicated, but better.
For variable names, provide something meaningful as way of self documenting the code. For example, when I first read your code, I wondered what CdailyRate meant. It was obvious later, but it would be better if it was obvious straight away.
Thanks.
I thought putting a loop in but I'm stuck how to do it
You can use a bool type variable to store whether the input is valid or not, and use this to control a while loop. Here is an example - it is not a complete program: