0

So I've created a linked list that's supposed to print out name, age, major, and GPA from a file.

It seems to be working when I run it in Qt Creator, but when I try to run it in cloud9, it doesn't seems to be working out.

For example, the findStudent function isn't finding the student -- instead it comes up as "student not found."

It also doesn't seem to like the way I've formatted it, everything from the nodes (name, age, major, gpa) looks to be printing on top of eachother.

It does this unless I tab the info for each one over to the far right but that's not really what I want, since that ends up not looking that nice outside of cloud9.

studentlist.h:

#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>

using namespace std;

struct StudentNode{
    string name;
    int age;
    string major;
    double gpa;
    StudentNode *link;
};

void createLinkedList(StudentNode *&head);
void findStudent(StudentNode *&head, string find);
void removeStudent(StudentNode *&head);
double calculateGPA(StudentNode *&head);
void printList(StudentNode *&head);


#endif // STUDENTLIST_H

studentlist.cpp:

#include "studentlist.h"

void createLinkedList(StudentNode *&head){
    StudentNode *last = NULL;
    StudentNode *newNode = NULL;

    fstream inFile;

    inFile.open("inFile.txt");

    if (inFile.fail()){

        cout << "file failed to open" << endl;
        exit(1);
    }
    else{

        string newLine;

        getline(inFile, newLine);
        newNode = new StudentNode;
        getline(inFile, newNode->name);
        inFile >> newNode->age;
        getline(inFile, newLine);
        getline(inFile,newNode->major);
        inFile >> newNode->gpa;
        newNode->link = NULL;
        head = newNode;
        last = newNode;

        while(!inFile.eof()){
            getline(inFile, newLine);
            getline(inFile, newLine);
            newNode = new StudentNode;
            getline(inFile, newNode->name);
            inFile >> newNode->age;
            getline(inFile, newLine);
            getline(inFile,newNode->major);
            inFile >> newNode->gpa;
            newNode->link = head;
            head = newNode;
        }

    }

    inFile.close();
}

void findStudent(StudentNode *&head, string find){

    StudentNode *current = head;

    if(current->link ==  NULL){
        cout << "Student Not Found" << endl;
    }
    else if(current->name == find){
        cout << "Student Found:" << endl;
        cout <<left<< setw(15);
        cout <<current->name <<setw(7);
        cout << current->age<<setw(15);
        cout << current->major << setw(4);
        cout << current->gpa;
    }
    else{
        findStudent(current->link, find);
    }
}

void removeStudent(StudentNode *&head){
    StudentNode *temp_ptr;

    temp_ptr = head->link;

    cout << "Front Node Deleted:" << endl;

    cout << left << setw(15);
    cout << "Name" <<setw(7) << "Age" <<setw(15)
         << "Major" <<setw(4) << "GPA" <<setw(15) << endl;

    cout <<head->name <<setw(7);
    cout << head->age<<setw(15);
    cout << head->major << setw(4);
    cout << head->gpa;

    delete head;

    head = temp_ptr;

    temp_ptr = NULL;
}

double calculateGPA(StudentNode *&head){

    StudentNode *current = head;
    double calculatedGPA;
    int num_students=0;

    while(current!= NULL){

        calculatedGPA += current->gpa;
        current = current->link;
        num_students++;
    }

    cout << fixed << setw(3) << setprecision(2);
    return calculatedGPA/num_students;
}

void printList(StudentNode *&head){
    StudentNode *current = head;

    cout << left << setw(15) << "Name"
         <<setw(7)<<"Age" << setw(15)<< "Major"
        << setw(7) << "GPA" << endl;


    while(current!= NULL){

        cout << setw(15) << current->name<< setw(7);
        cout << current->age << setw(15);
        cout << current->major << setw(7);
        cout << current->gpa << endl;

        current = current->link;
    }
}

main.cpp:

#include <iostream>
#include  "studentlist.h"

using namespace std;

int main(int argc, char *argv[])
{
    StudentNode *head;

    createLinkedList(head);

    printList(head);

    cout << endl;
    findStudent(head, "Anna White");
    cout << endl << endl;

    removeStudent(head);
    cout << endl << endl;

    cout << "Updated Student List: " << endl;
    printList(head);
    cout << endl;

    cout << "GPA: " << calculateGPA(head) << endl;

    return 0;
}

Output in QT:

