Converting char into int

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 44
Reputation: c++ prog is an unknown quantity at this point 
Solved Threads: 1
c++ prog's Avatar
c++ prog c++ prog is offline Offline
Light Poster

Converting char into int

 
0
  #1
Nov 9th, 2007
i'm new to c++....i'm making a project here but i have a problem with this: reading values as type char and converting it to type int...such that 54 can be read as '5' and '4' but after that it will be read into type int...can anyone please tell me how?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Converting char into int

 
0
  #2
Nov 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Converting char into int

 
0
  #3
Nov 9th, 2007
>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:
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. std::string line;
  8.  
  9. std::cout<<"Enter a number: ";
  10.  
  11. if ( getline ( std::cin, line ) ) {
  12. std::stringstream conv ( line );
  13. int result;
  14.  
  15. if ( conv>> result )
  16. std::cout<< result <<" squared is "<< result * result <<'\n';
  17. else
  18. std::cerr<<"Invalid input\n";
  19. }
  20. }
If you write your own conversion it might look something like this C implementation:
  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4.  
  5. const char *parse_int ( const char *s, int *value )
  6. {
  7. /* Base of the final converted value */
  8. const unsigned base = 10;
  9.  
  10. /* Largest value that won't ever overflow */
  11. const unsigned limit = UINT_MAX / base;
  12.  
  13. /* Largest value that won't overflow beyond the limit */
  14. const unsigned top_digit = UINT_MAX % base;
  15.  
  16. unsigned overflow = 0; /* True if integer overflow occurs */
  17. unsigned sign = 0; /* Final sign of the converted value */
  18.  
  19. unsigned temp = 0; /* Intermediate converted value */
  20. unsigned n = 0; /* Count of converted digits */
  21.  
  22. /* Save and skip over the sign if present */
  23. if ( *s == '-' || *s == '+' )
  24. sign = *s++ == '-';
  25.  
  26. /* Build the intermediate value */
  27. for ( ; isdigit ( *s ); s++, n++ ) {
  28. unsigned digit = *s - '0';
  29.  
  30. /*
  31.   This protects *only* the intermediate value
  32.   from overflow. Overflow of the final value
  33.   requires further checks
  34.   */
  35. overflow = temp > limit
  36. || ( temp == limit && digit > top_digit );
  37.  
  38. if ( overflow )
  39. break;
  40.  
  41. /* Shift-add by the base */
  42. temp = temp * base + digit;
  43. }
  44.  
  45. if ( n > 0 ) {
  46. /*
  47.   A conversion was made, so now we need to
  48.   deal with overflow and set the final value
  49.   */
  50. if ( overflow
  51. || ( sign && temp > -INT_MIN )
  52. || ( !sign && temp > INT_MAX ) )
  53. {
  54. /*
  55.   The intermediate actually overflowed,
  56.   or converting it to int would overflow.
  57.   Either way it's an error to the caller
  58.   */
  59. errno = ERANGE;
  60. temp = sign ? -INT_MIN : INT_MAX;
  61. }
  62.  
  63. *value = sign ? -(int)temp : (int)temp;
  64. }
  65. else if ( sign ) {
  66. /*
  67.   We found a sign and skipped over it. But
  68.   because no conversion was made, we need
  69.   to "unskip" the sign
  70.   */
  71. --s;
  72. }
  73.  
  74. return s;
  75. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC