Struct and nested structs

I'm having trouble how to figure out this problem I'm assigned. When I'm referencing the code I keep getting a message saying "no matching function for call to 'getStudentInfo'.

//Here's what I'm supposed to do

Objectives:
 Structured Data: struct, array of struct, and nested struct
Assignment: Write a C++ program to record and display a list of student records.
1. Define a structure to hold the following information of a Student:
 char name[NAME_SIZE];
 char id[ID_SIZE];
 int years;
 double gpa;
 Course courses[COURSE_SIZE];
2. Define a structure to hold the following information for a Course
 char cName[CNAME_SIZE];
 int cNum;
 char days[DAYS];
 char instructor[NAME_SIZE];
3. Define a array variable students to hold students’ records
4. getStudentInfo(Student [ ], int&) This function asks user to input data for student name, id, years, gpa,
and the courses he/she is taking this semester.
5. getCourses(Course [ ], int &) This function is called by getStudentInfo to get the information of the
courses for a student.
6. displayStudents(Student [ ], int) This function displays the students information
Sample output:
Student Name: John Smith
Student ID: 000123456
Year in school: 2
GPA: 4.0
Course Name: CSI
Course Number: 1470
Schedule: MWF
Instructor: Wei
More courses? y
Course Name: DataStructure
Course Number: 2320
Schedule: TH
Instructor: Jim
More courses? n
More students? y
Student Name: Peter Pan
Student ID: 000234567
Year in school: 10
GPA: 1.0
Course Name: HowToFly
Course Number: 1000
Schedule: MTWHFS
Instructor: Captain Hook
More courses? n
More students? n
Here shows the students’ records:
******************
Student Name: John Smith
Student ID: 000123456
Year in school: 2
GPA: 4.0
Courses:
Name:CS1
Number: 1470
Schedule: MWF
Instructor: Wei
Name: DataStructure
Number: 2320
Schedule: TH
Instructor: Jim
*****************
Student:
Name: Peter Pan
......
......

// And here's my current code


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int NAME_SIZE = 50;
const int ID_SIZE = 10;
const int COURSE_SIZE = 10;
const int CNAME_SIZE = 50;
const int DAYS = 5;

struct Course{
char cName[CNAME_SIZE];
int cNum;
char days[DAYS];
char instructor[NAME_SIZE];
};

struct Student{
char name[NAME_SIZE];
char id[ID_SIZE];
int years;
double gpa;
Course courses[COURSE_SIZE];
};

Student getStudentInfo(Student [ ], int&);
void getCourses(Course [ ], int &);
void displayStudents(Student [ ], int);

int main()
{
const int NUM_STUDENTS = 12;

//Decalring a new array
Student *students = new Student[NUM_STUDENTS];
getStudentInfo(students[NUM_STUDENTS], &NUM_STUDENTS)
return 0;
}

Student getStudentInfo(Student stu[ ], int &numstu)
{
// Get the student name.
cout << "Student name: ";
cin >> stu-> name;

// Get the student ID number.
cout << "Student ID Number: ";
cin >> stu-> id;

// Get the credit hours enrolled.
cout << "Credit Hours Enrolled: ";
cin >> stu-> years;

// Get the GPA.
cout << "Current GPA: ";
cin >> stu-> gpa;
}
you appear to be missing ; in main's call to it.

also, you call it with one item but its a pointer. That is, you call it with a dereference into your array but the function wants the whole array, you want this I think:

getStudentInfo(students, & NUM_STUDENTS); //no [value] in students

if you use new, there should be a delete statement.
here, at the end of main, that would be
delete[] students;
Last edited on
In future please use code tags when posting code.

Your problem is that your function call is trying to pass a single Student when your function is expecting an array of Student.

By the way it's a good thing that the program doesn't compile because you're also trying access the array out of bounds in that function call, remember arrays start at zero and end at size - 1. That function call is trying to access array[size] which is out of bounds.

This is the line in question: getStudentInfo(students[NUM_STUDENTS], &NUM_STUDENTS) also note the missing semicolon at the end of this function call.

Since NUM_STUDENTS is a compile time const you really don't need to use dynamic memory to allocate memory for the array of students, you could just use a "normal" non-dynamic array: Student students[NUM_STUDENTS];

students[NUM_STUDENTS] means you want to access array element at index NUM_STUDENTS (out of bounds). If you want to pass (a pointer to the first element in) the array to the function you should simply use students.

I think the second argument is supposed to be a variable that keeps track of the number of students that you are currently storing in the array. The function can use this variable to know where in the array it should insert the new student, and because it is passed by reference it can also update it so that the caller don't have to do it each time.

1
2
int numberOfStudents = 0;
getStudentInfo(students, numberOfStudents);
Topic archived. No new replies allowed.