creating an array with a struct

So Im trying to create an array with a size equal to the course variable of studenta. What am I doing wrong?
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
#include <iostream>
#include <string>
using namespace std;

struct Student {
	string firstName;
	string lastName;
	string aNumber;
	int courses;
	double gpa;
};

struct Course {
	int courseNumber;
	string courseName;
	int creditHours;
	char grade;
};

Student readStudent();
void readCourseArray(Student);

int main () {
	Student studenta;
	studenta = readStudent();
	readCourseArray(studenta);

		cout << studenta.firstName << endl;

system ("pause");
return 0;
}

Student  readStudent() {
	Student student1;

	cout << "What is your first name? ";
	cin >> student1.firstName;
	cout << "What is your last name? ";
	cin >> student1.lastName;
	cout << "What is your A number? ";
	cin >> student1.aNumber;
	cout << "How many classes are you taking? ";
	cin >> student1.courses;

	return student1;
}

void readCourseArray(Student studenta) {
	Course courseArray[studenta.courses];
}
closed account (Dy7SLyTq)
the ugly way (in my opinion) is you have to do this:
Course courseArray = new Course[sizeof(studenta)]

of course it would be better to use std::vector<Course> courseArray (sizeof(studenta));

edit:
Course has to be a pointer
Last edited on
My instructor wouldn't allow the second option, because he hasnt covered it yet. But the first wasnt working.
closed account (Dy7SLyTq)
im not actually sure i did it right hold on
If you're going to do this with arrays, you're going to need a constant to define the size of the array.
 
const int MAX_STUDENT = 100;


Then declare studenta as an array.
 
Student studenta[MAX_STUDENTS];


You're going to need to keep track of the number of students read into each entry:
 
studenta[num_students++] = readStudent();


Not sure what you want to do with the course array.

@DTScode - new Course[sizeof(studenta)]
That going to allocate a number of occurrances of Course based on the byte size of a single Student instance. Probably not what you intended.

@AbstracionAnon Ive done it that way before, but I have to use the courses member variable. I cant just use a counter.

This is the prompt for the readCourseArray function:
-Use  the  student’s  number  of  courses  to  set  up  an  array.
-Prompts  the  user  for  information  about  each  course  the  student  has  taken:  the course  name,  the  course  number,  the  credit  hours,  and  the  student’s  letter  grade.  
Last edited on
I am completely stumped.
I'd change your Student struct as follows:
1
2
3
4
5
6
7
8
struct Student 
{  string firstName;
    string lastName;
    string aNumber;
    int num_courses;
    Courses * courses;
    double gpa;
};


Now change line 44 as follows:
1
2
3
    cin >> student1.num_courses;
    //  Allocate a dynamic array to hold the courses for this student
    student1.courses = new Course [num_courses];

Now you want to loop through the number of courses prompting the user for the information for each course for that student.

Of course, Course must be declared before Student.
Last edited on
The code you have seems to be compiling fine on my computer. Only warning is that Course courseArray[studenta.courses]; is not being used. What error are you getting exactly?

g++ -Wall -W -o tempStu Stu.cc
Stu.cc: In function ‘void readCourseArray(Student)’:
Stu.cc:53:10: warning: unused variable ‘courseArray’ [-Wunused-variable]
Last edited on
@Smac89 its saying that studenta.courses that defines the array, as in courseArray[studenta.courses], needs to be a constant value.
Ok then what you have to do is either compile with the option -std=c++11 or make the create a dynamic course object so:

Course *courseArray = new Course[studenta.courses];
Ok then what you have to do is either compile with the option -std=c++11


That will have no effect whatsoever. It's not any more legal in C++11 than it is in any other version of the standard. You can use the -ansi flag when compiling to disable nonstandard extensions if you're using a gcc variant.
Topic archived. No new replies allowed.