0

How i can MAKE CONVERSATION from CHAR to INT ( if my character , for example '1' )

I need to make like this : string str; getline(cin,str); Input : //11:30 PM //How we can see str[0] is digit character ('1'); I tried to conver it by atoi function , but it doesn't work; So how i can realize it?

25th Apr 2020, 7:08 PM
Eskalate
Eskalate - avatar
8 Answers
0
Use (int)char; (char)int; edit: if you mean converting string numbers to integres and vice versa, you might think doing something like; for example, lets say '1' has ASCII value of 20, you would do this, int num = '1' - 19;
25th Apr 2020, 7:18 PM
Mustafa K.
Mustafa K. - avatar
0
add : i tried to make like this : char num = str[0]; atoi(num); But it doesn't work too.
25th Apr 2020, 7:10 PM
Eskalate
Eskalate - avatar
0
What exactly you mean?
25th Apr 2020, 7:20 PM
Eskalate
Eskalate - avatar
0
Do you want their ascii values or translations between string numerals to integer data types? If so I edited first comment.
25th Apr 2020, 7:21 PM
Mustafa K.
Mustafa K. - avatar
0
Yes
25th Apr 2020, 7:23 PM
Eskalate
Eskalate - avatar
0
char c = '1'; // '1' is 49 in decimal (ascii value) so when you want to convert 1 digit numbers its easy. Just do that, int i = (int)c - 48; With more than 1 digit strings you need to create a loop and make your own logic, i will leave this to you. Or you can simply use pre defined ones.
25th Apr 2020, 7:27 PM
Mustafa K.
Mustafa K. - avatar
0
Eskalate atoi is C library function that accepts charecter array.. Try these ways: For total string like "1234" string str; getline(cin,str); int i= stoi(str); cout<<i; Or For single charecter : cout<<abs(48-(int)str[0]); //str="1234", output: 1
25th Apr 2020, 7:29 PM
Jayakrishna 🇼🇳
0
Thx
25th Apr 2020, 7:33 PM
Eskalate
Eskalate - avatar