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?
8 Respuestas
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;
0
add : i tried to make like this :
char num = str[0];
atoi(num);
But it doesn't work too.
0
What exactly you mean?
0
Do you want their ascii values or translations between string numerals to integer data types? If so I edited first comment.
0
Yes
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.
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
0
Thx