0

InputFile.txt
Bea
Sanchez
Junior
Math
OPS HIGH
17
90

Nicole
Sanchez
Freshman
Science
OCC HIGH
13
98

Billy
Cruz
Senior
English
OPS HIGH
17
80

#include <iostream>
#include <string>;
#include <fstream>
using namespace std;

struct StudentNode{
    string firstName;
    string lastName;
    string gradeYear;
    string subject;
    string schoolName;
    int age;
    int grade;
    StudentNode* next;
};

void GetInputFile(string& inputFile)
{
    cout << "Enter file name: ";
    getline(cin, inputFile);
}

StudentNode* CreateList(string inputFile)
{
    ifstream inFile;
    StudentNode *head;
    StudentNode * stdPtr;
    head = NULL;
    stdPtr = new StudentNode;

    inFile.open("inFile.txt");

    while(inFile && stdPtr != NULL)
    {
        getline(inFile, stdPtr->firstName);
        getline(inFile, stdPtr->lastName);
        getline(inFile, stdPtr->gradeYear);
        getline(inFile, stdPtr->subject);
        getline(inFile, stdPtr->schoolName);
        inFile >> stdPtr->age;
        inFile >> stdPtr->grade;
        inFile.ignore(1000,'\n');
        inFile.ignore(1000,'\n');

        stdPtr->next = head;
        head = stdPtr;
        stdPtr = new StudentNode;
    }
    delete stdPtr;
    inFile.close();
    stdPtr = NULL;

    return head;
}

void OutputList(StudentNode* head)
{
    StudentNode* stdPtr;
    ofstream oFile;
    stdPtr = head;

    oFile.open("outputStudentList.txt");

    while (stdPtr != NULL)
    {
        oFile << stdPtr->firstName << endl
              << stdPtr->lastName << endl
              << stdPtr->gradeYear << endl
              << stdPtr->subject << endl
              << stdPtr->schoolName << endl
              << stdPtr->age << endl
              << stdPtr->grade;

        }

    oFile.close();
}

int main()
{
    string inputFile;
    StudentNode* head;

    GetInputFile (inputFile);
    CreateList(inputFile);
    OutputList(head);
    cout << "done output\n";
}

What I'm doing is reading an inputfile and doing linked list. When I output it, only the second data which is nicole's info is output in the outputfile. Also, the output would look like this:

Nicole
Sanchez
Freshman
Science
0
4536547474

I am completely confused as to why this is happening and outputting the same thing infinite times.

2
Contributors
1
Reply
18
Views
2 Years
Discussion Span
Last Post by deceptikon
0

The two primary problems are that head is never updated in main, and your output loop never sets stdPtr to stdPtr->next. Given your description of the problem, as well as syntax errors, I find it difficult to believe that the provided code is the same code that you're executing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.