#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep Power
    0

    Need help making an infinite loop


    hello im a bit stuck i want to make an infinite loop for my program but not sure how

    i want the program to display

    Do You want to Continue Y/N ?

    On the user typing "Y" the program will run again

    On the user typing "N" the program ends

    How do i do this ?

    Where would be a good place to put the code ?

    Can anyone help please?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    There's more than one way to do it!

    Code:
    while (1) {
        printf("Do you want to run this again? (Y/N) ");
        ch = getch();
        if (ch == 'N' || ch == 'n')
            break;
        else if (ch == 'Y' || ch == 'y') {
            .... your code comes here
        }
        else 
            printf ("Dammit! I said type Y or N\n");
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    another one:
    Code:
    char ch='J';
    while (ch=='j'||ch=='J') {
      ... run code ...
      do {
        printf("Do you want to run this again? (Y/N) ");
        ch = getch();
      } while (ch!='J' && ch!='j' && ch!='n' && ch!='N');
    }
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep Power
    0
    thanks for your help everyone ive got the loop working perfectly :D

IMN logo majestic logo threadwatch logo seochat tools logo