
-----------------------------------
Dan_
Fri Oct 06, 2006 11:54 am

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?

-----------------------------------
wtd
Fri Oct 06, 2006 12:03 pm


-----------------------------------
It is almost always helpful to post the offending code.

-----------------------------------
Dan_
Fri Oct 06, 2006 12:09 pm


-----------------------------------
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

    // 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
Fri Oct 06, 2006 12:25 pm


-----------------------------------
Please do post the whole thing.

-----------------------------------
Dan_
Fri Oct 06, 2006 1:00 pm


-----------------------------------
Alright, here it is.  Just so I know in the future, should I always just supply the entire thing?

-----------------------------------
bugzpodder
Fri Oct 06, 2006 1:12 pm


-----------------------------------
no

-----------------------------------
bugzpodder
Fri Oct 06, 2006 1:14 pm


-----------------------------------
no

Bug #1

 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
Fri Oct 06, 2006 1:15 pm


-----------------------------------
iGuess not initialized: while (iGuess != iSecret) 

ur debugger should have complained

-----------------------------------
Dan_
Fri Oct 06, 2006 1:20 pm


-----------------------------------
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
Fri Oct 06, 2006 2:25 pm


-----------------------------------
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_
Fri Oct 06, 2006 2:32 pm


-----------------------------------
Is that an option somewhere or do I need to add "-Wall" to the code somewhere?

Thanks for the headsup

-----------------------------------
wtd
Fri Oct 06, 2006 2:41 pm


-----------------------------------
A couple of things that will generally help:

Properly format your code.
Use names like "number_of_guesses" rather than the inane "iGuesses."

-----------------------------------
Dan_
Fri Oct 06, 2006 2:45 pm


-----------------------------------
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
Fri Oct 06, 2006 2:52 pm


-----------------------------------
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
Fri Oct 06, 2006 2:57 pm


-----------------------------------
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...)?

-----------------------------------
Dan_
Fri Oct 06, 2006 3:11 pm


-----------------------------------

As for -Wall... what IDE are you using (since you obviously are...)?

I'm using DevC++

I'll go fix the variables to be more descriptive, but does anyone know why my guess number isn't always correct?

-----------------------------------
Dan_
Fri Oct 06, 2006 3:28 pm


-----------------------------------
Problem solved!  As I was testing it and trying to see why it was giving me the wrong number I noticed that if I played multiple times, my num_Guesses was holding the old guess number, so I moved the variable definition further down in the code and fixed it.

Thanks for all the help guys!

-----------------------------------
Null
Fri Oct 06, 2006 5:44 pm


-----------------------------------
I just want to add that as an alternative to names like numb_guesses,  names like NumbGuesses or numberSum also follow common convention.

Choose whichever you prefer, but once you choose a style, try to stick to it. :)

-----------------------------------
Dan_
Fri Oct 06, 2006 8:50 pm


-----------------------------------
Will do Null, I'm still trying to figure out my style. :lol:

-----------------------------------
bugzpodder
Mon Oct 09, 2006 5:31 pm


-----------------------------------
As for Hungarian notation (prefixing the name of the variable with the type), it is wholly worthless.  Just use meaningful variable names.

disagree.  while i dont support these naming styles, it sometimes helps to figure out what type a variable is easily if your compiler doesnt provide this functionality.

once I had.  for (i=0;i