can u guess my number?

This is a discussion on can u guess my number? within the C Programming forums, part of the General Programming Boards category; Originally Posted by strider496 thx for the helps guys i got it to run correctly Hey i'm writing a program ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1
    Quote Originally Posted by strider496
    thx for the helps guys i got it to run correctly
    Hey i'm writing a program similar to this and i was wondering if u could help me. i'm a first year student doing c programming so this is new to me but i'm doing a project for my course. Anyway I have the line Would you like to play again? (y or n) at the end of the function but i don't know how to loop it back to play again if the player enters y. can you help me?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > but i don't know how to loop it back to play again if the player enters y
    while ( answer == 'y' )

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Anyway I have the line Would you like to play again? (y or n) at the end of the function but i don't know how to loop it back to play again if the player enters y.
    You can use the function's return value for that.
    Code:
    #include <stdio.h>
    #include <ctype.h>  /* for tolower() */
    
    int game(void) {
        /* play the game */
        printf("Would you like to play again? (y/n) ");
        return tolower(getchar()) == 'y';
    }
    
    int main(void) {
        while(game());
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. small problem
    By ucfmustang in forum C Programming
    Replies: 1
    Last Post: 02-10-2003, 03:55 PM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM

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