C++ Putting a character into a cin for a while loop causes the loop to execute outside of it : <p>I decided to try to make a little game. It's a simple survival game. Part of it has people try to survive as long as they can by choosing options. I've scaled down the game as much as possible from 1000 lines to this for the minimum case.</p>

<p>At one part of the game it asks, "You were visited by a gifting clown. On a scale of 0-10, how badly do you want a gift?"</p>

<p>If they answer 0-10, the loop works fine. If they answer with a character, like y or n, the loop basically forces the game to execute where it no longer asks players for input on any other choices.</p>

<p>A previous while loop works, one where it will continue so long as the player is alive. This clown while loop is nested inside of it. I have broken up the while loop with the clown section to where I think the problem is... And also included the full code just in case it's not inside there.</p>

<p>My goal is simply if a character is put into it, that it doesn't break this game. </p>

<p>main.cpp - clown section</p>

<pre><code>encounterHurt = 0;
randomEncounter = rand() % 8;
cin.ignore(1, '\n');
if (randomEncounter == 1 &amp;&amp; clown == true){
    encounterChoice = 1;
    cout &lt;&lt; "\n\nYou were visited by a gifting clown. \nOn a scale of 0-10, how badly do you want a gift? ";
    while (encounterChoice &gt;= 0 &amp;&amp; encounterChoice &lt;= 10){
        cin &gt;&gt; encounterChoice;

        encounterFood = (rand() % 3) + encounterChoice / 2;
        encounterWood = (rand() % 3) + encounterChoice / 2;
        encounterMedicine = (rand() % 2);
        encounterBullets = (rand() % 2);

        if (encounterChoice &gt; 0){
            encounterHurt = (rand() % 10) - encounterChoice;

            if (encounterHurt &lt;= 1){
    health--;
    cout &lt;&lt; "The crazy clown stabs you, but still provides gifts.";
            }
        }
        if (encounterFood &gt; 0) {
            cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterFood &lt;&lt; " food." &lt;&lt; endl;
            food += encounterFood;
        }
        if (encounterWood &gt; 0) {
            cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterWood &lt;&lt; " wood." &lt;&lt; endl;
            wood += encounterWood;
        }
        if (encounterMedicine &gt; 0) {
            cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterMedicine &lt;&lt; " medicine." &lt;&lt; endl;
            medicine += encounterMedicine;
        }
        if (encounterBullets &gt; 0) {
            cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterBullets &lt;&lt; " bullets." &lt;&lt; endl;
            bullets += encounterBullets;
        }
    encounterChoice = 11;
    }
</code></pre>

<p>main.cpp - Condensed code</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;random&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

using namespace std;

int main() {
    srand (time(NULL));
    int randNumber = 0;
    int food = 4;
    int wood = 4;
    int medicine = 2;
    int bullets = 8;
    int money = 25;
    int randomEncounter = 0;
    int hunt = 0;

    bool axe = false;
    int axeTemp = 0;
    int axeBonus = 0;
    int lumberTemp = 0;
    int lumber = 0;

    int findStore = 0;
    int storeChoice = 0;

    bool gun = false;
    int gunSearch;
    int gunTemp;
    int gunBonus = 0;
    int gunFind = 0;
//    int searches;
//    int searchesBonus;

    int farmFind = 0;
    int farmFood = 0;
    int farmSearch = 0;
    bool farm = false;
    string description;

    int foodTemp = 0;
    int woodTemp = 0;
    int medicineTemp = 0;
    int bulletsTemp = 0;
    int moneyTemp = 0;
    int huntTemp = 0;
    int huntSuccess = 0;
    char huntChoice;

    int encounterFood = 0;
    int encounterWood = 0;
    int encounterBullets = 0;
    int encounterMedicine = 0;
    int encounterHurt = 0;
    unsigned int encounterChoice = 0;

    int hurt = 0;
    int health = 3;
    int healthMax = 3;
    int days = 1;

    char action = '0';
    char pause = '1';
    char classChoice;
    char mainChoice;

    bool clown = true;
    int clownHealth = 5;
    char clownChoice;
    int yourShot = 0;
    int clownShot = 0;

    string place;
    //Food 1 per day per person. Can expand to include more people.
    //Fuel 1 per day, takes that much to stay warm even if fewer people
    //Medicine used one per wound
    //Bullets 1 to hunt, though can spend more to increase chance of success.
    //Days how many days that they have survied.
    //Health, everyone starts with three health. Good, okay, bad, dead.
    cout &lt;&lt; "\nFood: " &lt;&lt; food &lt;&lt; " Wood: " &lt;&lt; wood &lt;&lt; " Medicine: " &lt;&lt; medicine &lt;&lt; " Bullets: " &lt;&lt; bullets &lt;&lt; " Health: " &lt;&lt; health &lt;&lt; endl;

    while (health &gt; 0){
        cout &lt;&lt; "\nDay: " &lt;&lt; days;
        cout &lt;&lt; "\nFood: " &lt;&lt; food
             &lt;&lt; "\nWood: " &lt;&lt; wood
             &lt;&lt; "\nMedicine: " &lt;&lt; medicine
             &lt;&lt; "\nBullets: " &lt;&lt; bullets
             &lt;&lt; "\nHealth: " &lt;&lt; health
             &lt;&lt; "\nMoney: " &lt;&lt; money &lt;&lt; endl;

        if (food &gt;= 1){
            food--;
        }

        if (wood &gt;= 1){
            wood--;
        }

        if (food &lt;= 0){
            health--;
            cout &lt;&lt; "Health lost due to lack of food" &lt;&lt; endl;
        }

        if (health &lt; healthMax &amp;&amp; medicine &gt; 0){
            health++;
            medicine--;
            cout &lt;&lt; "Health pack used to heal your character\nHealth : " &lt;&lt; health &lt;&lt; endl;
        }


        action = '0';
        cout &lt;&lt; "\n1: Find food" &lt;&lt; endl;

        cout &lt;&lt; "What's your action? ";
        cin &gt;&gt; action;
        cout &lt;&lt; endl;

        if (action == '1'){
            //
            //Section for random sites to search.
            //
            //
        randNumber = rand() % 4;
        description = "";
    //Maybe + days at the end, and subtract some, so that they eventually run out of places to check.

        if (randNumber &gt;= 0 &amp;&amp; randNumber &lt; 2) {
            place = "supermarket";
            foodTemp = (rand() % 4) + 1;
            woodTemp = (rand() % 2) + 0;
            bulletsTemp = (rand() % 2) + 0;
            medicineTemp = (rand() % 2) + 1;
            moneyTemp = (rand() % 5) + 5;
            }

        if (randNumber &gt;= 2 &amp;&amp; randNumber &lt; 4) {
            place = "boat house";
            foodTemp = (rand() % 2) + 1;
            woodTemp = (rand() % 4) + 1;
            bulletsTemp = (rand() % 2) + 0;
            medicineTemp = (rand() % 2) + 0;
            moneyTemp = (rand() % 3) + 0;
            }

    cout &lt;&lt; "You have come to the " &lt;&lt; place &lt;&lt; "." &lt;&lt; endl;
    cout &lt;&lt; description &lt;&lt; endl;
    food += foodTemp;
    wood += woodTemp;
    bullets += bulletsTemp;
    medicine += medicineTemp;
    money += moneyTemp;

    if (foodTemp &gt; 0)
        cout &lt;&lt; "You have found " &lt;&lt; foodTemp &lt;&lt; " food." &lt;&lt; endl;
    if (woodTemp &gt; 0)
        cout &lt;&lt; "You have found " &lt;&lt; woodTemp &lt;&lt; " wood." &lt;&lt; endl;
    if (medicineTemp &gt; 0)
        cout &lt;&lt; "You have found " &lt;&lt; medicineTemp &lt;&lt; " medicine." &lt;&lt; endl;
    if (bulletsTemp &gt; 0)
        cout &lt;&lt; "You have found " &lt;&lt; bulletsTemp &lt;&lt; " bullets." &lt;&lt; endl;
    if (moneyTemp &gt; 0)
        cout &lt;&lt; "You have found " &lt;&lt; moneyTemp &lt;&lt; " money." &lt;&lt; endl;

    cout &lt;&lt; "\nFood: " &lt;&lt; food &lt;&lt; " Wood: " &lt;&lt; wood &lt;&lt; " Medicine: " &lt;&lt; medicine &lt;&lt; " Bullets: " &lt;&lt; bullets
         &lt;&lt; " Health: " &lt;&lt; health &lt;&lt; " Money: " &lt;&lt; money &lt;&lt; endl;

            //End of search rooms.
        }

//Random encounter chance to see if they can gain additional items.
        encounterHurt = 0;
        randomEncounter = rand() % 8;
        cin.ignore(1, '\n');
        if (randomEncounter == 1 &amp;&amp; clown == true){
            encounterChoice = 1;
            cout &lt;&lt; "\n\nYou were visited by a gifting clown. \nOn a scale of 0-10, how badly do you want a gift? ";
            while (encounterChoice &gt;= 0 &amp;&amp; encounterChoice &lt;= 10){
    cin &gt;&gt; encounterChoice;

    encounterFood = (rand() % 3) + encounterChoice / 2;
    encounterWood = (rand() % 3) + encounterChoice / 2;
    encounterMedicine = (rand() % 2);
    encounterBullets = (rand() % 2);

    if (encounterChoice &gt; 0){
        encounterHurt = (rand() % 10) - encounterChoice;

        if (encounterHurt &lt;= 1){
            health--;
            cout &lt;&lt; "The crazy clown stabs you, but still provides gifts.";
        }
    }
    if (encounterFood &gt; 0) {
        cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterFood &lt;&lt; " food." &lt;&lt; endl;
        food += encounterFood;
    }
    if (encounterWood &gt; 0) {
        cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterWood &lt;&lt; " wood." &lt;&lt; endl;
        wood += encounterWood;
    }
    if (encounterMedicine &gt; 0) {
        cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterMedicine &lt;&lt; " medicine." &lt;&lt; endl;
        medicine += encounterMedicine;
    }
    if (encounterBullets &gt; 0) {
        cout &lt;&lt; "\nYou were provided with " &lt;&lt; encounterBullets &lt;&lt; " bullets." &lt;&lt; endl;
        bullets += encounterBullets;
    }
            encounterChoice = 11;
            }


//Option to attack clown
//
//
  }
        //End of random encounter from the clown.


//Pause mechanic to prevent the game from cycling.
//            pause = 'b';
//            while (pause != 'a'){
//                cout &lt;&lt; "\nEnter a to continue: ";
//                cin &gt;&gt; pause;
//            }

//End of game message
            cout &lt;&lt; endl;
            if (days == 100){
    cout &lt;&lt; "You have made it to 100 days. You have beaten this game. You can quit now, or try to see how long you'll last." &lt;&lt; endl;
            }

//Add day at end of while loop.
            days++;
        }
    cout &lt;&lt; "You have died after " &lt;&lt; days &lt;&lt; " days" &lt;&lt; endl;
}
</code></pre>
