Hey guys help with HiLo program
Hey guys im writing a simply HiLo game.
I cant get my do while statement to work correctly. I want it to stop running the program after you get the right answer. Right now everything works properly but it continues even after you get the right answer. Im using microsoft visual 2010. Also let me know if you see anything that is messy or I could have done easier. Mind that I am still new. I am reteaching my self after 1 semester of learning.
Also is there a way i can change the setting and have the program stay open without using the cin.clear(); cin.ignore(255, '\n'); cin.get(); at the end?
Code:
#include "stdafx.h"
#include <iostream>
#include <ctime> //for time
#include <cstdlib> //for rand()
using namespace std;
int nNum;
int nCheck;
int nTry;
unsigned int GetRandomNumber(int nLow, int nHigh)
{
srand(time(0));
nNum = (rand() % (nHigh - nLow +1) + nLow);
return nNum;
}
bool Check()
{
if (nCheck == nNum)
{
cout << "Correct!" << endl;
cout << "It took you " << nTry << " trys." << endl;
return 1;
}
else if (nCheck < nNum)
{
cout << "Too low try Again." << endl << endl;
return 0;
}
else if (nCheck > nNum)
{
cout << "Too high try again." << endl << endl;
return 0;
}
}
int main()
{
nNum = GetRandomNumber(1, 100);
do
{
for (nTry = 0; nTry < 10; nTry++)
{
cout << "You have " << 10 - nTry << " trys to guess a number 1-100: ";
cin >> nCheck;
Check();
}
}
while (Check() == 0);
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}