samedi 9 juillet 2016

else condition get's somehow bypassed when the user input has a value

So i'm trying to make a programm that repeatedly asks the user to guess a number. He then loses (or the programm quits) if one of these conditions are true:

  • user uses the number 5
  • user uses a number bigger then 10
  • user uses a number that is equal to the number of the guessed times he has already guessed
  • user has only 10 tries

Here is the problem, if I input the number 5 as my first guess the else function works and cout outputs the strings but if I first guess something else and then use the number 5 for some reason the program just quits without outputting the string in the last else block.

Here is were I would love to see some feedback on what I need to change so that I can input multiple user input and then still us the number 5 to get the cout strings in the else function. Thank you in advance :)

Here is the code:

#include <iostream>

using namespace std;

int main()  {

   int input = 0;
   short counter = 0;

   cout << "Hello fellow PC-User why don't you guess a number that is between 1-10 ?" << endl;
   cout << "Come on give it a try!\nOnly two conditions (maybe three) don't use the Number 5 and only Numbers 1-10!\nNow let's go!!!" << endl;
   cin >> input;



   if(input != 5 && input < 10){

      while(input != 5 && input < 10) {
         ++counter;
         cout << "Nice you guessed " << counter << flush;
         if (input > 1){
            cout <<" times!" << flush;
         } else{
            cout << " time!" << flush;
         }
         cout << "\nNow go at it again just don't used the number the times u tried bro!" << endl;
         cin >> input;
         if(counter == 10){
            cout << "Damn that motivation doe...well what can I say\n I give up \n U win." << endl;
            return 0;
         }       else if(counter == input){
            cout << "Don't do it..." << endl;
            return 0;
         }
      }
   } else {
      cout << "Yo! You weren't supposed to use that number!\nNow u looooose" << endl;
   }

   return 0;
}

Aucun commentaire:

Enregistrer un commentaire