2

I have a project that I am working on using Dev-C++ as the IDE and Windows as the operating system. As the project is restricted in complexity due to size constraints and current knowledge, I would prefer to avoid using GetAsyncKeyState and threading unless there is a fairly simple (less than 10 lines) approach I could use.

My program currently displays text using the Text function using strings and outputting them to the console window. I want to implement a feature that would allow the user to press a key, and allow the text to print out without waiting for the function to read the entire string.

For example:

"Welcome to ..." [user presses space bar while the text is slowly being displayed character by character]

"Welcome to my program! This was developed by me!" [entire string is displayed]

Code:

void Text(string input)
{
    int x = 0;
    for(int i = 0; i <= 3; i++) //decides when to stop running based on number of strings
    {
        while (input[x] != '\0')
        {
            if(input[x] == '.' || input[x] == '!')
            {
                cout << input[x];
                Sleep(375);
                cout << " ";
                x++;
            }
            else if(input[x] == '*')
            {
                Sleep(375);
                cout << endl;
                x++;
            }
            else if(input[x] == '~')
            {
                Sleep(2000);
                system("CLS");
                x++;
            }
            else
            {
                cout << input[x];
                Sleep(75);
                x++;
            }
        }
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Austin
  • 23
  • 3
  • Don't post images of code, if the code is relevant include it in the question. Also make sure to include as little code as possible as per the guidelines of a [mcve]. You can't do two things at the same time without using some form of threading, no. – super Apr 03 '20 at 20:14
  • It seems to me that you want more control over the console then what the standard library functions offer. Have a look at something like ncurses. – super Apr 03 '20 at 20:18
  • @super Thank you for your timely response. I will definitely have to look into ncurses, as it looks like something I could use for private projects. Unfortunately, our instructor is very strict in forcing us to use Dev C++. I do agree it does have limits in terms of control at times... :( – Austin Apr 03 '20 at 21:37

1 Answers1

0

No, you cannot in a portable way. The reason is that standard I/O on the usual consoles wait for a line to be completed, so you cannot capture single characters as soon as they are typed.

You can achieve that with non-standard functions, though! See Capture characters from standard input without waiting for enter to be pressed

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 1
    Thank you so much! I used the header conio.h and was able to use a check to see if kbhit() was true. I'm pretty sure my professor has a windows computer but if not I can always rewrite this section without it. Thanks again! – Austin Apr 03 '20 at 22:01