Reputation: 333
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?
Upvotes: 2
Views: 1716
Reputation: 170
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..
Upvotes: 1
Reputation: 2054
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.
Upvotes: 3
Reputation: 718
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
Upvotes: 0