I'm trying to write a program that has a user input student names, their ages, their GPAs, and their graduations dates (i.e. 'F13', F for Fall, S for Spring, then the last two digits of the year). This information goes into a linked list, then the user can search a name or part of a name to find students and display their information.
#include <iostream>
#include <cstring>
using namespace std;
const char NAME_SIZE = 50;
struct StudentInfo
{
char studentName[NAME_SIZE];
int age;
double gpa;
char graduationSemester[4];
StudentInfo *next;
};
void displayStudentNames(StudentInfo *top);
void displayStudentInfo(StudentInfo *top);
int main(){
StudentInfo *top = 0;
cout << "Please enter the students. Enter the name, age, gpa, and semester of graduation (e.g. F13)." << endl;
cout << "Enter an empty name to stop." << endl << endl;
bool done = false;
while(!done){
char graduationBuffer[4];
char nameBuffer[NAME_SIZE];
cin.getline(nameBuffer, NAME_SIZE);
if(nameBuffer[0] != 0){
//create new struct in list
StudentInfo *temp = new StudentInfo;
strcpy(temp->studentName, nameBuffer);
//do-while loop for age input & validation
do{
cin >> temp->age;
if(temp->age < 1 || temp->age > 120){
cout << "Please choose an age between 1 and 120." << endl;
}
}while(temp->age < 1 || temp->age > 120);
//do-while loop for gpa input & validation
do{
cin >> temp->gpa;
if(temp->gpa < 0 || temp->gpa > 4){
cout << "Please choose a gpa between 0 and 4." << endl;
}
}while(temp->gpa < 0 || temp->gpa > 4);
cin.getline(graduationBuffer, 4);
strcpy(temp->graduationSemester, graduationBuffer);
cin.ignore(80, '\n');
temp->next = top;
top = temp;
//else statement occurs when user input is ended by empty name
}else{
//function is called to display all input names
displayStudentNames(top);
//function is called to ask for name to search for
displayStudentInfo(top);
//after functions are called, boolean set to true to end the program
done = true;
}
}
}
//function to display the names of students in the linked list
void displayStudentNames(StudentInfo *top){
cout << "Here are the students that you entered: " << endl << endl;
while(top){
cout << top->studentName << endl;
top = top->next;
}
cout << endl;
}
//function to search for student and display info
void displayStudentInfo(StudentInfo *top){
char name[NAME_SIZE];
StudentInfo *node = top;
bool done = false;
while(!done){
cout << "Which student do you want? ";
cin.getline(name, NAME_SIZE);
if(name[0] != 0){
do{
const char *str = top->studentName;
const char *substr = name;
const char *index = str;
if(node->studentName == strstr(node->studentName,name)){
cout << "Name: " << top->studentName;
cout << ", Age: " << top->age;
cout << ", GPA: " << top->gpa;
cout << ", Graduation Date: " << top->graduationSemester;
cout << endl << endl;
}
node = node->next;
}while(node != NULL);
}else{
cout << "No students found." << "Goodbye!" << endl;
done = true;
}
}
}
I am having a few problems, however. 1) when I use the displayStudentNames function, the names are being printed in reverse order from what I put in, 2) graduation dates aren't being printed out, so I'm not sure if they're being stored correctly, if at all, 3) when I use the displayStudentInfo function, it either works incorrectly, or crashes the program.