0

I am fully aware that switch must be used with int, but my assignment is requiring me to use switch in regards to user input which will be strings.

I've looked and I've seen some mentioning stoi, but I'm not sure if that is what my professor is expecting, because we have not been introduced to it yet. I'm completely new to C++, so I'm still learning and this code is not completed yet, but how can I do it?

int main()
{
    // Declare Constant variables
    const float DISC_GOLF_RETAIL = 14.96;
    const float ULTIMATE_RETAIL = 20.96;
    const double DISCOUNT1 = 8;
    const float DISCOUNT2 = .16;
    const float DISCOUNT3 = .24;
    const float DISCOUNT4 = .32;
    const double DEC = 100;

    // Declare variables
    double quantity;
    double pricePerDisc;
    double totalSavings;
    double total;
    char userInput;
    float discount;
    string discType;
    string disc1 = "Ultimate Disc";
    string disc2 = "Disc-Golf Disc";


    // Display title
    cout << "Welcome to the Flying-Disc Shop!" << "\n" << endl;

    // Prompt the user for input
    cout << "Enter 'u' for ultimate discs and 'g' for disc golf: ";
    cin >> (userInput);
    cout << endl;

    switch (discType)
    {
        case 1: userInput == 'u' || 'U';
            discType = disc1;
            pricePerDisc = ULTIMATE_RETAIL;
            break;

        case 2: userInput == 'g' || 'G';
            discType = disc2;
            pricePerDisc = DISC_GOLF_RETAIL;
            break;

        default:
            cout << "Invalid disc type." << endl;
            return 0;
    }

    cout << "Enter the number of Ultimate Disc(s): ";
    cin >> (quantity);
    cout << endl;

    cout << "------------Receipt------------" << endl;

    if (quantity > 5 && quantity <= 9)
    {
        discount = DISCOUNT1 / DEC;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity > 10 && quantity <= 19)
    {
        discount = DISCOUNT2;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity > 20 && quantity <= 29)
    {
        discount = DISCOUNT3;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity >= 30)
    {
        discount = DISCOUNT4;
    }

    totalSavings = (pricePerDisc * quantity) * discount;
    total = quantity * pricePerDisc - totalSavings;
    cout << "Disc Type: " << discType << endl;
    cout << "Quantity: " << quantity << endl;
    cout << "Pricer per Disc: " << "$ " << fixed << setprecision(2)
    << pricePerDisc << endl;
    cout << "Total Savings: " << "$ " << "-" << fixed << setprecision(2)
    << totalSavings << endl;
    cout << "Total: " << "$ " << total << fixed << setprecision(2) << endl;
}
5
  • 1
    Off-topic: Use tolower() or toupper() after receiving a single character. This will eliminate half of your comparisons. Commented Sep 12, 2022 at 23:11
  • 1
    Try this: switch (discType) { case 'u': /*...*/ case 'g': /*...*/}; Commented Sep 12, 2022 at 23:13
  • You may be calculating double the total savings. You have the calculation inside the if-else-if ladder and after the ladder. I recommend removing the totalSavings calculation from inside all the if statements. Commented Sep 12, 2022 at 23:15
  • FWIW a string can be hashed to an integer value and used in a switch statement, but that’s probably beyond your assignment Commented Sep 13, 2022 at 4:00
  • This must be a duplicate. What is the canonical question? Commented Feb 23, 2023 at 0:43

1 Answer 1

6

For single char responses, you can use the switch/case statement:

switch (userInput)
{
    case 'g':
    case 'G': 
       /* ... */
       break;
    case 'u':
    case 'U': 
       // ...
       break;
    default:
       // ...
       break;
}

A single character can be treated differently than a string. A string is usually consists of more than one character.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion in regards to the totalSavings also, didn't even realize
Addendum: switch only works on integer types. char is an integer type. You can fake out the behaviour with a map/unordered_map mapping strings to the function to be run when the string is received.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.