I'm having some problems with changing an array of numbers of type char to type int. Every time i try to sum 2 array indexed values it returns some letter or symbol. Also, if i change the type of the array in the functions the compiler gives me an error message. I would also like to add that the problem requires that the first two arrays be char so each individual number gets assigned to a different value.
My current code is:
Code:
#include <iostream>
void input(char a[], char b[], int& size_a, int& size_b);
void convert(char a[], int size);
void reverse(char a[], int size);
void add(char a[], char b[], int c[], int size);
int main()
{
using namespace std;
char a[20], b[20];
int c[20];
int size_a, size_b;
input(a, b, size_a, size_b);
convert(a, size_a);
convert(b, size_b);
a[0] += b[0];
cout << a[0] << " " << b[0] << endl;
reverse(a, size_a);
reverse(b, size_b);
add(a, b, c, 3);
cout << a << " " << b << " " << c << endl;
}
void input(char a[], char b[], int& size_a, int& size_b)
{
using namespace std;
cout << endl
<< "Enter a number with up to 20 digits:\n";
cin >> a;
cout << "Enter the amount of digits it has.\n";
cin >> size_a;
cout << "Enter a second number to be summed to the first:\n";
cin >> b;
cout << "Enter the amount of digits it has.\n";
cin >> size_b;
cout << endl;
}
void convert(char a[], int size)
{
using namespace std;
for (int i = 0; i < size; i++)
{
a[i] = static_cast <int> (a[i]);
}
}
void reverse(char a[], int size)
{
using namespace std;
int b[size];
for (int i = 0; i < size; i++)
{
b[i] = a[i];
}
for (int j = 0; j < size; j++)
{
a[j] = b[size - (j + 1)];
}
}
void add(char a[], char b[], int c[], int size)
{
using namespace std;
for (int i = 0; i < size; i++)
{
c[i] = a[i] + b[i];
if (c[i] >= 10)
{
c[i] -= 10;
c[i+1] += 1;
}
}
}