Wanna play again?

This is a discussion on Wanna play again? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { char answer; int guess; int tries; int number; srand(time(0)); number=rand()%20; ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Wanna play again?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    
    char answer;
    int guess;
    int tries;
    int number;
    
    srand(time(0));
    number=rand()%20;
    
    printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
    printf("Enter your first guess!\n");
    scanf("%d", &guess);
       if (guess > 20)
          printf("That guess is over 20!\n");
    
       for (tries=1; guess != number;tries++)
          {
             if (guess > number)
                printf("Wrong! %d is greater than the secret number!\n",guess);
    
             if (guess < number)
                printf("Wrong! %d is less than the secret number!\n",guess);
    
             printf("Enter another guess!\n");
             scanf("%d", &guess);
    
             if (guess > 20)
                printf("That guess is over 20!\n");
    
          }
    
    if(tries ==1)
       printf("Amazing! You got it on your first guess!\n");
    
    if(tries ==2)
       printf("Excellent! It took you only 2 tries!\n");
    
    if(tries ==3)
       printf("Good job! It took you 3 tries\n!");
    
    if(tries ==4)
       printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");
    
    if (tries >=5)
       printf("Not bad! It took 5 or more guesses!\n");
    
    return 0;
    }
    I made this guessing game program. I want it to ask the user after he/she gets the correct answer if they want continue or play again... then the user will select an answer Y for yes, N for no, and the game will loop again. I know it's simple but I can't seem to get it!!! I've been trying for a while now! HELP!

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    user_wants_to_play = true;
    while( user_wants_to_play )
    {
       PlayGame();
       AskUserIfTheyWantToPlayAgain();
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       for ( ;; )
       {
          int c;
          /* ... main body of program ... */
          fputs("Continue? [y/n] ", stdout);
          fflush(stdout);
          c = tolower(getchar());
          if ( c == 'n' )
          {
             break;
          }
          while ( c != '\n' && c != EOF )
          {
             c = getchar();
          }
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 04-20-2005 at 09:00 AM. Reason: Tweaked 'eat to end of line' loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    
    char answer;
    int guess;
    int tries;
    int number;
    
    srand(time(0));
    number=rand()%20;
    
    answer='Y';
    while (anwers != 'N');
    {
    printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
    printf("Enter your first guess!\n");
    scanf("%d", &guess);
       if (guess > 20)
          printf("That guess is over 20!\n");
    
       for (tries=1; guess != number;tries++)
          {
             if (guess > number)
                printf("Wrong! %d is greater than the secret number!\n",guess);
    
             if (guess < number)
                printf("Wrong! %d is less than the secret number!\n",guess);
    
             printf("Enter another guess!\n");
             scanf("%d", &guess);
    
             if (guess > 20)
                printf("That guess is over 20!\n");
    
          }
    
    if(tries ==1)
       printf("Amazing! You got it on your first guess!\n");
    
    if(tries ==2)
       printf("Excellent! It took you only 2 tries!\n");
    
    if(tries ==3)
       printf("Good job! It took you 3 tries\n!");
    
    if(tries ==4)
       printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");
    
    if (tries >=5)
       printf("Not bad! It took 5 or more guesses!\n");
    
    printf("Play Again!?);
    answer=getchar();
    }
    return 0;
    }

    The bold areas are what I added. But it won't work!!! The damn thing loops on it's own after the correct answer!

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Are you entering 'N' or 'n'? Your program as it is now will continue to loop until 'N' alone is entered. I suggest at least using toupper() in your code so that the user may enter 'n'.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    That's because you're using scanf() right before getchar(). scanf() leaves a '\n' in the input buffer and that's what your call to getchar() is reading. The solution is in the FAQ (hint: you'll need to clear the input buffer.)
    Last edited by itsme86; 04-20-2005 at 09:16 AM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    so I can't use scanf, then what am I suppose to use?

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    [QUOTE=98dodgeneondohc]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    
    char answer;
    int guess;
    int tries;
    int number;
    
    srand(time(0));
    number=rand()%20;
    
    answer='Y';
    while (anwers != 'N');
    {
    printf("This is a guessing game! The computer will choose a number from 1-20, try to guess the number!\n");
    printf("Enter your first guess!\n");
    scanf("%d", &guess);
       if (guess > 20)
          printf("That guess is over 20!\n");
    
       for (tries=1; guess != number;tries++)
          {
             if (guess > number)
                printf("Wrong! %d is greater than the secret number!\n",guess);
    
             if (guess < number)
                printf("Wrong! %d is less than the secret number!\n",guess);
    
             printf("Enter another guess!\n");
             scanf("%d", &guess);
    
             if (guess > 20)
                printf("That guess is over 20!\n");
    
          }
    
    if(tries ==1)
       printf("Amazing! You got it on your first guess!\n");
    
    if(tries ==2)
       printf("Excellent! It took you only 2 tries!\n");
    
    if(tries ==3)
       printf("Good job! It took you 3 tries\n!");
    
    if(tries ==4)
       printf("Pretty good! It took you 4 tries. Now try to beat this record!\n");
    
    if (tries >=5)
       printf("Not bad! It took 5 or more guesses!\n");
    
    printf("Play Again!?);
    answer=getchar();
    }
    return 0;
    }
    I just left that out for now. But the problem is, the program will not even stop and wait for an answer if they want to play again. It will just print out "Play Again!?" then it runs the game.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I don't think you read my post very thoroughly. getchar() is seeing the '\n' that scanf() is leaving on the input buffer. You'll have to get rid of the '\n' somehow before getting the user's answer.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by itsme86
    I don't think you read my post very thoroughly. getchar() is seeing the '\n' that scanf() is leaving on the input buffer. You'll have to get rid of the '\n' somehow before getting the user's answer.

    Sorry about that. I was very frustrated and tired yesterday. I'm actually teaching myself c programming. I don't quite undestand the scanf() function throughly. I just know how to use it, but don't know how it works. If scanf() is leaving behind the \n... how will I get rid of it? or is there another function I can use other than scanf() that will take the input?

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    you should change the if-conditional code to be switch code.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Something like:
    Code:
    while((answer = getchar()) != '\n')
      ;
    Just add that code right before the answer = getchar(); line you already have. If you don't understand how that's achieving the goal then feel free to ask.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    21
    I'm not 100% sure on this. but wouldn't you also need the toupper() command to make sure that the input is made uppercase. I would give this a try at the end.
    Code:
    toupper(answer);

  15. #15
    Registered User
    Join Date
    Feb 2005
    Posts
    21
    i forgot to add that toupper is part of the ctype.h library.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to play play and stop wav files
    By cnu_sree in forum Linux Programming
    Replies: 4
    Last Post: 08-14-2006, 11:11 PM
  2. pls post here tricks that you know of in C/C++, thanks
    By mickey in forum C++ Programming
    Replies: 55
    Last Post: 06-12-2003, 04:28 PM
  3. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM
  4. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM
  5. Wanna play my game?
    By PJYelton in forum Game Programming
    Replies: 7
    Last Post: 09-26-2002, 09:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21