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;
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;
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);