Thread: c progammimg problem

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    8

    c progammimg problem

    hi. i am having problem with the loop in my program. i am only a beginner and struggling with c program. i hope to pass this course so please help me.

    PROBLEM: when ever i enter a 'y' in the loop, the loop continues but i want it to ask if i want to play again in between. here is my code:


    the output should be:

    Code:
    Would you like to play inBetween [y|n]? y
    Die 1: 2 Die 2: 8
    Not the same, let's play!
    Number of chips: 100
    Place your bet: 10
    Die 3: 5
    *** You win! ***
    You now have 110 chips!
    Play again [y|n]? y
    Die 1: 1 Die 2: 10
    Not the same, let's play!
    Number of chips: 110
    Place your bet: 10
    Die 3: 5
    *** You win! ***
    You now have 120 chips!
    Play again [y|n]? y
    Die 1: 9 Die 2: 9
    Even-steven - let's play again!
    Play again [y|n]? y
    Die 1: 3 Die 2: 5
    Not the same, let's play!
    Number of chips: 120
    Place your bet: 20
    Die 3: 6
    *** Sorry - You lose! ***
    You now have 100 chips!
    Play again [y|n]? n
    Thanks for playing!
    any help would be appreciated
    Last edited by amie001; 06-05-2011 at 08:39 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You didn't actually post your code, so I can't really tell you why it's not working. The general idea for running a process until the user quits is something like:
    Code:
    do {
        // play the game
        print "Play again [y|n]?"
        play_again = getchar();
        // flush the input buffer -- read about it here
    } while (play_again == 'y' || play_again == 'Y');
    That red text in there is a link, so click it and read up.

    A few common mistakes we see around here are for this type of problem:
    1. failing to flush the input buffer -- getchar wont remove the character that represents the user hitting the enter key
    2. Not realizing that lower case 'y' and upper case 'Y' are two separate characters according to C
    3. Incorect use of the || or && operator: play_again == 'y' || 'Y' is wrong.
    4. Getting confused on which combination of ==, !=, || and && to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM