Not Yet Answered # Converting char into int

Ancient Dragon 5,243 Narue 5,707

0

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).

0

>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;
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`