Like Tree1Likes
  • 1 Post By rogster001

Input during loop

This is a discussion on Input during loop within the C++ Programming forums, part of the General Programming Boards category; I am attempting to allow the user of my program to be able supply an keyboard input while a for ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Question Input during loop

    I am attempting to allow the user of my program to be able supply an keyboard input while a for loop is looping. The input is going to be used to stop the for loop.

    Here is the loop:

    Code:
           case 1: for (int key = 0; key == 0; key+0)
                    {
                        system("CLS");
                        hunger = hunger + 10;
    
    
    
                        if (hunger >= 60 && hunger <= 80)
                            health = health - 10;
    
                            else if (hunger >= 80 && hunger <= 180)
                                 health = health -10;
    
                                 cout << "::Pet's Stats::\n\n\n"
                                      << "Health: " << health << "\n"
                                      << "Hunger: " << hunger << "\n\n\n"
                                      << "Press 1 and enter to get back to the pet care menu!\n\n";
    
                                      Sleep (5000);
    
                                 cin  >> key;
    
    
    
    
                    }
    It would also be good if I could allow the user to push any key to exit the loop, but I am unsure how to go about this.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    Do you mean that you want the loop to continue cycling unless it sees user input (i.e. not stop and wait for user input each cycle)?

    If so, there isn't really a standard way of doing it, but you can find some information here.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    I'm just looking for a way to use the goto function to get back to my main menu I need to be able to do this at any point while my loop is looping and its ok for the loop to stop once I've gone back to my menu but it must keep looping until I press a key or press 1 and enter or something like this.

    when I try to use cin it stops the loop from working properly!

    Thanks

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    I think you probably mean a kbhit() style function, again this (using conio.h) is non standard but the faq link is here kbhit - C++ Function Reference

    Bear in mind to keep track of all your data status when you exit the loop, that is if you wish the kbhit to perform a 'pause' like function then you need to ensure that when the loop resumes that everything picks up where it left off, variable values etc.

    In a 'stop' scenario it is a bit simpler where you can just call an Init() function to reset everything prior to a restart - or reset with values according to the new menu choice etc etc.
    SRT4847 likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Quote Originally Posted by rogster001 View Post
    I think you probably mean a kbhit() style function, again this (using conio.h) is non standard but the faq link is here kbhit - C++ Function Reference

    Bear in mind to keep track of all your data status when you exit the loop, that is if you wish the kbhit to perform a 'pause' like function then you need to ensure that when the loop resumes that everything picks up where it left off, variable values etc.

    In a 'stop' scenario it is a bit simpler where you can just call an Init() function to reset everything prior to a restart - or reset with values according to the new menu choice etc etc.
    After a load of googling I found out about the kbhit function and I used it in my code and it works great. If I had not found it your help would have been spot on so thanks for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop reading input
    By c_newbie in forum C Programming
    Replies: 5
    Last Post: 12-08-2010, 08:09 AM
  2. Input continuation loop
    By Sinosizer in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 11:54 AM
  3. Loop while waiting for input?
    By Terran in forum C++ Programming
    Replies: 6
    Last Post: 05-22-2008, 08:32 PM
  4. while loop char input
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 04-09-2006, 05:19 PM
  5. user input loop
    By asudave2302 in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2004, 08:45 PM

Tags for this Thread


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