getch() not working properly

Sep 4, 2010 at 6:48pm
I wrote a little piece of code to test the getch() function:

1
2
3
4
5
6
7
8
9
10
int menu()
{
    cout<<"What would you like to do?\n";
    cout<<"\n1: Battle a monster!\n";
    cout<<"2: Go to the shop!\n";
    int keuze = getch();

    return keuze;

}


But when I input for example, 1 and then let int main() show keuze, it's always 50...
Sep 4, 2010 at 7:22pm
well to answer you're question y need to now what do you wand the program to do?..from what y see there,if you print the variable keuze,it will show the ascii code of the character 1,or 2 depending of you're choice..y tested it and for character '1' the code is 49, for '2' is 50.what is not working properly?
Sep 4, 2010 at 7:57pm
How do I make it show the corresponding ASCII equivalent?
Like 1 for 50
Sep 5, 2010 at 5:29pm
so if you want to show the character,by giving the ASCII code u simply print the int variable where you stored the ASCII code with the correct descriptor %c.like in the program below.
1
2
3
4
5
6
7
8
9
10
int main ()
{
	int AsciiCode;
	printf("Enter a number: ");
	scanf("%d",&AsciiCode);
	printf("The caracter that has the ASCII code: %d is: %c\n",AsciiCode,AsciiCode);
	printf("Press Enter to continue");
	getche();
	return 0;
}
Last edited on Sep 5, 2010 at 5:29pm
Sep 5, 2010 at 6:11pm
Note that print and scan are carried over from the C library. It's better to use IOstreams instead.
Sep 5, 2010 at 6:17pm
I saw another solution...Much more simple.

int keuze = getch() - '0'
Sep 5, 2010 at 7:23pm
I saw a better solution, since 0 is not the null character:
char keuze = getch();
Alternatively:
1
2
3
4
5
6
7
int keuze = getch();

// bla bla
// bla
// blub

std::cout << char(keuze);
Sep 6, 2010 at 4:41pm
1
2
How do I make it show the corresponding ASCII equivalent?
Like 1 for 50


man,then you made a mistake when you said what you want the program to do..or maybe u don't even now what you want..u said enter the value 50,(49 is the correct but no matter)and you need to show 1.well how can you enter that value with that crap??int keuze = getch() - '0' or int keuze = getch();,the piece of code u wrote reads a character from the keyword..so y am asking you one more time:what do you want to do? read a character and show the ASCII code for it,or read the ASCII code and show the character??.because you're solution is not better,it does something else that what xander33 told me above.
Sep 6, 2010 at 5:10pm
Mihay07: this is completely off topic, but may I ask why you use "y" instead of "I"? Your "i" key seems to be working fine. The letter y doesn't sound like an i in english.
Sep 6, 2010 at 5:21pm
wow..i made a mistake so y am asking ..thank you for reading all posts and spotting errors in other people's grammar.so as a tip,if you still read the posts you could be ON TOPIC..i don't make comments on other's writing.TY
Sep 6, 2010 at 6:34pm
I didn't mean to be ironic or anything like it, I was just curious because you did it systematically. This is a friendly forum and we're not in the habit of making fun of other people's mistakes.
Topic archived. No new replies allowed.