#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Newbie and Guess the Number Script


    Hello all,

    I'm a beginner at C, and my professor assigned us the good ole "Guess the Number" problem. My code so far is below. I can't figure out why when I try to have more than 2 scanf's the script passes over the second and goes on and on. I also know that there are 2 instances where I have a identical scanf's on the same line (this is the only way I could stop the 'looping' effect). Any help would be wonderful. Thanks in advance.

    #include <stdio.h>
    #define CHOSEN_NUM 50

    int calcGuess(int,int);
    char userAcceptGameAgain(char);
    int errorMsg(int);

    int userGuess, checkIt, errorNum, calcGuessByPass;
    char userAgain;

    int main(void) {
    printf("I'm thinking of a number between 1 and 100\n");
    printf("Want to try and guess it? Sure you do...Take a stab at it: ");
    checkIt = scanf("%d", &userGuess);
    calcGuess(userGuess,checkIt);
    return 0;
    }

    int calcGuess(int userGuess, int calcGuessByPass) {
    if (calcGuessByPass == 1) {
    if (userGuess < CHOSEN_NUM) {
    printf("Sorry...your number is too low. Try again ");
    checkIt = scanf("%d", &userGuess);
    calcGuess(userGuess,checkIt);
    } else if (userGuess > CHOSEN_NUM) {
    printf("Sorry...your number is too high. Try again ");
    checkIt = scanf("%d", &userGuess);
    calcGuess(userGuess,checkIt);
    } else {
    printf("\nWonderful...You beat me!");
    printf("\nWould you like to try again? (Y/N) ");
    scanf("%c", &userAgain);scanf("%c", &userAgain);
    userAcceptGameAgain(userAgain);
    }
    } else {
    errorMsg(0);
    }
    return 0;
    }

    char userAcceptGameAgain(char userAgain) {
    if (userAgain == 'Y' || userAgain == 'y') {
    printf("\nThat's great. I'm gonna beat you this time.");
    printf("\nHit me with a number: ");
    checkIt = scanf("%d", &userGuess);
    calcGuess(userGuess,checkIt);
    } else if (userAgain == 'N' || userAgain == 'n') {
    printf("\nFine, have it your way. Good bye!\n\n");

    } else {
    errorMsg(1);
    }
    return 0;
    }

    int errorMsg(int errorNum) {
    if (errorNum == 0) {
    printf("Oops...you didn't give me a number. Tsk tsk. Try again ");
    checkIt = scanf("%d", &userGuess);
    calcGuess(userGuess,checkIt);
    } else if (errorNum == 1) {
    printf("What kind of 'Yes' or 'No' is that? Is it 'Y' or 'N'?");
    scanf("%c", &userAgain);scanf("%c", &userAgain);
    userAcceptGameAgain(userAgain);
    } else {}
    return 0;
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Please repost with your code in "code" tags (see http://forums.devshed.com/misc.php?action=bbcode&s=).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Reposting with code tags


    Code:
    #include <stdio.h>
    #define CHOSEN_NUM 50
    
    int calcGuess(int,int);
    char userAcceptGameAgain(char);
    int errorMsg(int);
    
    int userGuess, checkIt, errorNum, calcGuessByPass;
    char userAgain;
    
    int main(void) {
    	printf("I'm thinking of a number between 1 and 100\n");
    	printf("Want to try and guess it? Sure you do...Take a stab at it:  ");
    	checkIt = scanf("%d", &userGuess);
    	calcGuess(userGuess,checkIt);
    	return 0;
    }
    
    int calcGuess(int userGuess, int calcGuessByPass) {
    	if (calcGuessByPass == 1) {
    		if (userGuess < CHOSEN_NUM) {
    			printf("Sorry...your number is too low. Try again  ");
    			checkIt = scanf("%d", &userGuess);
    			calcGuess(userGuess,checkIt);
    		} else if (userGuess > CHOSEN_NUM) {
    			printf("Sorry...your number is too high. Try again  ");
    			checkIt = scanf("%d", &userGuess);
    			calcGuess(userGuess,checkIt);
    		} else {
    			printf("\nWonderful...You beat me!");
    			printf("\nWould you like to try again? (Y/N)  ");
    			scanf("%c", &userAgain);scanf("%c", &userAgain);
    			userAcceptGameAgain(userAgain);
    		}
    	} else {
    		errorMsg(0);
    	}
    	return 0;
    }
    
    char userAcceptGameAgain(char userAgain) {
    	if (userAgain == 'Y' || userAgain == 'y') {
    		printf("\nThat's great. I'm gonna beat you this time.");
    		printf("\nHit me with a number:  ");
    		checkIt = scanf("%d", &userGuess);
    		calcGuess(userGuess,checkIt);
    	} else if (userAgain == 'N' || userAgain == 'n') {
    		printf("\nFine, have it your way. Good bye!\n\n");
    
    	} else {
    		errorMsg(1);
    	}
    	return 0;
    }
    
    int errorMsg(int errorNum) {
    	if (errorNum == 0) {
    		printf("Oops...you didn't give me a number. Tsk tsk. Try again  ");
    		checkIt = scanf("%d", &userGuess);
    		calcGuess(userGuess,checkIt);
    	} else if (errorNum == 1) {
    		printf("What kind of 'Yes' or 'No' is that? Is it 'Y' or 'N'?");
    		scanf("%c", &userAgain);scanf("%c", &userAgain);
    		userAcceptGameAgain(userAgain);
    	} else {}
    	return 0;
    }
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Look up scanf(). You are using it incorrectly! Hint: checkIt is not what you think it is.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Hmm...


    Hmm..not sure what you mean that I'm using scanf incorrectly, and I have used the " checkIt = scanf("%d", &var); " before and it worked fine.

    From my experience, checkIt will return a 1 if scanf is a number (when using %d) and return a 0 if not a number.

    Can you please be a little more specific which scanf is incorrect and why checkIt is not what I think it is?

    Much appreciation.
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    My bad, I read your code too fast. I always use a buffer for user input to avoid the exact same problems you are having. When you use buffered input (the default, I don't even remember how to bypass it), you get the full line upto the user's 'Enter' (be it charriage return, linefeed, or both). You are not factoring that in, so your next read has to accomodate that. You can either do a getc() or do what I did below with a buffer:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define CHOSEN_NUM 50
    #define BUF 1024
    
    char inputBuf[BUF];
    
    int calcGuess(int);
    char userAcceptGameAgain(char);
    int errorMsg(int);
    
    int userGuess, checkIt, errorNum, calcGuessByPass;
    char userAgain;
    
    int main(void) {
        printf("I'm thinking of a number between 1 and 100\n");
        printf("Want to try and guess it? Sure you do...Take a stab at it:  ");
        fgets(inputBuf, BUF, stdin);
    
        calcGuess(atoi(inputBuf));
        return 0;
    }
    
    int calcGuess(int userGuess) {
        if (userGuess > 0) {
            if (userGuess < CHOSEN_NUM) {
                printf("Sorry...your number is too low. Try again  ");
                fgets(inputBuf, BUF, stdin);
                calcGuess(atoi(inputBuf));
            } else if (userGuess > CHOSEN_NUM) {
                printf("Sorry...your number is too high. Try again  ");
                fgets(inputBuf, BUF, stdin);
                calcGuess(atoi(inputBuf));
            } else {
                printf("\nWonderful...You beat me!");
                printf("\nWould you like to try again? (Y/N)  ");
                fgets(inputBuf, BUF, stdin);
                userAcceptGameAgain(inputBuf[0]);
            }
        } else {
            errorMsg(0);
        }
        return 0;
    }
    
    char userAcceptGameAgain(char userAgain) {
        if (userAgain == 'Y' || userAgain == 'y') {
            printf("\nThat's great. I'm gonna beat you this time.");
            printf("\nHit me with a number:  ");
            fgets(inputBuf, BUF, stdin);
            calcGuess(atoi(inputBuf));
        } else if (userAgain == 'N' || userAgain == 'n') {
            printf("\nFine, have it your way. Good bye!\n\n");
        } else {
            errorMsg(1);
        }
        return 0;
    }
    
    int errorMsg(int errorNum) {
        if (errorNum == 0) {
            printf("Oops...you didn't give me a number. Tsk tsk. Try again  ");
            fgets(inputBuf, BUF, stdin);
            calcGuess(atoi(inputBuf));
        } else if (errorNum == 1) {
            printf("What kind of 'Yes' or 'No' is that? Is it 'Y' or 'N'?");
            fgets(inputBuf, BUF, stdin);
            userAcceptGameAgain(inputBuf[0]);
        } else {}
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Yee Haw


    Thank you so much. I completely forgot (or maybe never knew fully) that the newline and zero-character are returned within a scanf and that was one crucial problem. I'm going to re-work using getchar like you had mentioned before and see how that pans out in addition to the fgets.

    Muchos Gracias! :)

IMN logo majestic logo threadwatch logo seochat tools logo