Hello ace184,
I made some changes to your program, but not sure if you would want to use them.
jonnin's example using the do while loop is the same as what I did, but after this your function does not make use of the variable "numStudents". It just inputs information for one student and returns to the main menu.
An Idea I had is to create two structs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef STUDENTINFO_HPP
#define STUDENTINFO_HPP
struct Courses
{
std::string s_CID;
std::string s_Course;
int s_Credits;
};
struct StudentInfo
{
std::string s_sid;
std::string s_firstName;
std::string s_lastname;
std::vector<Courses> s_vCourses;
// <--- This can be avoided by entering information directly into the struct variables.
void SetInfo(std::string sid, std::string firstName, std::string lastname);
};
#endif // !STUDENTINFO_HPP
|
In "main" you could create a vector of type "StudentInfo", something like
std::vector<StudentInfo> vStudentInfo;
, to hold the information about each student. And from the "addStudent" function you could call the "addCourse" function passing the temporary "StudentInfo" struct to fill the vector about the courses. When you return from the "addCourses" function put the completed struct into the vector.
I will take this opportunity to say that the function "exit()" is a bad choice for a name as there is a built in function called "exit(1)" that takes one parameter of type int. Any number greater than zero denotes an error or a problem. Your function having no parameter makes it an overloaded function and that is why it works. Since the menu choice is "Save and Exit" call the function "saveAndExit()" and avoid any confusion or problems.
That said by passing the vector "vStudentInfo", defined in "main", to the "saveAndExit" function you will have something to work with and your output would keep the information about each student and the courses together making it easier to understand. This way you could write to "StudentFile.txt" the way you have and create a second file with different output that would make it easier for the program to read in the future, (thinking of something like a CSV file).
Another problem I see if that each function ends with writing the information just input to a file. Done properly this would create a section of 4 or more student followed by the courses which would be a separate section of courses information with the only thing tying them together being the student ID. Not the greatest idea for creating a file, but it may work for you.
I do not know what information you were given to write this program, but it would be interesting to know.
Most of your code is useful and usable just not quite in the right order or done at the right time.
There is something to think about.
Hope that helps,
Andy