0

Below is the code I wrote, for taking the arrow key imput, just wanted to share it and get some feedback thanks :)

#include <iostream>
#include <conio.h>


using namespace std;

int main()
{
	int number = 0;
	int number2 = 0;
	
	
	while(true)
	{
	
	int arrow = getch();
	
		if (arrow == 77)
		{
				number = number + 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
		if (arrow == 80)
		{																	
			number2 = number2 + 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
			if (arrow == 75)
		{																	
			number = number - 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
		if (arrow == 72)
		{																	
			number2 = number2 - 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
	}
	
	
}

Edited by invisi: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by invisi
0

getch() returns two bytes, not just one, when you press special keys such as arrows and function keys. I like to make the keys negative values so that the program can easily distinguish between special keys and normal keys, but there are other ways such as add 255 to the value return by getch().

int main(int argc, char* argv[])
{
    int c = getch();
    if( c == 0 || c == 224)
        c = -getch();
    
    printf("%d\n", c);
	return 0;
}
0

Thanks, I didn't think that the special keys would conflict with the normal key so thanks for the heads up :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.