Thread: looping went berserk

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Angry looping went berserk

    Could anyone advice me on why my loop gives a funny output?

    My program should work this way...
    1. Prompt user if they want to play the game
    2. If user type 'y'
    >>>2.1. Display 3 random number
    >>>2.2. Prompt user if they want to play again.
    >>>2.3. If user type 'y' again (repeat step 2.1 & 2.3)
    >>>2.4. Else If user type 'n', program terminate.
    3. Else if user type 'n', no loop just terminate program.
    Here is my code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void welcome_msg(void)
    {
      printf("Pick your number! \n");
    }
    
    void reply_msg(char reply)
    {
      printf("Would you like to play? (y/n) ");
      scanf("%c", &reply);
    }
    
    void num_display(void)
    {
      srand((unsigned)time(NULL));
      printf("\nNum: %i  %i  %i  ", rand() %10, rand() %10, rand() %10);
    }
    
    void goodbye_msg(void)
    {
      printf("Thank you for playing.\n");
    }
    
    int main(void)
    {
      char answer;
    
      welcome_msg();
      reply_msg(answer);
    
      looping: /* a label */
      printf("Play again? \n ");
      scanf("%c", &answer);
      
      if(answer == 'n')
      {
        goodbye_msg();
      }
      else
     {
       num_display();
       goto looping; /* Jump to the label*/
      }
    
     return 0;
    }
    Really appreciate for all the help.
    Thanks in advance!

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Don't use goto, your program can easily be written without it. The reason your code loops forever is because scanf() is leaving '\n' the input buffer. You need to flush it. Here is a tutorial how: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Try reworking your code.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    also
    Code:
    void reply_msg(char reply)
    is not returning the value of answer
    this code is messy

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should probably work towards only seeding the random number generator once in your code, at the beginning of the main function perhaps.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Okie now I'm a little confused which to change first....

    Well I'm trying to change the getchar... but not sure where I've to put it in my code without the go to...

    Here is what I've done without the "go to"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void welcome_msg(void)
    {
     printf("Pick your number!\n");
    }
    
    void num_display(void)
    {
     srand((unsigned)time(NULL));
     printf("Num: %i  %i  %i  ", rand() %10, rand() %10, rand() %10);
    }
    
    void goodbye_msg(void)
    {
     printf("Thank you for playing.\n");
    }
    
    int main(void)
    
    {
     char ans;
     char ch;
    
     welcome_msg();
     puts("Would you like to play (yes/no)? ");
    
     while((ch = getchar()) != '\n' && ch != EOF);
      if (ans == 'y')
     {
      num_display();
      printf("Play again? ");
      scanf("%c", &ans);
     }
     else
     if (ans == 'n')
     {
      goodbye_msg();
    }
    
    return(0);
    }
    I know the code don't make any sense... but oh boy... im getting real confused.... any help would be great!

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just put while((ch = getchar()) != '\n' && ch != EOF); after each scanf() you do.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Like this:

    Code:
        while (c != '\n' && c != EOF && c != 'n') {
            c = getchar();
            scanf("%c", &input);
            if (input != 'n')
                printf("%d %d %d\n", rand() % 10, rand() % 10, rand() % 10);
        }

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Quote Originally Posted by miryellis
    Okie now I'm a little confused which to change first....

    Well I'm trying to change the getchar... but not sure where I've to put it in my code without the go to...

    I know the code don't make any sense... but oh boy... im getting real confused.... any help would be great!
    You can try to compact things a bit. Some of the functions calls could be improvised to cover more aspects of the program, and possibly eliminate the need for some of the user entry testing with if/else.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    
    char welcome_msg(void)
    {
       char choice;
       static int runOnce = 0;
       
       fflush(stdout);
       
       puts("Pick your number!\n");
       
       printf("Would you like to play %s (yes/no): ", (runOnce != 0 ? "again" : ""));
       scanf("%c", &choice);
       
       while(getchar() != '\n'){}   
       runOnce = 1;
       
       return (toupper(choice)); 
    }
    
    void num_display(void)
    {
     
        printf("\nNum: %i  %i  %i  \n\n", rand() %10, rand() %10, rand() %10);
    }
    
    void goodbye_msg(void)
    {
        printf("\nThank you for playing.\n");
    }
    
    int main(void)
    
    {
        char ans;
         
        srand((unsigned)time(NULL)); 
    
        while((ans = welcome_msg()) == 'Y')
           num_display();     
     
        goodbye_msg();
    
       return(0);
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. Looping in Switches
    By Kaminaga in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2005, 07:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM