samedi 29 novembre 2014

Integer getch() -'0'


Vote count:

0




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


asked 36 secs ago







Integer getch() -'0'

Aucun commentaire:

Enregistrer un commentaire