A friend sent me a simpler form of the following code (didn't have the high/low check, while loop/max number of guesses) and I got bored and threw this together real quick. Problem is it's not working. I'm sure I did something monumentally stupid, anyone see my screwup? (Yes, I know it doesn't currently pick a random number).
Code:
#include <stdio.h>
#include <ctype.h>
main ()
{
int iRandom = (rand() % 10) + 1;
char cGuess = '\0';
int iGuess;
int count = 0;
while (count < 4)
{
printf("Guess a number 1 through 10: ");
scanf("%c", &cGuess);
count++;
if (!isdigit(cGuess))
printf("Invalid entry!\n");
else
{
iGuess = cGuess - '0';
if (iGuess > iRandom)
{
printf("You guessed too high.\n");
}
else if (iGuess < iRandom)
{
printf("You guessed too low.\n");
}
else
{
printf("You got it!\n");
break;
}
}
}
if (count < 4)
return;
else
printf("You lost! The answer was %d.\n", iRandom);
}
And the output...
Code:
Guess a number 1 through 10: 2
You guessed too low.
Guess a number 1 through 10: Invalid entry!
Guess a number 1 through 10: 3
You guessed too low.
You lost! The answer was 4.