User determining Size of Array of Structs

Hi,

I need help with a homework assignment. I have a struct which contains some basic information about a student and their test grades. I need to have the user determine the number of structures to be defined. I know I need to use pointers, but I'm not sure how exactly to go about doing this.

I know how to use pointers to dynamically allocate an array, and have the user determine the amount of elements in the array. However, I keep running into errors when I try to use an array of structs.

Any help would be appreciated, thank you.
Hm? There is 0 difference whether you use a struct or an inbuilt datatype. What exactly did you write, and what errors do you get?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void getValues(struct *&iPtr)

int numStudents;

struct student{
char firstName[20];
char lastName[20];
int grades[4];
double average;
} *iPtr

int main(){
	getValues(iPtr);
	system("PAUSE");
}

void getValues(student *&iPtr){

	iPtr = new struct[numValues];
	for(int i=0; i<numStudents; i++){
		cin >> iPtr[i].firstName;
	}
}


I'm getting an error on the function header at *&iPtr that says "expected either a definition or a tag name"
struct *&iPtr - no datatype there.
what do I put?
closed account (D80DSL3A)
Line 1:void getValues(student *&iPtr) but move it to after line 10 else the compiler won't know what a student is yet.

Also, line 19 - where does numValues come from?
Sorry about line 19, that was a mistake.
Okay, I moved it and now I have no errors. Now I have this:

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
int numStudents;

struct student{
char firstName[20];
char lastName[20];
int grades[4];
double average;
} *iPtr;


void getValues(student *&iPtr);


int main(){

	getValues(iPtr);

	system("PAUSE");
}

void getValues(student *&iPtr){

	iPtr = new student[numStudents];
	for(int i=0; i<numStudents; i++){

		cout << "askdjfalsdfkj" << endl;
		cin >> iPtr[i].firstName;
	}
}


This was just a test still, and nothing prints out. I'm not sure why.
closed account (D80DSL3A)
numStudents is uninitialized. Isn't your compiler warning you about this?
Topic archived. No new replies allowed.