Thread: do while loop

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    do while loop

    Im in the stages of creating my 1st few programs and i am stuck on this code,
    i cant figure out how to test what the user types in to keep the loop going or not
    if the user enters y then i want it to loop, and n to end the loop. can anyone help?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    right or wrong, you should post whatever code you have been able to muster.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    You can get one character of input using getchar(). Suppose you do:
    Code:
    int c;
    c = getchar();
    you can then simply do:
    Code:
    if(c == 'y)
        /* do something */
    else if(c == 'n')
       /* do something else */

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    to improve it a little you can use the toupper function so you won't have to check for caps, i.e.

    Code:
    while (condition!='N'){
       /*Whatever process*/
       printf("Do you wan to continue? [Y/N]?\n");
       condition=toupper(getchar()); /*Whatever the user enters will forcibly be a cap*/
    }
    Either way the best way uncovering things in programming is: Try it! Mess it up! and try it again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM