how to get input from user without pressing enter.

i want to know if there is any way if the user just press 1 and the function runs without pressing enter?

any help would be appreciated.




1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	int n;
    cin>>n;
	if(n==1)
	{
		cout<<"Working";
	}
}
Last edited on
Hello daimkhalid07,

Your closing code tag is incorrect. It should be "[/code]". Notice the forward slash. These links may help you:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

"conio.h" is not a standard C++ header file, but if you want to use it I would replace std::cin >> n; with n = _getch();. This gets one character from the keyboard and moves on.

This is a good start for something, but as is it only proves point and does not have much use.

Hope that helps,

Andy
but if i want to enter an integer value??
Hello daimkhalid07,

Good question. Sorry it was early and I did not think it through. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<conio.h>

//using namespace std;  // <--- Best not to use.

int main()
{
	int n{};  // <--- Always a good practice to initialize your variables.
	char ch{};

	ch = _getch();
	n = static_cast<int>(ch - 48);  // <--- Because 48 is the ASCII code for zero.
	
	if (n == 1)
	{
		std::cout << "Working";
	}

	std::cout << "\n\n\n Press any key to continue: ";
	_getch();

	return 0;
}


Hope that helps,

Andy
Last edited on
yes it helps but l am confuse with the expression you used in static_cast<int>(ch - 48), why you use "ch - 48" can you please help me with it.
Hello daimkhalid07,

Doing the input as a "char" the character, in this case a number, is stored as the ASCII code 48 decimal or 30 in hex for zero. So subtracting 48 from the ASCII code for 1, which is 49, leaves you with 1 that can be stored as an "int". Using static_cast<int> just makes sure that the end result is stored in "n" as an "int".

This is used quite often to change a "char" to an "int" when you need a number from zero to nine.

This is a link for part of the ASCII chart: http://en.cppreference.com/w/cpp/language/ascii There are others and even some that go to 255. A link worth saving.

Hope that helps,

Andy
Just a suggestion. The code is more readable if it is written as
ch - '0' rather than ch - 48

This has at least two advantages:
• it more clearly expresses the programmer intention
• it avoids the need for both original programmer and a later reader to have to memorise the ASCII values

Regarding the cast, it is probably not needed. The compiler automatically does many conversions e.g assigning an int to a variable of type double without it.
Last edited on
@Chervil,

Thank you for the input. I will change that in the future. I must have seen using "48" somewhere and it stuck.

Andy
@Andy No problem, I didn't want it to seem like I was nitpicking or deliberately finding fault.
Topic archived. No new replies allowed.