Reputation: 827
I've created a sudoku game in which you will enter the row number then the column number. I set an if else condition that only numbers 1-9 will be entered. I used getch function instead of cin after entering a number to avoid using the enter button.
But when I enter 1 in row, it won't accept it and it prints the number 49. I've researched about it and learned that getch function is for get character and if you enter 1, it will translate to '1' which is equivalent of 49 in integer. I've found a solution by using getch() -'0' instead of only getch();
It works but I can't seem to understand the explanation about how it works. Can someone explain it how the getch() -'0' works?
Here's where I used the getch() -'0':
cout << "Enter row: "; row = getch() -'0';
if(row > 9 || row < 1) {
cout << "Rows 1 to 9 only!" << endl;
Sleep(1000);
system("cls");
loop -= 1;
} else {
cout << row << "\nEnter column: "; column = getch() -'0';
if(column > 9 || column < 1) {
cout << "Columns 1 to 9 only!" << endl;
Sleep(1000);
system("cls");
loop -= 1;
}
Upvotes: 0
Views: 4661
Reputation: 511
Note that you are not subtracting 0 but '0'. This is a big difference because '0' is a char so when you subtract it, you are implicitly converting it to an int value which is 48 in ASCII. So you are basically reading in values that are "indexes" to chars in the ASCII table, then subtracting the index of the first char you care about to get relative "indexes". Your if statement should take care of any invalid chars including letters and symbols so this should work fine.
Upvotes: 0
Reputation: 3055
getch() returns the ascii code for the pressed key.
The ascii codes for the number 0..9 are contiguous, 0 = 48, 1 = 49, 2 = 50, and so on.
The C/C++ compiler converts a single-quoted character into its ascii code, so '0' compiles to 48.
getch() - '0' is subtracting the ascii code offset of the digits range from the typed number, yielding the offset of the pressed digit from 0. 0 is at offset 0, 1 is at offset 1, 2 is at offset 2, and so on.
Upvotes: 0
Reputation: 285
Its similiar as you write
int k=getch() //stores ascii value of the entered character
As the ascii value of character '0' is 48 so if you substract k by 48 u will get the corresponding entered number
similiar thing happens here---
getch()-'0'
Upvotes: 0
Reputation: 4555
getch() will get the value of a char and translate that to its int value. Which is why you get 49.
Now if chars have int values and 1 is 49, imagine subtracting the int value of 0 away from that. The numbers are in order from 0-9, so if one has 49, then zero has 48. So you are saying, get this int value (1 = 49) and subtract away 0's int value (48), 49-48 = 1.
This works for all char's which represent numbers. Ex: 8 - '0' == 56 - 48 = 8.
Upvotes: 5
Reputation: 7294
This trick is relying on the fact that the encoding for the numbers 0-9 are in order in the character sequence, in ASCII they are the values 48-57. So if the character '4' is typed, it will return 52, and you can convert that to the number via 52 - 48 = 4. The danger of course is that someone will enter a non-number - you need to check carefully for error.
Upvotes: 1