getch() not working properly

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...
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?
How do I make it show the corresponding ASCII equivalent?
Like 1 for 50
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
Note that print and scan are carried over from the C library. It's better to use IOstreams instead.
I saw another solution...Much more simple.

int keuze = getch() - '0'
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);
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.
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.
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
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.