When ever run this program, I can't get the total price right and I tried it so many times but failed. Can anyone help me where or how to fix this mistake. This is my mini project for my assessment :(.

using namespace std;
int main ()
{
    char name [30], ic [20];
    char day [10], date [10];
    char package, vehicle_code;
    char number_plate [10];
    double price, total;

    cout<<"\n\n                              ===================================================================";
    cout<<"\n                              |-----------------------------------------------------------------|";
    cout<<"\n                              |                                                                 |";
    cout<<"\n                              |____________________WELCOME TO BOOK PLUS PARKING_________________|";
    cout<<"\n                              |                                                                 |";
    cout<<"\n                              |-----------------------------------------------------------------|";
    cout<<"\n                              ===================================================================";

    cout<<"\n\n                              Please enter your name:";
    cin.getline (name, 30);
    cout<<"\n                              Please enter your IC number:";
    cin.getline (ic,20);

    cout<<"\n\n                               ===============================================================================================";
    cout<<"\n                               | DAY                    | TYPE OF VEHICLE           | PACKAGE PROVIDED           |PRICE (RM) |";
    cout<<"\n                               -----------------------------------------------------------------------------------------------";
    cout<<"\n                               | Weekday                | Car                       | A (1/2 hour)               | 5.50      |";
    cout<<"\n                               |                        |                           | B (3/4 hour)               | 8.30      |";
    cout<<"\n                               |                        |                           | C (5+ hour)                | 10.00     |";
    cout<<"\n                               -----------------------------------------------------------------------------------------------";
    cout<<"\n                               |                        | Motorcycle                | A (1/2 hour)               | 3.50      |";
    cout<<"\n                               |                        |                           | B (3/4 hour)               | 5.30      |";
    cout<<"\n                               |                        |                           | C (5+ hour)                | 8.00      |";
    cout<<"\n                               -----------------------------------------------------------------------------------------------";
    cout<<"\n                               | Weekend                | Car                       | A (1/2 hour)               | 6.50      |";
    cout<<"\n                               |                        |                           | B (3/4 hour)               | 9.30      |";
    cout<<"\n                               |                        |                           | C (5+ hour)                | 11.00     |";
    cout<<"\n                               -----------------------------------------------------------------------------------------------";
    cout<<"\n                               |                        | Motorcycle                | A (1/2 hour)               | 4.50      |";
    cout<<"\n                               |                        |                           | B (3/4 hour)               | 6.30      |";
    cout<<"\n                               |                        |                           | C (5+ hour)                | 9.00      |";
    cout<<"\n                               ===============================================================================================";

    cout<<"\n\n                              =====================================================================";
    cout<<"\n                              | TYPE OF VEHICLE                    | VEHICLE CODE                 |";
    cout<<"\n                              ---------------------------------------------------------------------";
    cout<<"\n                              | Car                                | 'C'                          |";
    cout<<"\n                              -------------------------------------------------------------------- ";
    cout<<"\n                              | Motorcycle                         | 'M'                          |";
    cout<<"\n                              =====================================================================";

    cout<<"\n\n                              Please enter day of booking:";
    cin.getline(day,10);
    cout<<"\n                                Please enter date of booking:";
    cin.getline (date,10);

    cout<<"\n                                Please enter vehicle code:";
    cin>>vehicle_code;
    cout<<"\n                                 Please choose package:";
    cin>>package;


    if (strcmp (day,"Monday")|| (day, "Tuesday")|| (day, "Wednesday")|| (day, "Thursday")|| (day,"Friday"))
    {
        if (vehicle_code == 'C')
        {
            if (package == 'A')
            {
                price = 5.50;
            }
            else if (package == 'B')
            {
                price = 8.30;
            }
            else if (package == 'C')
            {
                price = 10.00;
            }
            else 
            {
                cout<<"\n                      INVALID PACKAGE!";
            }
        }
        else if (vehicle_code == 'M')
        {
            if (package == 'A')
            {
                price = 3.50;
            }
            else if (package == 'B')
            {
                price = 5.30;
            }
            else if (package == 'C')
            {
                price = 8.00;
            }
            else
            {
                cout<<"\n                      INVALID PACKAGE!";
            }           
        }
        else 
        {
            cout<<"\n                           INVALID VEHICLE CODE!";
        }
    }
    else if (strcmp (day, "Saturday")|| (day,"Sunday"))
    {
        if (vehicle_code == 'C')
        {
            if (package == 'A')
            {
                price = 6.50;
            }
            else if (package == 'B')
            {
                price = 9.30;
            }
            else if (package == 'C')
            {
                price = 11.00;
            }
            else 
            {
                cout<<"\n                       INVALID PACKAGE!";
            }
        }
        else if (vehicle_code == 'M')
        {
            if (package == 'A')
            {
                price = 4.50;
            }
            else if (package = 'B')
            {
                price  = 6.30;
            }
            else if (package = 'C')
            {
                price = 9.00;
            }
            else 
            {
                cout<<"\n                       INVALID! PACKAGE!";
            }
        }
        else 
        {
            cout<<"\n                           INVALID VEHICLE CODE!";
        }
    }
    else 
    {
        cout<<"\n                               INVALID DAY!";
    }


    total = price;
    cout<<"\n                                  TOTAL PAYMENT (RM) :"<<total;
}

Recommended Answers

All 3 Replies

Can you please let us know what isn't working as expected. For example, if you enter your name IC number, day of booking, date of booking, vehicle code, package, etc. what values do you enter? Then, what does it say the total is? What do you expect it to be?

l.png

The only problem is I can't the right total price. In the screenshot, I insert the day of booking "Sunday" which is in weekend. After that, for the vehicle code I insert "M" which is stands for Motorcycle and for the package I choose C. The total price should display 9.00 but it display 8.00 which is the price on the weekday. I've tried checking where is the problem, but it seems like I can't detect the mistake.

These two lines

    if (strcmp (day,"Monday")|| (day, "Tuesday")|| (day, "Wednesday")|| (day, "Thursday")|| (day,"Friday"))

and

    else if (strcmp (day, "Saturday")|| (day,"Sunday"))

are both wrong. They should read

    if (strcmp (day,"Monday")|| strcmp (day, "Tuesday")|| strcmp (day, "Wednesday")|| strcmp (day, "Thursday")|| strcmp (day,"Friday"))

and

    else if (strcmp (day, "Saturday")|| strcmp (day,"Sunday"))

I'm surprised that it even compiled, to be honest. It seems likely that it worked only because of how the comma operator works, meaning it was interpreting it as:

    if (strcmp (day,"Monday")|| "Tuesday"|| "Wednesday"|| "Thursday"|| "Friday")

and since any non-empty string is equal to true, it would always use the first clause.

commented: After this, should we reveal that strcmp is unsafe? +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.