| | |
Converting char into int
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I assume you are not allowed to use standard C functions such as atoi() ? If you have to do your own thing then just subtract '0' from the digit and add the result to an int accumulator. For the second digit multiply the accumulator by 10 and repeat the above process. Put all that in a loop and you can do as many digits as there are in the string, but you have to be careful of data overflow (converting more digits then the int accumulator can hold -- see limits.h for max possible value for your compiler and os).
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.
>can anyone please tell me how?
Use stringstream to handle the conversion for you. This relies on the existing formatted input code and saves you the trouble of writing the tricky algorithms yourself:
If you write your own conversion it might look something like this C implementation:
Use stringstream to handle the conversion for you. This relies on the existing formatted input code and saves you the trouble of writing the tricky algorithms yourself:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> int main() { std::string line; std::cout<<"Enter a number: "; if ( getline ( std::cin, line ) ) { std::stringstream conv ( line ); int result; if ( conv>> result ) std::cout<< result <<" squared is "<< result * result <<'\n'; else std::cerr<<"Invalid input\n"; } }
C++ Syntax (Toggle Plain Text)
#include <ctype.h> #include <errno.h> #include <limits.h> const char *parse_int ( const char *s, int *value ) { /* Base of the final converted value */ const unsigned base = 10; /* Largest value that won't ever overflow */ const unsigned limit = UINT_MAX / base; /* Largest value that won't overflow beyond the limit */ const unsigned top_digit = UINT_MAX % base; unsigned overflow = 0; /* True if integer overflow occurs */ unsigned sign = 0; /* Final sign of the converted value */ unsigned temp = 0; /* Intermediate converted value */ unsigned n = 0; /* Count of converted digits */ /* Save and skip over the sign if present */ if ( *s == '-' || *s == '+' ) sign = *s++ == '-'; /* Build the intermediate value */ for ( ; isdigit ( *s ); s++, n++ ) { unsigned digit = *s - '0'; /* This protects *only* the intermediate value from overflow. Overflow of the final value requires further checks */ overflow = temp > limit || ( temp == limit && digit > top_digit ); if ( overflow ) break; /* Shift-add by the base */ temp = temp * base + digit; } if ( n > 0 ) { /* A conversion was made, so now we need to deal with overflow and set the final value */ if ( overflow || ( sign && temp > -INT_MIN ) || ( !sign && temp > INT_MAX ) ) { /* The intermediate actually overflowed, or converting it to int would overflow. Either way it's an error to the caller */ errno = ERANGE; temp = sign ? -INT_MIN : INT_MAX; } *value = sign ? -(int)temp : (int)temp; } else if ( sign ) { /* We found a sign and skipped over it. But because no conversion was made, we need to "unskip" the sign */ --s; } return s; }
I'm here to prove you wrong.
![]() |
Similar Threads
- how to convert char to int (C++)
- converting from char to ASCII to char (C++)
- Conversion from Char* to int ? (C++)
- Convertings strings to numbers (C++)
- ascii to binary then to hex (C)
- converting a char array to a single char (C)
- Coverting varibles (C++)
- converting the date to an int (C++)
- Converting Int to Const Char (C)
- Converting a char array into numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: for loop question
- Next Thread: Homework help
| 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 parameter 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 visualstudio win32 windows winsock wordfrequency wxwidgets






