0

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.

Emily
  • 11
  • 3
  • 3
    Does the assignment require you to use `char` arrays rather than [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string)>? `std::string` will eliminate most of the manipulation problems you will encounter with `char` arrays. – user4581301 Dec 01 '15 at 19:50
  • Don't you want `str(n)cmp` (compare strings) here instead of `strstr` (find substring)? – crashmstr Dec 01 '15 at 19:56
  • I would suggest to stop messing around with C stuff (strstr, strcpy, hand writen linked lists, etc...) and use C++ containers ans stings, you would make your task far easier – OznOg Dec 01 '15 at 19:56
  • He told us to use the strstr() function, and gave us examples, which had char arrays in them. I figured it'd be easier to use those throughout then. – Emily Dec 01 '15 at 20:01
  • I think for learning purposes (ie. an assignment), the C stuff is important. Sure, `std::string` is cleaner, easier to maintain, etc. Before learning this though, one needs to understand how it works. char arrays also happen to be very effective for understanding pointers. – Michael Oliver Dec 01 '15 at 20:06
  • @MichaelOliver Myself and Stroustrup disagree with you. – PaulMcKenzie Dec 01 '15 at 20:31
  • Chicken and egg problem, @MichaelOliver. I sit pretty firmly in the camp that recommends teaching logic and program building first so that students have some basis of reference to understand what the underbelly does. – user4581301 Dec 01 '15 at 20:31

2 Answers2

3

Your code problem is obvious. You are just checking head of list and not traversing into the list.

Here is a part that has problem(Assuming each function call should return info for 1 student):

void displayStudentInfo(StudentInfo *top){
    char name[NAME_SIZE];

    //Addes 'node' variable for simplicity, you can use 'top' itself
    StudentInfo *node = top;

    cout << "Which students do you want? ";
    cin.getline(name, NAME_SIZE);
    do{
        // Check if name is correct
        // "Entered name should be at start of field"
        // You should use == not =
        if(node->studentName == strstr(node->studentName, name){
            cout << "Name: " << top->studentName << ", Age: " << top->age << ", GPA: " << top->gpa << ", Graduations Date: " << top->graduationSemester;
        }
        // Traverse in list
        node = node->next;
        // Until reach end of list
    }while(node != NULL);
}
Afshin
  • 8,839
  • 1
  • 18
  • 53
  • The `if` statement is comparing pointers, not content of the strings. – Thomas Matthews Dec 01 '15 at 20:06
  • I know, I just added `node->studentName == ` part to make sure entered name is at start of name saved in list. `strstr()` will return position of string in content which can be anywhere in that string. so I validate it is at start of string with that `==`. Then checking that it is not null. – Afshin Dec 01 '15 at 20:08
  • Your `==` operator will return a boolean, and you're comparing that to `NULL`. That's bad. – Michael Oliver Dec 01 '15 at 20:11
  • oh...my bad. I thought this is a c question, not c++. Updating. :D – Afshin Dec 01 '15 at 20:13
  • Ah that makes sense. `NULL == 0 == false` in C, I forgot. – Michael Oliver Dec 01 '15 at 20:15
  • EDIT: I guess it it OK now. Because even if `strstr()` returns NULL, comparison will return false(node->studentName is a structure member and cannot be NULL). – Afshin Dec 01 '15 at 20:17
  • If I put a full name in for a student, if I search the last name it doesn't pull up any names? – Emily Dec 02 '15 at 03:37
0

You need to add top = top->next somewhere inside displayStudentInfo, similarly to how you have done in displayStudentNames. Currently your loop does not iterate through the linked-list without this.

I won't post any code to avoid doing your homework, but feel free to ask me more questions or for clarification.

Michael Oliver
  • 1,392
  • 8
  • 22