Anyway, after compiling it, I see your problem. It doesn't "crash", but enter an infinite loop somewhere. It's running a loop over and over again.
Note that you only ask for user input once, not six times as you should have.
And you generate randomNumber() over and over again.
Worst of all is this typo. Find it:
Code:
for(i = 0; 1 < 1000; i++)
A simple typo can mess up an entire program.
Besides that, your test() function should test for invalid values first. Otherwise they are never tested; the function only prints "Too Low" or "Too High" no matter what invalid values you enter.
This does not produce a number between 1 and 100, but a number between 0 and 999:
Code:
int randomNumber()
{
int number =-99999;
number=((rand()+101) % 1000);
return number;
}
Because the "%" operator will only return a number between 0 and 999.
Code:
int randomNumber()
{
int number =-99999;
number=rand() % 1000 + 1;
return number;
}
Note that you can initialize variables in loops, unlike in C:
Tip: the correct code snippet for main() should resemble
Code:
rnum = randomNumber();
for(int i = 0; i < 6; i++)
{
cin >> yourGuess;
if(test(yourGuess, rnum))
{
break;
win = 1;
}
}
if(win)
{
cout << "You Guessed it!\n";
}
else
{
cout << "You Lose!\n";
}