简体   繁体   中英

How could I code if-statement after switch-statement in C++?

I am writing a code to calculate the toll fee literally for my class assignment in C++. The code is shown below. My problem is that I only want to perform the calculation, ie, toll = vRate * distance if it meets any of the cases. If the input is invalid, by default, it will show "Invalid input" only and no calculation is required. For instance:

if !(default), 
    toll = vRate * distance;
    cout << "RM" <<toll << endl; 

But, I understand this would not work. Hence, any ideas in amending the code to make it clear cut and straightforward? Thank you in advanced.

#include <iostream>
using namespace std;
int main()
{
    char vCode;
    double vRate; // Toll rate for the vehicle
    double toll, distance;
    
    cout << "Enter vehicle code: " << endl;
    cin >> vCode; // C-Car, B-Bus, T-Truck, M-Motorbike
    cout << "Enter the distance travelled by the vehicle (in km): " << endl;
    cin >> distance;
    
    switch(vCode)
    {
        case 'C' : cout << "vRate = 50%" << endl;
        vRate = 0.5;
        break;
        case 'B' : cout << "vRate = 85%" << endl;
        vRate = 0.85;
        break;
        case 'T' : cout << "vRate = 100%" << endl;
        vRate = 1.00;
        break;
        case 'M' : cout << "Free toll" << endl;
        vRate = 0.00;
        break;
        default  : cout << "Invalid Vehicle Code" << endl;
    }
    toll = vRate * distance;
    cout << "RM" <<toll << endl; 
    return 0;
}

You can't check whether default case was used after the end of switch. You'll need to do something different to achieve result you want.

One thing you can do is to halt further processing in case you get to the default section

default: 
    cout << "Invalid Vehicle Code" << endl;
    return 0;

This will make program stop after outputting "Invalid Vehicle Code". That will work for your case, but if you had to do something else in your program after that, that wouldn't work.

Better way you can do it is to create additional variable isValid, that will be used to check whether input is valid after switch statement

bool isValid = true;
switch(vCode)
{
    case 'C' : cout << "vRate = 50%" << endl;
    vRate = 0.5;
    break;
    case 'B' : cout << "vRate = 85%" << endl;
    vRate = 0.85;
    break;
    case 'T' : cout << "vRate = 100%" << endl;
    vRate = 1.00;
    break;
    case 'M' : cout << "Free toll" << endl;
    vRate = 0.00;
    break;
    default  : cout << "Invalid Vehicle Code" << endl;
    isValid = false;
}
if (isValid)
{
    toll = vRate * distance;
    cout << "RM" <<toll << endl; 
}

And that's not the only ways to do it, it's just few ways how I would do it if it was assignment.

SIR CAN YOU HELP ME ON HOW TO REPEAT THAT PROGRAM FOR 25 CUSTOMER?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM