PointersLab

Hi guys I keep getting some errors in this code if you help explain it, I would
Greatly be thankful

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>

int* addToSize (int*, int);

using namespace std;

	int* addToSize(int* newArray, int newSize) 
{
	    int* expandSize= new int [newSize +1];
	
	        for (int index = 0; index < newSize; index++)
	        {
		expandSize[index]= newArray[index];
	        }
    for (int index = newSize; index < (newSize+1); index ++) 
	{
		expandSize[index]=0;
	}	
	return expandSize;
}

int main()
{ 	
	
	int userInts;
	int usersSize=0;
	int *memory; //dynamically allocate an array
	int num;
	
	
	cout << "User Please enter new array size\n";
	cin >> usersSize; 
	
	memory = new int [usersSize];
	
	for (int count = 0; count < usersSize; count ++)
	{
		cout << "Please enter the value for " << count+1 << endl;
		cin >> userInts;
	}
	
	    for (int index = 0; index < usersSize; index ++)
        {
        cin >> num[index];
        }
	
	num = addToSize(userArray, usersSize);
   
	for(int index=0;index< (userSize + 1);index++)
	
		cout<<num[index]<<endl;

	delete[] num;	//Used to delete memory
	num = 0;
	
    return 0;
    
}
Post the error messages.
int num; // integer variable called num.

You cant use num as an array, since it's not an array.

1
2
3
 cin >> num[index];
cout<<num[index]<<endl;
delete[] num;


You never create a variable or an array called userArray, so it doesnt exist.
num = addToSize(userArray, usersSize);

Sometimes you use num as an array, and sometimes as a singlevariable. You can't have it both ways.
In function 'int main()':
56:25: error: invalid types 'int[int]' for array subscript
59:18: error: 'userArray' was not declared in this scope
61:26: error: 'userSize' was not declared in this scope
63:18: error: invalid types 'int[int]' for array subscript
65:11: error: type 'int' argument given to 'delete', expected pointer
Please stop posting so many threads:
http://www.cplusplus.com/forum/general/175726/
http://www.cplusplus.com/forum/beginner/175886/

You are being incredibly rude.
Topic archived. No new replies allowed.