How do I restrict the amount of times a user can guess a number in C++?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, Im sure Im putting something wrong in the code. This is mainly testing something out, but this is what I have:
<pre>// NumberMatch2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
//Pick a random number//
int Number = 0;
srand(GetTickCount());
int i = rand()%10;
int NumberCount = 1;

//Prompt the user to guess a number between 1 and 100//
cout << "Can you guess which number Im thinking of? Hint: Its between 1 and 100 ;)" << endl;
cout << "Careful, You only have 5 guesses!" <<endl;



//do a while loop for each time the user guesses wrong//
while ((Number != i) || (NumberCount < 5));
{
cin >> Number;

if (Number < i)
{
cout << "Your guess was too low. Try again." <<endl;
}
else
{
cout << "Your guess was too high. Try again."<<endl;
}


}
//If the right answer was guessed, congratulate the user!//
if (Number == i)
{
cout << "Nice job, you guessed the number!" << endl;
}

if (NumberCount >= 5)
{
cout << "Sorry, you were unable to guess the correct number within 5 tries" <<endl;
}
return 0;
}[/code]
The program seems to run okay but the problem is that it wont allow me to type in any number. Just wondering how I can do this properly so that it only allows the user to guess 5 times and then end. Thank you

View the full article
 
Back
Top