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?
- 3 Contributors
- forum2 Replies
- 10 Views
- 11 Years Discussion Span
- comment Latest Post by Narue
Ancient Dragon 5,243
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).
Narue 5,707
>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:
#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";
}
}
If you write your own conversion it might look something like this C implementation:
#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;
}