| | |
how to convert char to int
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







