2

I wrote a simple C++ program that displays a math table:

#include <iostream>
using namespace std;
int main()
{
    int number, result, var=0;
    cout << "Enter number to generate table:";
    cin >> number;
    while (var < number*10)
    {
        result = number + var;
        cout << "\n" << result;
        var += number;
    }
    cin>>var;
    return 0;
}

So when the user types some digits (like e.g. 22) and hits Enter in the Console Window the table will generate. But I want to show the result instantly as the user types the digits. The user should not be required to hit Enter.

How do I process the input without the user hitting Enter?

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
MAK
  • 163
  • 2
  • 11
  • 2
    We'll need more information. C++ itself has no notion of keypresses. – chris Jul 26 '13 at 06:04
  • Can you take a look at [this](http://www.cplusplus.com/forum/general/29137/) – NREZ Jul 26 '13 at 06:05
  • This is a console and platform feature. Without knowing what console and/or platform you are using, we wouldn't even know if it was possible. Some consoles don't send any keypresses until you hit enter. (Though you'd have a hard time finding any of those today.) – David Schwartz Jul 26 '13 at 06:07
  • Not sure if I understand completely, but you may get the desired effect simply by flushing the buffer (`std::cout << std::endl;`) after producing the output. – jogojapan Jul 26 '13 at 06:07
  • @jogojapan: He's talking about input, not output. – David Schwartz Jul 26 '13 at 06:08
  • You will just limit your output table to single digit number input by doing this. Not preferable it is. –  Jul 26 '13 at 06:12
  • guys my simple point is i just want to get the keystroke and show the result instantly without hitting Enter , can c++ do that itself or not – MAK Jul 26 '13 at 06:14
  • 1
    How your program is going to decide whether user finished writing or he wants to add another digit? – Qiu Jul 26 '13 at 06:24
  • @Qiu no matter user has finished or still want to adds more for instance user hit 2 so program should show the table of 2 but as he add more digits e.g 22, 25 or 26 then it should show the 22, 25 or 26 table instantly. we have a good Example Like Google instant Suggestion – MAK Jul 26 '13 at 06:28
  • See also [This question](http://stackoverflow.com/q/1449324/78845) and [the C++ FAQ Lite](http://www.parashift.com/c++-faq-lite/kbhit.html). – johnsyweb Jul 26 '13 at 06:36
  • @MAK, you are talking about console here & Google instant suggestion uses web API, Doesn't it occur to you that it is not possible to do such thing on the console. You could do it for just one input using getch() but after that even if there is another way for it, I think that the output table on the console will not change dynamically with your input entry. –  Jul 26 '13 at 06:37
  • @AmoghDikshit you are correct in your point of view but the idea is same , when you type for a query in Google gives instant suggestions so i want to do the same as user type digits to generate table program should give instant output – MAK Jul 26 '13 at 09:22

3 Answers3

3

getch() from <conio.h>, outputs the ASCII code for the single key that was pressed. You may process the returned value afterwards. Something like this:

#include <conio.h>
#include <ctype.h>
int main()
{
    int myVar = 0;
    myVar = getch();
    if (isdigit(myVar))
    {
        myVar = myVar - '0';
    }
    return 0;
}

The drawback is that getch() will only read 1 key.

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
1

for input without Enter key, you may use the getch() function in conio.h.. it takes one input character. if you want input to be displayed (echoed) on console, use getche() kbhit() is another function that can detect any keyboard press..

0

In VC++ you can implement the same logic for LostFocus()of an item where the particular input(var) is given, but in C++ the cin takes the value from the console on the basis of the Enter key press, if you dont press Enter, then the value is not passed from Console to the pgm

Saby
  • 718
  • 9
  • 29