Thread: How could I make my program switch Sleep() while enter is pressed?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    How could I make my program switch Sleep() while enter is pressed?

    I was thinking of a small rpg to write for fun, and I stumbled upon a question... Here's the problem:

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        string intro_speech = "Hello, and welcome to the introduction of this RPG!";
        
        for(unsigned i = 0; i < intro_speech.length(); i++)
        {
            cout << intro_speech[i];
            Sleep(100);
        }
        return 0;
    }
    So basically, what this simple program does (so far) is outputs the string "intro_speech" character by character, hence the for loop, with a 100 millisecond delay inbetween each character. But what I wish to do is while it's outputting it, if the user clicks ENTER it will change the 100 millisecond to 0 milliseconds, thus instantly outputting the rest of the string.

    I was thinking of maybe putting a "while" or "while...do" loop inside the FOR loop, but I'm not sure how to make the program pick up the enter key being pressed without using cin.get() which issue #1: Stops the continous output of string "intro_speech", making the user press enter after each character; issue #2: I would rather like the argument decision influenced by whether cin.get() equals to true or false at each moment of time until the loop is over , which, I have know idea how to check if cin.get() equals to true. Whether its even possible to check cin.get() for that, I honestly have no clue.

    The code I posted above has been edited of my horrific 'while' and 'do...while' loops.


    Any advice or tips would be greatly appreciated! Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    I was able to transform my program into this:

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    
    using namespace std;
     
    int main()
    {
        
        string intro_speech = "Hello, and welcome to the introduction!";
        for(unsigned i = 0; i < intro_speech.length(); i++)
        {
            cout << intro_speech[i];
            Sleep(100);
            if (GetAsyncKeyState(VK_RETURN)){
            system("CLS");
            cout << intro_speech;
            break;
    
            }
        }
        cin.get();
        return 0;
    }
    It is nowhere near the mothod I really wished to implement the program with ( Check if ENTER equals true or false), but this finishes with almost similiar results I wanted. But what I really wanted was to be able to press enter while the string is outputting, and speed up the process to ALMOST instant by switching the milliseconds in the previous Sleep() function.. but unfortunately I do not have that knowledge and need help..

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Okay sweet... After a little thinking, I remembered I could put in a variable in the curly's of Sleep(). So I declared int x as 100 ( so x would be 100 millisec's), took away the clear system function, cout, and the break, than added "x = 10;" in the "if" statements' brackets. Here's the code:

    Code:
     #include <iostream>
    #include <string>
    #include <windows.h>
    
    
    using namespace std;
     
    int main()
    {
        string intro_speech = "Hello, and welcome to the introduction!";
        unsigned int x = 100;
    
        for(unsigned i = 0; i < intro_speech.length(); i++)
        {
            cout << intro_speech[i];
            Sleep(x);
            if (GetAsyncKeyState(VK_RETURN)){
            x = 10;
            }
    }
        cin.ignore();
        cin.get();
        return 0;
    }
    So Woolah! I got the exact effect I wanted! =) But I would still like to know if there is way I could replicate this effect with bool variables ( true or false).

    BTW I made the variable int x an unsigned type because it should never be a negative number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  2. make the screen sleep
    By Headline in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2008, 10:30 AM
  3. How to find if enter was pressed in an edit control?
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2007, 07:25 PM
  4. How do i make out which button is pressed ?
    By intruder in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2006, 12:15 PM
  5. how do you know when enter is pressed?
    By pinkcheese in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2002, 12:45 PM