loop stop

This is a discussion on loop stop within the C++ Programming forums, part of the General Programming Boards category; how do u make a loop that will stop when u hit a key ex.kbhit();...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    loop stop

    how do u make a loop that will stop when u hit a key
    ex.kbhit();

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    what kind of loop are you wanting to use?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    //i want a loop like this to be stoppded when hit key
    //anybody who knows, please help me!


    #include <iostream.h>

    int main()
    {
    int x;
    for(x=0;x<100000;x++)

    cout<<x;

    return 0;
    }

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    This is what you would do and how you would do it:
    Code:
    while (!kbhit())
    {
    /*** do whatever you have to do ***/
    }
    Does this help?

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    well you may want to use an if statement and a function that always gets a keystrok, and execute the if statement if a key is pressed. then you could have it x=100000, and that would stop it. you could also use the while loop suggested, but you would want to limit it by using a counter and another condition to the loop

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> but you would want to limit it by using a counter and another condition to the loop <<

    Why???
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    he has a for loop, it will end after a period of time, so if he wanted it to still do that, he would need a counter and an additional condiditon that includes the counter

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    But why would you need a "limit"?
    1978 Silver Anniversary Corvette

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    I don't know, it is his code, but he had one in his example, so he will probably want one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM

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