Making Multiple Comparisons in One Big Block - C++ Statement

C++ examples for Statement:switch

Description

Making Multiple Comparisons in One Big Block

Demo Code

#include <iostream>
#include <conio.h>

using namespace std;

int main()//  w  ww  . j a va 2s.co  m
{
    cout << "1. Apples " << endl;
    cout << "2. Bananas " << endl;
    cout << "3. Fried worms " << endl;
    cout << "4. Poison Apples " << endl;
    cout << "5. Lobster " << endl;

    // Obtain the user's selection.
    char ch = getch();

    while (ch < '1' || ch > '5'){
        ch = getch();
    }

    cout << "You chose " << ch << endl;
    switch (ch) {
    case '1':
        cout << "Apples are good for you!" << endl;
        break;
    case '2':
        cout << "Bananas have plenty of potassium!" << endl;
        break;
    case '3':
        cout << "That's disgusting!" << endl;
        break;
    case '4':
        cout << "All I wanna know is WHY?" << endl;
        break;
    case '5':
        cout << "Expensive but good taste you have!" << endl;
        break;
    }

    return 0;
}

Result


Related Tutorials