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 6 Years Ago by invisi: n/a

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;
}

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

This article has been dead for over six months. Start a new discussion instead.