Thread: Noob code question

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Question

    I am trying to make a continue loop for a game, am I on the right track? If not what is wrong with it and what do I need to fix. Thank you.
    Code:
    #include <stdio.h>
    
    int playagain;
    
    int main(void)
    {
        printf("Would you like to play again? (y/n)");
        scanf("\n%d", playagain);
    
        if ( playagain = 'y') {
            printf("Would you like to play again? (y/n)");
            scanf("\n%d", playagain);
        }
        else if ( playagain = 'n') {
            printf("Maybe another time");
        }
    
        return 0;
    }
    i can't get it to ask you to play again a second time and when i enter "n" it comes up with "would like to play again" and not "Maybe another time.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Just a thought...
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       char line[80] = "y"; /* Buffer size excessive? I'm being lazy. */
       while ( tolower(line[0]) != 'n' )
       {
          /* ... */
          printf("Would you like to play again? (y/n) ");
          fflush(stdout);
          fgets(line, sizeof line, stdin);
       }
       return 0;
    }
    
    /*
    Would you like to play again? (y/n) sure
    Would you like to play again? (y/n) yes
    Would you like to play again? (y/n) why not
    Would you like to play again? (y/n) no
    */
    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.*

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    thank you

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    You could use this too:

    Code:
    int main() {
    
    char ans;
    printf("Play again? (y/n");
    ans = fgetc;
    
    if (ans == 'y') {
    // play again
    } else {
    //exit or whatever
    }
    
    return(0);
    }
    Last edited by octoc; 03-27-2008 at 08:10 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by octoc View Post
    You could use this too:
    For your own practice, try making compilable, working code that repeats the prompt in a 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.*

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    what i pick up from your code is that u need

    &playagain when using scanf or will crash
    playagain=='y' because its evaluationg playagain to see if its (y) when u put playagain = 'y'. this mean y is giving to playagain


    Code:
    #include <stdio.h>
    #include <conio.h>
    int playagain;
    
    int main(void)
    {
        char playagain;
        printf("Would you like to play again? (y/n)");
        scanf("\n&#37;c", &playagain); 
     
      
        if ( playagain == 'y') 
        {
            printf("Would you like to play again? (y/n)");
             scanf("\n%c", &playagain); 
         if ( playagain == 'y') 
        {
            printf("Would you like to play again? (y/n)");
             scanf("\n%c", &playagain);
             
         }
        
        }
        else if ( playagain == 'n')
         {
            printf("Maybe another time");
         }
         
     
        getch();
        
        return 0;
    }
    Last edited by joker_tony; 03-27-2008 at 09:41 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are missing fflush(stdout) between printf and scanf
    also conio.h and getch could be fully avoided here - use standard equiivalent instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  3. Noob question
    By clb2003 in forum C Programming
    Replies: 19
    Last Post: 12-15-2008, 03:01 AM
  4. Noob question! Code completion - Array challenge
    By theleprechaun in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2008, 08:58 PM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM