Question:
Number guessing program, C++?
Dylan and Liz
2012-07-25 20:34:57 UTC
So I made a number guessing game for an assignment in my class. The program works and makes a random number and everything. The only problem is that after the first guess, it writes the correct number beside the dialog. I can't figure out how to make it not do that. My code is-

#include
#include
#include
using namespace std;

int main ()
{
unsigned short int randomNr;
unsigned short int numberGuess=1;
unsigned short int guessedNr=0;
const int maxNrOfGuess = 15;
char answer='y';
;


srand((unsigned)time(0));

randomNr= 1+(rand()%100);

while (answer=='y'){

numberGuess=1;
guessedNr=0;

cout << "Guess a number between 1 and 100 : ";
cin >> guessedNr;

while (numberGuess <= maxNrOfGuess && guessedNr != randomNr){
cout<
(guessedNr < randomNr) ? cout<<"Guess higher! " : cout<<"Guess lower! ";
cout<< maxNrOfGuess - numberGuess << " more guesses \n";
numberGuess++;
if (numberGuess <= maxNrOfGuess){
cout << "Make a guess (1-100) : ";
cin >> guessedNr;
}


}

if (numberGuess > maxNrOfGuess)
cout<< " You ran out of guesses and have lost the game.";
else
cout<< " You win!";

cout<<"\nWould you like to play again (y/n): ";
cin>> answer;
}

char f;
cin >> f;
}



So where am I messing up and how can I make it not give me the answer?
Three answers:
husoski
2012-07-25 20:53:26 UTC
Just remove the cout<< that outputs it.



while (numberGuess <= maxNrOfGuess && guessedNr != randomNr){

cout<
James Bond
2012-07-25 21:28:14 UTC
Remove the following after while loop statement

cout<
arkontaky
2016-12-13 14:07:32 UTC
i do no longer happen to have a C compiler reachable, yet i grants you the set of rules: decrease, greater, best=0,one million,2 def variety(){ return max - min } def mid(){ return min + variety() / 2 } min=0 max=two hundred correctGuess = fake jointly as !correctGuess{ int effect = wager(mid()) //person enter swap(effect){ case decrease: max = max - variety() / 2 wreck; case greater: min = min + variety() / 2 wreck; case best: go out("best wager is " + mid()) wreck; if(max == min){ go out("best wager is " + mid()) } } }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...