Similar ascii

I have problem wiht my code.

Arrow down key ascii is 80 and letter P is also 80

so when i press arrow key down it's cout letter P

I want to fix it.
@rathpanha

Make sure the Caps-lock is off, so that you're using lower case. That way, only the down key is read.
This is a quirk of getch. It returns characters, not keypresses, and there is no character corresponding to down arrow. As you are probably using Windows anyway, use ReadConsoleInput instead.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684961%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
@whitenite1

I try to make username login password,so i use getch() and function push_back to transfer value from getch() to variable.

The point is when i press down arrow key it's also transfer letter "P" to variable.
@MiiNiPaa

Thanks for your reply,but i think the way you tell me it's not work.You can see a little detail of my problem above.
You can see a little detail of my problem above.
You have a problem with getch behavior. Do not use getch then. Did you try to use alternative approach? What exactly did not work?
@MiiNiPaa

This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
char ch;
string username;
rc1:ch=getch();
while(1)
{
	if(ch>=33&&ch<=126)
	{
		username.push_back(ch);
		cout<<ch;
		ch=getch();
	}
	else if(ch==8)//8 is backspace ascii code
	{
		if(username.length()==0)
		{
			goto r1;
		}
		username.erase(username.length()-1,1);
  		cout<<' ';
  	   r1:	ch=getch();
	}
	else if(ch==13)//13 is enter ascii code 
	{
		if(username=="")//just don't let cursor go down when press enter while username don't have value
		{
			goto rc1;
		}
		goto r2;
	}
	else
	{
	       ch=getch();
	}
}
r2:


Look at the line number 9 when i press (uppercase letter p) it show letter P normally,but when i press (arrow down key) it also show letter P.It mean (Letter P ascii) and (arrow down key ascii) is the same(80).So when i press (arrow down key) it will transfer letter P to (variable : username),I don't want it.I want it's nothing when i press (arrow down key).
Last edited on
MiiNiPaa wrote:
This is a quirk of getch. It returns characters, not keypresses, and there is no character corresponding to down arrow.
If you do not like that behavior, use something else. For example
As you are probably using Windows anyway, use ReadConsoleInput instead.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684961%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
@MiiNiPaa

Thanks.
Topic archived. No new replies allowed.