Ok i am working on a program for class to add to big numbers. The numbers are entered as Chars then converted to int. Then i need to place them into a dynamically allocated array with a pointer. I need to do this twice to make two arrays and the pass both arrays to a function and add them together. First i am working on just getting the first array built and filled. Here is my code.

int _tmain(int argc, _TCHAR* argv[])
  
{ int size;
char data1;
int *pointer1;

cin>>size;
cin>>data1;
 data1=data1-48;
pointer1=new int[size];
for(int i=0;i<size;i++)
{pointer1[i]=data1;
       
  cout<<*pointer1;
}


 return 0;
}

It needs some work because it only prints the first char i enter. So if i enter 3 for the size and 123 for the data. My array prints 111. I need 123. Also does anybody know how i can enter the data first then find the size of the data so i don't have to enter that from the keyboard.

It needs to work like if i enter 123 for data. It will automatically make size equal 3 then print the array 123.

Edited 6 Years Ago by Jeff_5_7: n/a

The code below shows you how dynamic arrays work. Hope it answers your question.

#include <iostream>

using namespace std;

int main()
{
	int *p = 0;

	cout << "Forward: ";
	for(int x=1; x<=10; x++)
	{
		p = new int;
		*p = x;

		cout << *p << " ";
	}

	cout << endl << endl;

	cout << "Backward: ";
	for(int x=1; x<=10; x++)
	{
		cout << *p << " ";

		--*p;
	}

	cout << endl << endl;

	system("pause"); // Or Ctrl + F5 when building the project in Visual Studio
	return 0;
}

Edited 6 Years Ago by Jiwe: n/a

You need to input multiple digit numbers, right? So you need arrays for your data. What's the largest number you want to handle? Create arrays at least that large.

The way I did it is have 1 input array (char) and 3 calculation arrays (2 number and 1 answer)

1) Set all the calculation arrays to 0's
2) Input one entire number into the input array
3) Starting at the end of the input number, move and convert each character into the first number array, starting at the end moving to the beginning.
4) Repeat for the next number

Example -- if all arrays are 10 long, and your first number entered is 163, input[2] (the '3') => num1[9] (as 3, not '3')
Continue with input[1] (the '6'), etc.

You need to input multiple digit numbers, right? So you need arrays for your data. What's the largest number you want to handle? Create arrays at least that large.

The way I did it is have 1 input array (char) and 3 calculation arrays (2 number and 1 answer)

1) Set all the calculation arrays to 0's
2) Input one entire number into the input array
3) Starting at the end of the input number, move and convert each character into the first number array, starting at the end moving to the beginning.
4) Repeat for the next number

Example -- if all arrays are 10 long, and your first number entered is 163, input[2] (the '3') => num1[9] (as 3, not '3')
Continue with input[1] (the '6'), etc.

Not exactly i am entering 2 big numbers of no more that 20 digits. Such as 1234 for the 1st and 45678 for the second. I need to enter the values as Char converted to int then put in array with pointer. Do this for both then send both arrays to an addition funcition where the anser is put in the third array.

This article has been dead for over six months. Start a new discussion instead.