# Thread: Converting character array to integer array

1. ## Converting character array to integer array

It seems like my old thread had died because the people helping me are now offline and no one else is repsponding so I am just gonna start this new post.

This is an assigment I recieved for one of my classes:

Write a program that reads two positive integers that are 20 or fewer digits in length and then ouputs their sum. Your program will read the digits as values of type char so that the number 1234 is read in as the four characters '1', '2', '3', '4'. After they are read into the program that characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elments in the array ater the data is read. Perform the addition by implementing hte usual pencil-and-paper algorithm. The result of the addition is stored in an array the same size as the input arrays and the result is written to the screen. If the result of the addition is an iteger with more than the maximum number of digits, the program should print a message saying it has encountered "integer overflow".

So far this is what I have written:

Code:
```include <iostream>
using namespace std;

const int Size = 20;
int main ()
{

char FirstNumber [Size];
char SecondNumber [Size];
int Length;
int Length2;
int Num1 [Size];
int Num2 [Size];

cout<<"Enter the First Number : ";
cin>>FirstNumber;

cout<<"Enter the Second Number : ";
cin>>SecondNumber;

Length = strlen (FirstNumber);

Length2 = strlen (SecondNumber);

Num1 [Length] = FirstNumber [Size] - '0';
Num2 [Length2] = SecondNumber [Size] - '0';

cout<<Num1 [Length - 1];

return 0;
}```
At this point I am attempting to convert the characters to integers. When I cout<Num1 [Length - 1] I get some trash number and not the actual number. Am I converting things correctly?

2. I don't see why you have been told to read the numbers in as strings (char arrays). You should just read them in as integers.

3. I know but thats how he wanted us to do it

4. The only way I know how to do this is with atoi. Though from what I gather from your posts, you're probably hemmed into doing it the way you have above, which in that case you'll have to wait for other help.

Code:
```int main ()
{

char FirstNumber [Size];
char SecondNumber [Size];

int Num1;
int Num2;

cout<<"Enter the First Number : ";
cin>>FirstNumber;

cout<<"Enter the Second Number : ";
cin>>SecondNumber;

Num1  = atoi(FirstNumber);
Num2  = atoi(SecondNumber);

cout<<Num1<<std::endl;
cout<<Num2<<std::endl;

cin.get();

return 0;
}```

5. >> It seems like my old thread had died because the people helping me are now offline and no one else is repsponding so I am just gonna start this new post.

This is a bad idea. I think it is against the rules, and regardless it leaves people with no idea what previous advice has been given. You end up getting confusing and conflicting advice from all over the place. If I understand your situation correctly, neither of the posts in this thread will help you, but they might have been able to help better if they had seen your other 3 threads on this subject.