0

The name and RUID is read from read.txt, if it find a string ‘RUID’ in read.txt, you need to create a new Linkedlist to store the following RUID and name (there are multiple RUID in the string), finish reading all the elements in read.txt, use operator overloading + to connect linkedlists together

This is what's being asked of me. I already know how to open the text file and have managed to do everything else, including the operator overloading. I'm just confused as to how I should go about trying to get and store the information from the text file into separate linked lists.

This is my pseudo-code (I just don't know how to quite implement it)

while (!File.eof())
{
if (a string in a line = RUID)
{
make a new LinkedList and set as current LinkedList
jump to next line
read line into the LinkedList
}
}

This is my text file (ignore the spaces in-between each line)

RUID Name

4325 name1

RUID Name

5432 name2

6530 name3

RUID Name

1034 name4

2309 name5

and this is my code thus far

#include "LinkedList.h"
#include <string>
#include <algorithm>
#include <iostream>
#include <time.h>
#include "stdlib.h"
#include <cmath>
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    LinkedList x;
    string line;
    ifstream ReadFile;
    ReadFile.open("read.txt");
    int counter = 0;
    if (ReadFile.is_open())
    {
        while (!ReadFile.eof())
        {

        }
    }

    ofstream WriteFile("Write.txt");
    if (WriteFile.is_open())
    {
        Node* extra;

        int inf = 9;
        while (inf != 10)
        {
            cout << "Select the following options:" << endl;
            cout << "1. Add node to the list" << endl;
            cout << "2. Remove node from list" << endl;
            cout << "3. Print list" << endl;
            cout << "4. Print element of the list" << endl;
            cout << "5. Sort the list" << endl;
            cout << "6. Quit" << endl;
            int option;
            cin >> option;
            cout << "Selection: " << option << endl;

            if (option == 1)
            {
                cout << "Enter student name: ";
                string insert;
                cin.ignore();
                getline(cin, insert);

                int temp = rand() % 9000 + 1000;

                extra = new Node(insert, temp);
                x.addNode(extra);
                cout << endl;
                x.printList();
            }
            else if (option == 2)
            {
                cout << "Enter the RUID to be remove: ";
                int remove;
                cin >> remove;
                cout << endl;

                x.removeNode(remove);
                x.printList();
            }
            else if (option == 3)
            {
                x.printList();
            }
            else if (option == 4)
            {
                cout << "Enter the index: ";
                int index;
                cin >> index;
                cout << endl;

                x.printElement(index);
            }
            else if (option == 5)
            {
                x.sortList();
                x.printList();
            }
            else if (option == 6)
            {
                break;
            }
            else
            {
                cout << "Invalid output" << endl;
            }
        }
    }

    system("pause");
    return 0;
}

1 Answers1

0

Simply read as two strings in a loop. If the first is equal to "RUID" do whatever special handling you need to start a new list, else convert it to an integer and add to the current list.

Example (including pseudo-code for list handling):

ListType* current_list = nullptr;
std::string ruid_string, name;
while (ReadFile >> ruid_string >> name)
{
    if (ruid_string == "RUID")
    {
        // Logic to create a new list or whatever
        if (current_list != nullptr)
        {
            // A list already exists, add it to the "master" list
            master_list.AddSubList(current_list);
        }

        current_list = CreateList();
    }
    else
    {
        int ruid = std::stoi(ruid_string);

        // Add node to current list
        if (current_list != nullptr)
            current_list->AddNode(ruid, name);
    }
}

if (current_list != nullptr)
{
    // Add the last list to the master list
    master_list.AddSubList(current_list);
}
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550