+ 1

Suppose that there asked user to input char variable. How to make c++ to recognize entered character as int if it is a digit?

C++

22nd Jan 2019, 11:51 AM
Eric
Eric - avatar
4 odpowiedzi
+ 6
If a user is entering data in the form of a string, then you can use the atoi function to change it to an int. Format: atoi(const char * str)
22nd Jan 2019, 12:31 PM
Dread
Dread - avatar
+ 1
Based on the ASCII table you can use this condition: if (c>='0'&&c<='9')
22nd Jan 2019, 12:16 PM
HonFu
HonFu - avatar
+ 1
Also, if you know beforehand that the input is a number, you can use: int input; cin >> input;
22nd Jan 2019, 1:21 PM
Zen
Zen - avatar
+ 1
you you have a chat variable "input" storing entered digit, then to convert it to int you can do this: int converted = input - '0';
22nd Jan 2019, 11:55 PM
Data
Data - avatar