Computer Science Canada

Stupid counting error

Author:  Dan_ [ 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?

Author:  wtd [ Fri Oct 06, 2006 12:03 pm ]
Post subject: 

It is almost always helpful to post the offending code.

Author:  Dan_ [ Fri Oct 06, 2006 12:09 pm ]
Post 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!

Author:  wtd [ Fri Oct 06, 2006 12:25 pm ]
Post subject: 

Please do post the whole thing.

Author:  Dan_ [ Fri Oct 06, 2006 1:00 pm ]
Post subject: 

Alright, here it is. Just so I know in the future, should I always just supply the entire thing?

Author:  bugzpodder [ Fri Oct 06, 2006 1:12 pm ]
Post subject: 

no

Author:  bugzpodder [ Fri Oct 06, 2006 1:14 pm ]
Post 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

Author:  bugzpodder [ Fri Oct 06, 2006 1:15 pm ]
Post subject: 

iGuess not initialized: while (iGuess != iSecret)

ur debugger should have complained

Author:  Dan_ [ Fri Oct 06, 2006 1:20 pm ]
Post 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.

Author:  md [ Fri Oct 06, 2006 2:25 pm ]
Post 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 Wink

Author:  Dan_ [ Fri Oct 06, 2006 2:32 pm ]
Post subject: 

Is that an option somewhere or do I need to add "-Wall" to the code somewhere?

Thanks for the headsup

Author:  wtd [ Fri Oct 06, 2006 2:41 pm ]
Post subject: 

A couple of things that will generally help:

Properly format your code.
Use names like "number_of_guesses" rather than the inane "iGuesses."

Author:  Dan_ [ Fri Oct 06, 2006 2:45 pm ]
Post 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?

Author:  wtd [ Fri Oct 06, 2006 2:52 pm ]
Post 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.

Author:  md [ Fri Oct 06, 2006 2:57 pm ]
Post 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...)?

Author:  Dan_ [ Fri Oct 06, 2006 3:11 pm ]
Post subject: 

md wrote:

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?

Author:  Dan_ [ Fri Oct 06, 2006 3:28 pm ]
Post subject: 

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!

Author:  Null [ Fri Oct 06, 2006 5:44 pm ]
Post subject: 

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. Smile

Author:  Dan_ [ Fri Oct 06, 2006 8:50 pm ]
Post subject: 

Will do Null, I'm still trying to figure out my style. Laughing

Author:  bugzpodder [ Mon Oct 09, 2006 5:31 pm ]
Post subject: 

wtd wrote:
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<v.size()-1;i++) ...
well guess what? v.size() is an unsigned int, so 0 - 1 get casted to 0xFFFFFFFF and i get an infinite loop when the vector is empty. So if someone wrote code like:

iSize = v.size() - 1;
for (i=0;i<iSize;i++) ...
then when debugging it might have jumped out, than something like

numIterations = v.size() - 1;


: