Instruction output for Coke Machine code

I've always used expressions if

else

over operators switch

, but I figured I wanted to try disconnecting. I made a basic Coke Machine program with a toggle switch and I can't figure out for a lifetime why it doesn't work the way it should. When I use the number 1-5 for my input, it keeps giving the switch default error message instead of cout case statements (eg "You selected Coke"). Obviously something must be wrong that I can't see /

#include <iostream>

using namespace std;

int main()
{
    int number;

    cout << "Beverage List" << endl;
    cout << "Coke = 1" << endl;
    cout << "Dr. Pepper = 2" << endl;
    cout << "Water = 3" << endl;
    cout << "Sprite = 4" << endl;
    cout << "Lemonade = 5" << endl << endl << endl;
    cout << "Enter a number to choose a beverage: ";
    cin >> number;

    switch (number)
    {
    case '1':
        cout << "You chose Coke";
        break;

    case '2':
        cout << "You chose Dr. Pepper";
        break;

    case '3':
        cout << "You chose Water";
        break;

    case '4':
        cout << "You chose Sprite";
        break;

    case '5':
        cout << "You chose Lemonade";
        break;

    default:
        cout << "Error: Choice was not valid. Here is your money back.";

    }

    cout << "\n";
    system("pause");
    return 0;
}

      

+3


source to share


2 answers


The character '1'

does not match the number 1

.

Edit

case '1':

      



to

case 1:

      

+2


source


It looks like your case arguments are comparing characters, not integers:

case '5':

      



Try this instead:

case 5:

      

+2


source







All Articles