0

There are a couple of questions similar to this one, however they cover another usage of cin.

#include <iostream>

int main(int argc, char *argv[]) {

int coke = 1, sprite = 2, water = 3, fanta = 4, sevenup = 5, choice;

while (true){
    system("cls");
    std::cout << "Choose your drink:\n1) Coke\n2) Sprite\n3) Water\n4) Fanta\n5) SevenUp\n\n";

    std::cin >> choice;

    switch (choice){
    case 1:
        std::cout << "You chose a Coke.";
        break;
    case 2:
        std::cout << "You chose a Sprite.";
        break;
    case 3:
        std::cout << "You chose a Water.";
        break;
    case 4:
        std::cout << "You chose a Fanta.";
        break;
    case 5:
        std::cout << "You chose a SevenUp.";
        break;
    default:
        std::cout << "No choice like that. Here's your money back.";
        break;
    }

    while (true){
        std::cout << "Would you like to buy another beverage? (y/n)";
        char answer1;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        //***********************************************
        //THIS IS WHERE IT HAPPENS
        std::cin >> answer1;
        //THIS IS WHERE IT HAPPENS
        //***********************************************
        if (answer1 == 'y'){
            break;
        }
        if (answer1 == 'n'){
            //end game
            return 0;
        }
    }
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;

}

I commented the location "of failure" in the code above. This program asks the user for an input of 1-5. If the input is greater than 5, it requests another input. However if the user inputs a letter, not a number. The "location of failure" is simply skipped and the loop executed without ever requesting another input.

0 Answers0