Posted: Fri Oct 06, 2006 11:54 am Post subject: Stupid counting error
I'm not sure what is wrong with the code because sometimes it works perfectly, and other times it it completely off. I have to create a guessing game and at the end of the game I print how many guesses it took them to get the secret number. So I defined "iGuesses" as in int and set it equal to 0. I ask for the guess and tell them whether they need to go higher or lower, or if they got it.
I noticed the guesses were sometimes off by a large margin so I started to print the guess number at the beginning of the line when it asks for the guess. I've noticed that sometimes instead of starting at zero, it starts at 6, 11, 12, and about half the time 0...Any idea why it's doing this or do I need to post the code?
Sponsor Sponsor
wtd
Posted: Fri Oct 06, 2006 12:03 pm Post subject: (No subject)
It is almost always helpful to post the offending code.
Dan_
Posted: Fri Oct 06, 2006 12:09 pm Post subject: (No subject)
I'm going to try and post just the code that is involved with this function, and seeing as I'm new to C, I'm hopefully not going to miss anything! Alright, here it is
code:
// Declare variables
int iInput, iMaxNum, iNumProbs, iScore;
int iSecret, iGuess, iGuesses = 0;
srand(time(0)); // Start random number generator
else if (iInput == 2) {
printf("Enter the maximum number for the game.\n");
scanf("%d", &iMaxNum);
iSecret = 1 + rand()%iMaxNum; // Generate random number
while (iGuess != iSecret) {
printf("Enter your guess from 1 to %d.\n", iMaxNum);
scanf("%d", &iGuess);
if (iGuess == iSecret) {
++iGuesses;
printf("Congratulations! You guessed the secret number in %d guesses!\n", iGuesses);
}
else if (iGuess < iSecret) {
printf("You need to guess higher.\n");
printf("%d", iGuesses);
++iGuesses;
}
else {
printf("You need to guess lower.\n");
printf("%d", iGuesses);
++iGuesses;
}
} // End while statement
}
If I should just post the entire thing as a text attachment again, just say the words!
wtd
Posted: Fri Oct 06, 2006 12:25 pm Post subject: (No subject)
Please do post the whole thing.
Dan_
Posted: Fri Oct 06, 2006 1:00 pm Post subject: (No subject)
Alright, here it is. Just so I know in the future, should I always just supply the entire thing?
Posted: Fri Oct 06, 2006 1:12 pm Post subject: (No subject)
no
bugzpodder
Posted: Fri Oct 06, 2006 1:14 pm Post subject: (No subject)
no
Bug #1
Quote:
scanf("%d", iNumProbs);
when u suspect something is not working, comment everything else out and make sure it works. also output plenty of debugging things
bugzpodder
Posted: Fri Oct 06, 2006 1:15 pm Post subject: (No subject)
iGuess not initialized: while (iGuess != iSecret)
ur debugger should have complained
Sponsor Sponsor
Dan_
Posted: Fri Oct 06, 2006 1:20 pm Post subject: (No subject)
I didn't get any errors, and the iNumProbs I haven't finished working with yet. Right now I'm trying to make the iGuess work. Everything with iGuess is working fine, but the number of guesses is not tallying correctly.
md
Posted: Fri Oct 06, 2006 2:25 pm Post subject: (No subject)
To get all sorts of warnings about your code that otherwise don't get shown (and don't get shown by default) add -Wall when you compile.
I find that gcc is very good at finding things I've missed
Dan_
Posted: Fri Oct 06, 2006 2:32 pm Post subject: (No subject)
Is that an option somewhere or do I need to add "-Wall" to the code somewhere?
Thanks for the headsup
wtd
Posted: Fri Oct 06, 2006 2:41 pm Post subject: (No subject)
A couple of things that will generally help:
Properly format your code.
Use names like "number_of_guesses" rather than the inane "iGuesses."
Dan_
Posted: Fri Oct 06, 2006 2:45 pm Post subject: (No subject)
Will do, I figured a short name would be better, and also the lower case i, is because one of my friends who codes - but is rarely on - said it helps people identify the variable. So if it's an int put i, if it's a double put d, etc. Should I continue doing that?
wtd
Posted: Fri Oct 06, 2006 2:52 pm Post subject: (No subject)
Short variables names, eh? So optimally you'd call everything "a", "b", etc? Of course not. That's ridiculous. The compiler doesn't care. Your text editor doesn't care. Use meaningful variable names.
As for Hungarian notation (prefixing the name of the variable with the type), it is wholly worthless. Just use meaningful variable names.
md
Posted: Fri Oct 06, 2006 2:57 pm Post subject: (No subject)
As wtd says, use meaningful names. Clearly there is a point where it's too much, but num_guesses defininitely isn't it. Prefacing variables with i for intereger or d for double is a convention that some people find useful, and others find annoying. Personally I think it's a question of what you like. I used to use t for types and c for classes (I still occasionally do), but again, it's a personal choice.
As ofr -Wall... what IDE are you using (since you obviously are...)?