I have a homwework assignment that I can't get to work correctly at all in one area, specifically where I'm trying to compare strings. Here is the assignment:
You will write a program that prompts the user for student names, ages, gpas, and graduation dates. Your program will then read in all the student information and store them into a linked list. The program will then print the names of the students. Next, the program will prompt the user for a string. The program will print the complete information for each student containing the string in the name.
Here's what I have:
#include <iostream>
#include <cstring>
using namespace std;
const char NAME_SIZE = 50;
struct StudentInfo
{
char studentName[NAME_SIZE];
int age;
double gpa;
char graduationSemester[3];
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 nameBuffer[NAME_SIZE];
char graduationBuffer[3];
cin.getline(nameBuffer, NAME_SIZE);
if(nameBuffer[0] != 0){
StudentInfo *temp = new StudentInfo;
strcpy(temp->studentName, nameBuffer);
cin >> temp->age;
cin >> temp->gpa;
cin.getline(graduationBuffer, 3);
strcpy(temp->graduationSemester, graduationBuffer);
cin.ignore(80, '\n');
temp->next = top;
top = temp;
}else{
displayStudentNames(top);
displayStudentInfo(top);
done = true;
}
}
}
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;
}
void displayStudentInfo(StudentInfo *top){
char name[NAME_SIZE];
do{
cout << "Which students do you want? ";
cin.getline(name, NAME_SIZE);
const char *str = top->studentName;
const char *substr = name;
const char *index = str;
while((index = strstr(index,substr)) != NULL){
cout << "Name: " << top->studentName << ", Age: " << top->age << ", GPA: " << top->gpa << ", Graduations Date: " << top->graduationSemester;
index++;
}
}while(name[0] != 0);
}
My problem is happening in the displayStudentInfo function, I just can't make it work properly. I've tried many different things and this is just the latest thing I've tried. But after creating the linked list earlier in the program, we are supposed to enter a string ranging from a letter to a full name and find it anywhere in the list, then print out the information for that particular name.
eta: I'm also having a problem with my linked list storing the structures backwards? It also either isn't storing my graduation dates, or something's going wrong when I try to print them, because they print blank.