Page 1 of 1

Reputation:

# Convert char array to int array

Posted 07 April 2010 - 01:44 PM

Hi, I am new to the site and am having trouble with converting a char array in a function and returning it back to main. The program specs call for checking to make sure it is a digit in the function, and then converting each part of array element by element. I have included my code so far. I am not looking for the answer but just for one piece of this so that I can finish the other parts of the program.

//This program will prompt a user for a series of numbers and
//will total them
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <conio.h>
using namespace std;

int converttoInt(char *, int);

int main()
{
const int INPUT_SIZE = 81; // Size of input array
char input[INPUT_SIZE]; //Array to hold numbers
int total=0; //Variable to hold numbers added

cout << "Please input a series of numbers without spaces and I will total them? "<<endl;
cin.getline(input, INPUT_SIZE);
total = converttoInt(input, INPUT_SIZE);

cout << "The total of the inputted numbers are " << total << endl;
_getch();
return 0;
}
int converttoInt(char *strPtr, int size)
{
int total =0;
if (isdigit(strPtr[size]))
{
while (*strPtr != '\0')
{
for (int count = 0; count < size; count++)
total += strPtr[size];
}
}
else
return 0;
}

Thanks for any help or pointers.

Joe

Is This A Good Question/Topic? 0

## Replies To: Convert char array to int array

### #2 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: Convert char array to int array

Posted 07 April 2010 - 02:09 PM

convert a char array to an int, or convert a char array to an int array because each one is different. If your just trying to convert to an in take a look at stringstream or atoi()

### #3 japanir

• jaVanir

Reputation: 1012
• Posts: 3,025
• Joined: 20-August 09

## Re: Convert char array to int array

Posted 07 April 2010 - 02:15 PM

i guess size is the length of the array.
if so, this code is an error:
```if (isdigit(strPtr[size]))
```

remember that in an array of size values, valid indexes are int values between 0 and size - 1.
so the last value in your array shuld be
```strPtr[size-1]
```

now, i think that the way you wrote the function is not right.
it should be arranged that way:
iterate the char array
for every char, check id it is a digit.
if yes, add it to the new int array.
else, continue to the next value.

also, to convert a char to an int, you have to remove the ASCII code, by taking out the value of '0'.
as:
```int num = strPtr[i] - '0';
```

Reputation:

## Re: Convert char array to int array

Posted 07 April 2010 - 03:02 PM

Thank you for your help. To answer Imasexy, I am trying to get the input into the char array "Input". Then I am supposed to pass it to the converttoInt function for checking if it is digit and totaling. Then return that back to main.

Thank you very much japanir. I am using your logic in the function and build/testing it to see outcome. That is what I needed was some guidance.

Joe