Name           Age    Major          GPA
Paul Johnson   18     Physics        3.7
Anna White     19     English        3.2
John Smith     20     Math           3.5
Anthony Rogers 21     Art            3.1
Cynthia Morris 24     History        3.6

Student Found:
Anna White     19     English        3.2

Front Node Deleted:
Name           Age    Major          GPA
Paul Johnson   18     Physics        3.7

Updated Student List:
Name           Age    Major          GPA
Anna White     19     English        3.2
John Smith     20     Math           3.5
Anthony Rogers 21     Art            3.1
Cynthia Morris 24     History        3.6

GPA: 3.35
Press <RETURN> to close this window...

output in Cloud9 without reformatting:

Name           Age    Major          GPA    
       3.7    cs
       3.2    lish
          3.5    
           3.1    
       3.6    

Student Not Found


Front Node Deleted:
Name           Age    Major          GPA 
       3.7 ysics

Updated Student List: 
Name           Age    Major          GPA    
       3.2    lish
          3.5    
           3.1    
       3.6    

GPA: 3.35

reformatted output in cloud9:

Name            Age     Major           GPA
Paul Johnson    18      Physics         3.7
Anna White      19      English         3.2
John Smith      20      Math            3.5
Anthony Rogers  21      Art             3.1
Cynthia Morris  24      History         3.6

Student Not Found


Front Node Deleted:
Name            Age     Major           GPA
Paul Johnson    18      Physics         3.7

Updated Student List: 
Name            Age     Major           GPA
Anna White      19      English         3.2
John Smith      20      Math            3.5
Anthony Rogers  21      Art             3.1
Cynthia Morris  24      History         3.6

GPA: 3.35
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

Several things wrong with FindStudent:

First, you're checking the current node to see if you're at the end of the list before evaluating the name in the list. That's probably your bug. If the list has exactly one item in it, or the item begin sought is at the end of the list, findStudent will see that the node's link member is null and return before evaluating current->name==find

Second, findStudent is recursive. If the list was sufficiently large, findStudent would create a stack overflow. (And your program would crash).

Your code, with fix:

StudentNode* findStudent(StudentNode *head, const string& find) {
    StudentNode *current = head;

    while ((current != NULL) && (current->name != find)) {
        current = current->link;
    }

    if (current != NULL) {
        cout << "Student Found:" << endl;
        cout <<left<< setw(15);
        cout <<current->name <<setw(7);
        cout << current->age<<setw(15);
        cout << current->major << setw(4);
        cout << current->gpa;
    }
    else {
        cout "Student not found" << endl;
    }

    return current;
}

Another bug you have:

double calculateGPA(StudentNode *&head){

    StudentNode *current = head;
    double calculatedGPA;  // UNINITIALIZED VARIABLE!
    int num_students=0;

    while(current!= NULL){

        calculatedGPA += current->gpa;

Make sure you initialize caculatedGPA to 0

    double calculatedGPA = 0;

<opinion> Third, all your functions have the list head being passed as a reference to a pointer. Pass by pointer or reference, but not both. Improved function signatures:

StudentNode* createLinkedList();
StudentNode* findStudent(const string& find);
StudentNode* removeStudent(StudentNode *head); // removes the head and returns the list's new head:

double calculateGPA(StudentNode* head);
void printList(StudentNode* head);
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks for the quick response, the fix you provided is actually what I had before I went with recursion but I started second guessing myself. I haven't learned much about recursion yet so I'm not sure when it's best to use it, pretty clumsy on my part. I'll definitely hold on to the opinion you provided, it seems solid for me moving forward. I still don't know why it outputs fine in qt-creator but wrong in cloud9. – Jsandoval87 Apr 11 '18 at 04:31
  • You'll need to learn how to use a debugger. (Or add more print statements to your code). Working correctly in one environment, but incorrectly in another is a symptom of an uninitialized variable or some other bug that results in undefined behavior. – selbie Apr 11 '18 at 05:04
  • Found another bug in calculateGPA. See above. Are you compiling with all warnings enabled? – selbie Apr 11 '18 at 05:42
  • I have them enabled but it's not giving me any warnings. It wasn't formatting correctly in cloud9 because of the inFile.txt I was using. I'm thinking maybe it was picking up space from the .txt file but I'm just guessing. What I did was recreated the file directly in cloud9 and it worked out. If I ever encounter a similar problem next time I'll provide the .txt. I'm still curious about what bug you found, anything to improve my coding skills. – Jsandoval87 Apr 11 '18 at 14:39