# part of an array to integer?

• 04-24-2004
eifersucht
part of an array to integer?
Hello, does anyone know if it is possible to convert part of an array e.g Sarray[1] to Sarray[5] to a single integer variable to perform calculation.

Am trying to do a calculator in mfc with buttons (nothing to hide.. its a homework) but the teacher doesnot code in cpp, so i have no choice except to code in vb which i don't code in. despite the teacher want to help me he can't since its cpp.

Am doing it using mfc.
my algo is as follows....

Assume am pressing button 5 , three times
char Sarray [9] //max numbers on display
counter=1;
int actualnum
{
when button 5 press
Sarray[counter]='5'
put Sarray[1 to counter] in actualnum; //where am having trouble
counter=counter+1;
}

The above doesnot have any real codes, except for the int and char etc.., i can manage with the rest, just wrote them in simple english for the easy understanding, Am stuck in the part where i would need to combine the
Sarray[1]=5;
Sarray[2]=5;
Sarray[3]=5;
into actualnum=555

Any idea?

Thanks :(
• 04-24-2004
Salem
If you're reading user input, then this is most likely
Sarray[1]='5';

Sarray[4] = '\0';

Then you can say
int myint = atoi( Sarray );
• 04-24-2004
ripper079
Quote:

Hello, does anyone know if it is possible to convert part of an array e.g Sarray[1] to Sarray[5] to a single integer variable to perform calculation.
Of course there is :), but I donīt know if this is a good solution. Start from the end-index and work your way to the beginning-index of the array. Beware of potential errors.
Code:

```// Do not //* Pass invalid array bounderis (zero based) e.i int array[5] -> range 0 to 4 //* Array with negative values //* pFromIndex greater than pToIndex //* Array with other than int elements int ConvertToNumber(const int *pArray, int pFromIndex, int pToIndex) {         int total = 0;         int multiply = 1;         for(int i = pToIndex; i >= pFromIndex; --i)         {                 total += (multiply*pArray[i]);                 multiply *= 10;         }         return total; }```
When you understand this code snippet it should be easy to convert it to your needs.
• 04-25-2004
enjoy
Code:

```int ConvertToNumber(const int *pArray, int pFromIndex, int pToIndex) { int total = 0; int multiply = 1; for(int i = pToIndex; i >= pFromIndex; --i) { if(pArray<=0){printf("error %i",i); total += (multiply*pArray[i]); multiply *= 10; } return total; }```
• 04-26-2004
eifersucht
wow! was not expecting to be helped that quick, thanks guys. I'll now experiment with the codes you gave me to understand it better. Thanks. I'll drop in another message after i've tried out all of them.

-eifer
• 04-27-2004
eifersucht
I used salem's code, since it was straight forward and simple.
Thanks again salem, ripper and enjoy. I'd be doomed if it you were not here.
-eifer