| | |
how to convert char to int
![]() |
hi there, i just want to know what function to use in converting a char into an int
like:
is this the right way?
like:
C++ Syntax (Toggle Plain Text)
char temp; cout << "enter something"; cin >> temp; atoi(temp);
is this the right way?
Retreat!!!
>>is this the right way?
No because atoi() takes a pointer to a null-terminated string and your variable temp is just a single character.
The quickest way is to simply subtract the ascii value of '0' from it like this:
No because atoi() takes a pointer to a null-terminated string and your variable temp is just a single character.
The quickest way is to simply subtract the ascii value of '0' from it like this:
int n = temp - '0';. Why does that work? Its because a char is a small integer and if you look at an ascii chart you will see that all characters have an ascii numerical value. Internally, programs know nothing at all about characters, only numeric values as shown in that ascii chart. Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Aug 2008
Posts: 13
Reputation:
Solved Threads: 1
•
•
•
•
hi there, i just want to know what function to use in converting a char into an int
like:
C++ Syntax (Toggle Plain Text)
char temp; cout << "enter something"; cin >> temp; atoi(temp);
is this the right way?
but the only thing u need to do is that u should use char array in place of char variable, and the function atoi is return an int value so u should store the result in some integer variable.
like
C++ Syntax (Toggle Plain Text)
char temp[10]="10"; int i; i=atoi(temp)*2; cout<<i; \\ the output = 20;
![]() |
Similar Threads
- Conversion from Char* to int ? (C++)
- Convertings strings to numbers (C++)
- char+int????? (C++)
- How to LPCTSTR Convert to char * (C++)
- adding data into an char array (C++)
- Question regarding reading from registry (Win32) VC6 (C++)
Other Threads in the C++ Forum
- Previous Thread: word count
- Next Thread: How to block unauthorized windows service stop?????
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets







