# Thread: Question involving converting array->int

1. ## Question involving converting array->int

I have an array (char iiword[256];).
I filled it with iistream.get()s.
When i use printf("%s", iiword); the actual number I want is displayed.
However, i do not know how I can convert the printed number into an integer.
Anyone have any idea how to do this? Thanks.
-Drekenzin

2. Just write a simple function that accepts the character and converts it to it's respective integer. Basically something like:
Code:
```int convert(char number)
{
if(number < '0' || number > '9'){
return -1; //check for -1 when using function
}
switch(number)
{
case '0':
return 0;
case '1':
return 1;
//etc.....
}
}```
Just loop through the array and check to be sure the chars are really 0-9 with a similar if statement like above, then send that element of the array to the function. Reply if this doesn't work and I will try to fix it(I did that out of my head).

3. The thing is, the array holds the individual digits of the integer.
for example:
iiword[0] = 1
iiword[1] = 5
iiword[2] = 9
...
i'd want to get 159 set to an integer.
do you know how to do this?

4. Oh, well in that case you could do something like this:

Code:
```int convert(char *string)
{
int tempnumber = 0;
int number = 0;
int place;
for(unsigned int i=0; i<strlen(string); i++)
{
switch(string[i])
{
case '0':
tempnumber = 0;
break;
case '1':
tempnumber = 1;
break;
//etc...
}
//now find out what place value the number holds
if(!strlen(string)-i)
place = 1;
else{
place = strlen(string)-i;
number += (place * tempnumber);
}
}
return number;
}```
Something like that. You will have to complete the switch statement of course, and I am not sure if the place thing will work or not. Tell me if it does or doesn't.

5. nope, that doesn't work =(

6. Appearantly math isn't my strong point. Since our number system is base 10 I need to make the number 10 to the power of place * tempnumber. Stupid me! :-) Here is the revised code:

PHP Code:
``` int convert(char *string) { int tempnumber = 0; int number = 0; int place;    for(unsigned int i=0; i<strlen(string); i++)    {        switch(string[i])        {        case '0':              tempnumber = 0;              break;         case '1':              tempnumber = 1;              break;        case '2':              tempnumber = 2;              break;         case '3':              tempnumber = 3;              break;        case '4':              tempnumber = 4;              break;         case '5':              tempnumber = 5;              break;        case '6':              tempnumber = 6;              break;         case '7':              tempnumber = 7;              break;        case '8':              tempnumber = 8;              break;         case '9':              tempnumber = 9;              break;        }        //now find out what place value the number holds        place = strlen(string)-i;        number += (pow(10,place) * tempnumber);        cout << i << "\n";    } cout << "\n"; return number/10; }  ```
Be sure to include math.h for the pow function.

7. hi, you can go to the topic named "something wrong with this?". I posted a function where my code might be useful to you. this algorithm can be used without using strlen(); what it does is something like this:

Code:
```//replacement code (used inside the code to replace the strlen() )

long int storage=0; //initialize this outside the loop

//inside the loop :

int temp=<given number>;
storage=storage * 10 + temp; /* this statement can be used to replace strlen() */```

8. also, don't you mean istream.get() ?
is this better than getch() ?

9. 1. Input as char array
2. Find the length of the input.
3. Create a loop that goes from length minus one till zero.
4. Convert current number into an int
5. Multiply that int by 10^loop # and add it to current temp
6. Continue with loop until comple and return temp.