a little help

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.

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string Cartype, rateoption;
    double total, miles, days;
    const double CdailyRate=30;
    const double PdailyRate=40;
    const double FdailyRate=50;
    const double CmileRate=0.25;
    const double PmileRate=0.35;
    const double 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";
}

else if  (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";
}

 else if(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;
Hi Tanya122,

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.

Hope this is all useful to you :+)
Thanks.
I thought putting a loop in but I'm stuck how to do it
Line 22: cin>>Cartype;
This will leave a newline in the buffer which will get read in on the next cin.

Replace line 22 with getline(cin, Cartype);
Same goes for line 26 (with rateoption instead of Cartype).
Last edited on
Tanya122 wrote:
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:

http://www.cplusplus.com/forum/beginner/99203/#msg534078


I have posted other variations of this, use the search function at the top of this page to find them. Use
TheIdeasMan bool controlled while loop
to search with.

Let us all know how you get on, post your new code if you wish.
Topic archived. No new replies allowed.