Dynamic Arrays & Pointers

I'm trying to add numbers that are larger than the max value for an int. So I'm storing the numbers in arrays, however, it isn't even letting me type numbers into my second array from the console. Any suggestions?

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;



int main(int argc, char** argv) {
	
	int *pVar, *qVar, *tVar;
	int arraySize;
	
	cout << "Enter array size: ";
	cin >> arraySize;
	pVar = new int [arraySize];
	qVar = new int [arraySize];
	
	cout << "Enter an integer: ";
	for (int i = 0; i < arraySize; i++)
	{
		cin >> pVar[i];
	}
	
	cout << "Enter another integer: ";
	for (int j = 0; j < arraySize; j++)
	{
		cin >> qVar[j];
	}
	
	tVar = new int [arraySize + 1];
	
	for (int k = 0; k < arraySize + 1; k++)
	{
		tVar[k] = pVar[k] + qVar[k];
	}
	
	for (int m = 0; m < arraySize + 1; m++)
	{
		cout << tVar[m];
	}
	
	
	return 0;
}
closed account (28poGNh0)
Are you sure check again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# include <iostream>
using namespace std;

int main(int argc, char** argv)
{
	int *pVar, *qVar, *tVar;
	int arraySize;

	cout << "Enter array size: ";
	cin >> arraySize;
	pVar = new int [arraySize];
	qVar = new int [arraySize];

	for (int i = 0; i < arraySize; i++)
	{
	    cout << "Enter integer " << i+1 << " : ";
		cin >> pVar[i];
	}

	for (int j = 0; j < arraySize; j++)
	{
	    cout << "Enter another integer " << j+1 << " : ";
		cin >> qVar[j];
	}

	tVar = new int [arraySize + 1];
	// this one should be altered with  "tVar = new int [arraySize];"

	for (int k = 0; k < arraySize + 1; k++)
	{
	    // and this loop is dangerous because the pVar[arraySize] does not exist
		tVar[k] = pVar[k] + qVar[k];
	}

	for (int m = 0; m < arraySize + 1; m++)
	{
		cout << "Sum " << m+1 << " : " << tVar[m] << endl;
		// I add this line to show you the last odd value so watch out!
	}

    // Also you should delete you xVar variables it is a role
    delete pVar;
    delete qVar;
    delete tVar;

	return 0;
}


Hope that helps
Topic archived. No new replies allowed.