Guess my number c++ while loop inside a loop

program to guess a number
word guessing game in c programming
c program for guessing a number
guess my number program
number guessing game c++
random number game
how to store random numbers in an array c
srand in c

I have this simple guess my number code as I am learning C++. As you can see it selects a random number and lets the user guess it and records the number of tries.

int main(){
   srand(static_cast<unsigned int>(time(0))); // seed number generator

   int secretNumber = rand() % 100 + 1;
   int tries = 0;
   int guess;

   cout << "\tWelcome to Guess my Number\n\n";
   do{
    cout << "Enter a guess: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumber){
        cout << "Too high\n";
    } else if (guess < secretNumber){
        cout << "Too low \n";
    } else {
        cout << "You got it :) in " << tries << " tries \n\n";
    }
   } while(guess != secretNumber);
}

While this is working correctly, I also want user to have only 5 tries. I can think of adding another else if statement to ensure that (tries < 6).

OR

I can create another while loop inside do and ensure that (tries < 6).

What would be the best approach here?


you can add a condition to your do-while-loop

int main(){
   srand(static_cast<unsigned int>(time(0))); // seed number generator

   int secretNumber = rand() % 100 + 1;
   int tries = 0;
   int guess;

   cout << "\tWelcome to Guess my Number\n\n";
   do{
    cout << "Enter a guess: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumber){
        cout << "Too high\n";
    } else if (guess < secretNumber){
        cout << "Too low \n";
    } else {
        cout << "You got it :) in " << tries << " tries \n\n";
    }
   } while((guess != secretNumber) && (tries < 6));
}

C program to guess a random number, This is just like a small game program in which user has to guess correct number which is generated randomly. Here program will give 7 attempts to guess the� Guess My Number Game Enter a guess between 1 and 100 : 60 Too low! Enter a guess between 1 and 100 : 80 Too low! Enter a guess between 1 and 100 : 95


Another option would be to use a for loop and break on the correct answer:

int main(){
   srand(static_cast<unsigned int>(time(0))); // seed number generator

   int secretNumber = rand() % 100 + 1;

   cout << "\tWelcome to Guess my Number\n\n";
   for ( int tries = 1; tries < 6; tries++ ) {
    int guess;
    cout << "Enter a guess: ";
    cin >> guess;

    if (guess > secretNumber){
        cout << "Too high\n";
    } else if (guess < secretNumber){
        cout << "Too low \n";
    } else {
        cout << "You got it :) in " << tries << " tries \n\n";
        break;
    }
   }
}

"Guess the Number" game in C, Here are some of the improvements i can suggest: move your counter variable , from the inside the if statements , as increasing the count is� This is my first program in C. I'd rather not form any bad habits now. Is there anything that looks like bad practice, or something just looks wrong? #include <stdio.h&gt; #include &lt;stdlib.h


I just did some "trial and error" and also added another loop to "play again". Here's the code.

int main(){
   char again = 'y';
   do{
       srand(static_cast<unsigned int>(time(0))); // seed number generator
       int secretNumber = rand() % 100 + 1;
       int tries = 0;
       int guess;

       cout << "\tWelcome to Guess my Number\n\n";
       do{
        cout << "Enter a guess: ";
        cin >> guess;
        ++tries;

        if (guess > secretNumber){
            cout << "Too high\n";
        } else if (guess < secretNumber){
            cout << "Too low \n";
        } else {
            cout << "You got it :) in " << tries << " tries \n\n";
        }
       } while((guess != secretNumber) && (tries < 6));
        cout << "Too many tries \n\n";
        cout << "Start again?: ";
        cin >> again;
   } while(again == 'y');
 }

I am an advocate of optimizing the code as much as possible to make it faster and use less system resources. Can someone please review if I can tweak it further to optimize it?

Computer Guess my number C Programing, this line: computerGuess = ((high - low) + low)/2;. should be: computerGuess = ( high - low)/2+low;. What you are looking for is the number half� Question: Write a C program that plays the game of "Guess the Number" as follows: Your program should choose a number to be guessed by selecting an integer at random in the range of 1 to 1000.


I am not sure if there is the best approach. As you said there are 2 possibilities either add condition in while

while(guess != secretNumber && tries < 6);

or you can go with

else if(tries > 5)
{
    cout << "You are out of tries, better luck next time ;) \n"
    break;
}

I would prefer the one with adding condition to while. But this is my personal opinion. I meet programmer that wants to do the else if and try to keep loop condition relatively simple. With more complex if/boolean statements you can extract a function with proper name. In your case it can be done like this

 bool isEndGame(int guess, int secretNumber, int tries)
 {
     return (guess != secretNumber) && (tries < 6);
 }

 int main()
 {
   srand(static_cast<unsigned int>(time(0))); // seed number generator

   int secretNumber = rand() % 100 + 1;
   int tries = 0;
   int guess;

   cout << "\tWelcome to Guess my Number\n\n";
   do{
    cout << "Enter a guess: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumber){
        cout << "Too high\n";
    } else if (guess < secretNumber){
        cout << "Too low \n";
    } else {
        cout << "You got it :) in " << tries << " tries \n\n";
    }
   } while (isEndGame(guess, secretNumber, tries));
}

In my opinion the last one is the best choice. You don't need to think about condition and other programmers can clearly now what it means.

Guess the number, { p "Guess again!" } } C[edit]. #include <stdlib.h> #� Guess the number to unlock the phone and see the funny picture! Guess the Number is a fun educational game that challenges kids to find a number based on greater than or less than feedback. There are three different levels of play that vary the range of numbers.


C++ Number Guessing Game, Write a C++ program to implement the Number Guessing Game. In this game the computer chooses a random number between 1 and 100, and the player tries� Also, you should be decrementing the number of chances the user has when they guess instead of incrementing. – Asad Saeeduddin Jul 18 '17 at 18:49 That looks like a great game but maybe this question fits more to Stack Overflow Code Review – Yonlif Jul 18 '17 at 18:49


Guessing Game in C, Only 8 chances to guess the randomly generated number."); printf("\n2. You can WIN the game when the number guessed is same as the randomly generated� Write a C++ program to implement the Number Guessing Game. In this game the computer chooses a random number between 1 and 100, and the player tries to guess the number in as few attempts as possible. Each time the player enters a guess, the computer tells him whether the guess is too high, too low, or right.


Guess the Number.C problem, Im a newbie programmer on C. And I have an error on my Guess the random number game. Can you please help me fix this? When you got the� If you guess the right number you will win otherwise you will lose. This game is also known as guess the number game in C++. Let’s begin our tutorial. Algorithm: First of all, we will generate a random number using rand() function in C++. Then we will take input from the user if input matches to the random number, the user will win and the