Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Name list management (Linked lists practice) : Hello I need help with this assig

ID: 3744266 • Letter: N

Question

Name list management (Linked lists practice) :

Hello I need help with this assignment. I worked most of it but I'm stuck. My code is near the bottom and with what I need help with. Thanksss. C++.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is my code so far:

#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sys/stat.h>

using namespace std;

typedef struct node_type {
   long int id;
   string fname;
   string lname;
   string s;
   string dob;
   string sort;
   float gpa;
   struct node_type *next;
}node;

void addRecord(node* &head, string line)

{
   node *new_node = new node;
   string fn, ln, dob;
   string id;
   string gpa;
   stringstream ss;
   int i = 0;
   cout << line << endl;
       while (line[i] != ':') {
           i++;
       }
       i++;
       while (line[i] != ',') {
           id.push_back(line[i]);
           i++;
       }
       i++;
       while (line[i] != ':') {
           i++;
       }
       i++;
       while (line[i] != ',') {
           fn.push_back(line[i]);
           i++;
       }
       i++;
       while (line[i] != ':') {
           i++;
       }
       i++;
       while (line[i] != ',') {
           ln.push_back(line[i]);
           i++;
       }
       i++;
       while (line[i] != ':') {
           i++;
       }
       i++;
       while (line[i] != ',') {
           dob.push_back(line[i]);
           i++;
       }
       i++;
       while (line[i] != ':') {
           i++;
       }
       i++;
       while (line[i] != '}') {
           gpa.push_back(line[i]);
           i++;
       }
       ss << id;
       ss << " ";
       ss << gpa;
       new_node->dob = dob;
       new_node->fname = fn;
       new_node->lname = ln;
       new_node->next = NULL;
       ss >> new_node->id;
       ss >> new_node->gpa;
       ss.clear();
       if (head == NULL) {
           head = new_node;

       }
       else
       {
           node *cur = head;
           while (cur->next != NULL) {
               cur = cur->next;
           }
           cur->next = new_node;
       }
  
}

void swap(node *first, node *second) {
   node *temp = new node;
   temp->dob = first->dob;
   temp->fname = first->fname;
   temp->gpa = first->gpa;
   temp->id = first->id;
   temp->lname = first->lname;
   first->dob = second->dob;
   first->fname = second->fname;
   first->lname = second->lname;
   first->id = second->id;
   first->gpa = second->gpa;
   second->dob = temp->dob;
   second->fname = temp->fname;
   second->lname = temp->lname;
   second->id = temp->id;
   second->gpa = temp->gpa;
}

void sortByFirstName(node *head)
{
   node *cur1 = head;
   node *cur2 = head;

   while (cur1 != NULL)
   {
       cur2 = cur1->next;
       while (cur2 != NULL)
       {
           if (cur1->fname > cur2->fname)
           {
               swap(cur1, cur2);
           }
           else if (cur1->fname == cur2->fname)
           {
               if (cur1->id > cur2->id)
               {
                   swap(cur1, cur2);
               }
           }
           cur2 = cur2->next;
       }
       cur1 = cur1->next;
   }
}
void sortByLastName(node *head)
{
   node *cur1 = head;
   node *cur2 = head;

   while (cur1 != NULL)
   {
       cur2 = cur1->next;
       while (cur2 != NULL)
       {
           if (cur1->lname > cur2->lname)
           {
               swap(cur1, cur2);
           }
           else if (cur1->lname == cur2->lname)
           {
               if (cur1->id > cur2->id)
               {
                   swap(cur1, cur2);
               }
           }
           cur2 = cur2->next;
       }
       cur1 = cur1->next;
   }
}
void sortByGpa(node *head)
{
   node *cur1 = head;
   node *cur2 = head;

  

   while (cur1 != NULL)
   {
       cur2 = cur1->next;
       while (cur2 != NULL) {
           if (cur1->gpa > cur2->gpa)
           {
               swap(cur1, cur2);
           }
           else if (cur1->gpa == cur2->gpa)
           {
               if (cur1->id > cur2->id)
               {
                   swap(cur1, cur2);
               }
              
           }
           cur2 = cur2->next;
       }
       cur1 = cur1->next;
   }
}

void sortByDOB(node *head) {
   node *cur1 = head;
   node *cur2 = head;

   while (cur1 != NULL)
   {
       cur2 = cur1->next;
       while (cur2 != NULL)
       {
           if (cur1->dob > cur2->dob)
           {
               swap(cur1, cur2);
           }
           else if (cur1->dob == cur2->dob)
           {
               if (cur1->id > cur2->id)
               {
                   swap(cur1, cur2);
               }
           }
           cur2 = cur2->next;
       }
       cur1 = cur1->next;
   }
}

void sortByID(node *head)
{
   node *cur1 = head;
   node *cur2 = head;

   while (cur1 != NULL)
   {
       cur2 = cur1->next;
       while (cur2 != NULL) {
           if (cur1->id > cur2->id)
           {
               swap(cur1, cur2);
           }
           cur2 = cur2->next;
       }
       cur1 = cur1->next;
   }

}

void write(string file, node *head) {
  
  
  
   ofstream ofs;
  
   ofs.open(file.c_str());

               if (ofs.is_open())
               {
                  
                   node *cur = head;
                   node *cur1 = head;
                   cur1 = cur->next;
                   while (cur != NULL && cur1 != NULL)
                   {
                       if (cur1->id != cur->id && cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0)
                       {
                           ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
                           ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";
                           ofs << endl;
                           cur = cur->next;
                           cur1 = cur1->next;
                       }
                       else if (cur1->id != cur->id)   //when ids are not the same. print.
                       {
                           ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
                           ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";
                           ofs << endl;
                           cur = cur->next;
                           cur1 = cur1->next;
                       }
                       else   //else ignore line
                       {
                           cur = cur->next;
                           cur1 = cur1->next;
                       }
                       if (cur1 == NULL) //print last line
                       {
                           if (cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0)
                           {
                               ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
                               ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";
                               ofs << endl;
                           }
                           else
                           {
                               ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;
                               ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";
                               ofs << endl;
                           }
                       }
                      
                   }
               }
  

}
int main(int argc, char* argv[])
{

   node *head = NULL;
   std::ifstream ifs("input.txt");
   string input;
   string line;

   if (ifs.is_open())
   {
      
           while (!ifs.eof())
           {
               getline(ifs, line);
               addRecord(head, line);

           }

          
           input = "output.txt";
           sortByID(head); // calling to sort by id.
           write(input, head); //delete duplicates and output the list
      
      
   }
   else {
       cout << " File Opening Error";
   }
   //system("pause");
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What I have so far is:

-Reading the list from the input file.

-Storing that list

-Sorting Functions (id, firstname, lastname, DOB, GPA) <-- sorting function base off one of these

-Deleting duplicates lines by comparing IDs and printing only 1 of them

-Outputing the list in its sorted form.

What I don't have and need is:

-Reading the sort.txt file

-Calling the sorting functions based off of the sort.txt

-reading the "delete id" line in the input.txt file.

-deleting the id that the delete line is calling for

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Example testing my code:

INPUT.TXT

{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
{id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}

OUTPUT.TXT

{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
{id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}

SORT.TXT <--//NEED HELP READING THIS AND CALLING THE FUNCTIONS FOR THEM

first
id <----//THE OUTPUT.TXT SHOULD BE SORTED BASED OFF THE LAST LINE IN THIS .TXT FILE

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MY MAIN PROBLEM: EXAMPLE 2

INPUT.TXT

{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
{id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0}
delete 1234568
{id:1234570,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}

SORT.TXT

id
DOB

OUTPUT.TXT

{id:1234587,first:Katy,last:Green,DOB:1995-08-18,GPA:4.0}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
{id:1234570,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}

I'm stomped at the delete id line. Don't know how to read the line and store the id without skrewing up my code.

If you can only help me out with this part that would be great. :)

If you can help me with what I need left that would be awesome. :D

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sorting Description:

There will be two input files. one called input.txt which containes the list of students and their information. The second is sort.txt which contains a list of sorting commands in their own line (id, first, last, DOB, GPA). The sort file will have one or more commands.

Example:

Input.txt

{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}

sort.txt <--The code should read this and sort it based off of what in it (id, first, last, DOB, GPA)

DOB
first
last
id

This is how it should be output.

output.txt

{id:1234567,first:Mary,last:Green,DOB:1996-10-03,GPA:4.0}
{id:1234568,first:Peter,last:White,DOB:1997-05-22,GPA:3.8}
{id:1654238,first:Nick,last:Park,DOB:1995-08-18,GPA:4.0}

*Notice the last line in the sort.txt file. That line should be how the list should be sorted and outputed in the end*

Description of output:

I also fix my header in my code. For some reason it got removed when posting this.

IF MORE INFORMATION IS NEEDED PLEASE COMMENT

COSC2430 Hw2: Name list management (Linked lists practice) 1. Introduction You will create a C++ program to manage a name list. Read a file that has a lot of items that stand for the information of a name list of certain class. Then sort the records by specific attributes to alphabetical ascending order, and output to a plain text file. 2. Input and Output a. Input file 1) The input file has multiple records (number >1). Your program should read the records one by one from the beginning of the file to the end Each record has uniform attributes (columns), the attributes appear in a fixed order, a record should contain all the keys, no empty values part will be given. But the input file may contain duplicated records. If two records have same id value, they can be recognized as duplicated records. You should always update record with the latter one. If somebody's name has more than one words, the separator would be underline _". Other than useful information in records, no other character or space will be given 2) 3) Each record takes one line (ends in n), and one line only contains one

Explanation / Answer

//Hey. Your solution is here

// I found that some of your sorting methods are not working properly.

// Added code to select sorting criteria based on sort file provided.

// reading delete id and deleting record accordingly will be updated soon here. Given information is not sufficiet to do this, please provide more details e.g. //Sample input file, file containing deleting crteria and output file

// Please upvote it its helpful to you.

// Thank you

#include <string>

#include <vector>

#include <sstream>

#include <fstream>

#include <iostream>

#include <iomanip>

#include <sys/stat.h>

using namespace std;

typedef struct node_type {

long int id;

string fname;

string lname;

string s;

string dob;

string sort;

float gpa;

struct node_type *next;

}node;

void addRecord(node* &head, string line)

{

node *new_node = new node;

string fn, ln, dob;

string id;

string gpa;

stringstream ss;

int i = 0;

cout << line << endl;

while (line[i] != ':') {

i++;

}

i++;

while (line[i] != ',') {

id.push_back(line[i]);

i++;

}

i++;

while (line[i] != ':') {

i++;

}

i++;

while (line[i] != ',') {

fn.push_back(line[i]);

i++;

}

i++;

while (line[i] != ':') {

i++;

}

i++;

while (line[i] != ',') {

ln.push_back(line[i]);

i++;

}

i++;

while (line[i] != ':') {

i++;

}

i++;

while (line[i] != ',') {

dob.push_back(line[i]);

i++;

}

i++;

while (line[i] != ':') {

i++;

}

i++;

while (line[i] != '}') {

gpa.push_back(line[i]);

i++;

}

ss << id;

ss << " ";

ss << gpa;

new_node->dob = dob;

new_node->fname = fn;

new_node->lname = ln;

new_node->next = NULL;

ss >> new_node->id;

ss >> new_node->gpa;

ss.clear();

if (head == NULL) {

head = new_node;

}

else

{

node *cur = head;

while (cur->next != NULL) {

cur = cur->next;

}

cur->next = new_node;

}

}

void swap(node *first, node *second) {

node *temp = new node;

temp->dob = first->dob;

temp->fname = first->fname;

temp->gpa = first->gpa;

temp->id = first->id;

temp->lname = first->lname;

first->dob = second->dob;

first->fname = second->fname;

first->lname = second->lname;

first->id = second->id;

first->gpa = second->gpa;

second->dob = temp->dob;

second->fname = temp->fname;

second->lname = temp->lname;

second->id = temp->id;

second->gpa = temp->gpa;

}

void sortByFirstName(node *head)

{

node *cur1 = head;

node *cur2 = head;

while (cur1 != NULL)

{

cur2 = cur1->next;

while (cur2 != NULL)

{

if (cur1->fname > cur2->fname)

{

swap(cur1, cur2);

}

else if (cur1->fname == cur2->fname)

{

if (cur1->id > cur2->id)

{

swap(cur1, cur2);

}

}

cur2 = cur2->next;

}

cur1 = cur1->next;

}

}

void sortByLastName(node *head)

{

node *cur1 = head;

node *cur2 = head;

while (cur1 != NULL)

{

cur2 = cur1->next;

while (cur2 != NULL)

{

if (cur1->lname > cur2->lname)

{

swap(cur1, cur2);

}

else if (cur1->lname == cur2->lname)

{

if (cur1->id > cur2->id)

{

swap(cur1, cur2);

}

}

cur2 = cur2->next;

}

cur1 = cur1->next;

}

}

void sortByGpa(node *head)

{

node *cur1 = head;

node *cur2 = head;

while (cur1 != NULL)

{

cur2 = cur1->next;

while (cur2 != NULL) {

if (cur1->gpa > cur2->gpa)

{

swap(cur1, cur2);

}

else if (cur1->gpa == cur2->gpa)

{

if (cur1->id > cur2->id)

{

swap(cur1, cur2);

}

}

cur2 = cur2->next;

}

cur1 = cur1->next;

}

}

void sortByDOB(node *head) {

node *cur1 = head;

node *cur2 = head;

while (cur1 != NULL)

{

cur2 = cur1->next;

while (cur2 != NULL)

{

if (cur1->dob > cur2->dob)

{

swap(cur1, cur2);

}

else if (cur1->dob == cur2->dob)

{

if (cur1->id > cur2->id)

{

swap(cur1, cur2);

}

}

cur2 = cur2->next;

}

cur1 = cur1->next;

}

}

void sortByID(node *head)

{

node *cur1 = head;

node *cur2 = head;

while (cur1 != NULL)

{

cur2 = cur1->next;

while (cur2 != NULL) {

if (cur1->id > cur2->id)

{

swap(cur1, cur2);

}

cur2 = cur2->next;

}

cur1 = cur1->next;

}

}

void write(string file, node *head) {

ofstream ofs;

ofs.open(file.c_str());

if (ofs.is_open())

{

node *cur = head;

node *cur1 = head;

cur1 = cur->next;

while (cur != NULL && cur1 != NULL)

{

if (cur1->id != cur->id && cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0)

{

ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;

ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";

ofs << endl;

cur = cur->next;

cur1 = cur1->next;

}

else if (cur1->id != cur->id) //when ids are not the same. print.

{

ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;

ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";

ofs << endl;

cur = cur->next;

cur1 = cur1->next;

}

else //else ignore line

{

cur = cur->next;

cur1 = cur1->next;

}

if (cur1 == NULL) //print last line

{

if (cur->gpa == 4 || cur->gpa == 3 || cur->gpa == 2 || cur->gpa == 1 || cur->gpa == 0)

{

ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;

ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << ".0}";

ofs << endl;

}

else

{

ofs << "{id:" << cur->id << ",first:" << cur->fname << ",last:" << cur->lname;

ofs << ",DOB:" << cur->dob << ",GPA:" << cur->gpa << "}";

ofs << endl;

}

}

}

}

}

int main(int argc, char* argv[])

{

node *head = NULL;

ifstream ifs,ifsSort;

string input;

string line;

// You can add code here to split file name from the command line

// For now, using static file names for input, sort and output

ifs.open("input11.txt", ios::in);

if (ifs.is_open() == false)

{

cout << " File Opening Error - Input File";

exit(0);

}

while (!ifs.eof())

{

getline(ifs, line);

addRecord(head, line);

}

//code to sort

ifsSort.open("sort11.txt", ios::in);

if (ifsSort.is_open() == false)

{

cout << " File Opening Error - Sort File";

exit(0);

}

// To get last line from sort text file

while (!ifsSort.eof())

{

getline(ifsSort, line);

}

int choice;

if (line == "id")

choice = 1;

else if (line == "first")

choice = 2;

else if (line == "last")

choice = 3;

else if (line == "DOB")

choice = 4;

else if (line == "GPA")

choice = 5;

//Note : In cpp switch can not take string as choice variable. we need to convert choice to integers first.

switch (choice) // run-time

{

case 1: // compile-time

sortByID(head);

break;

case 2: // compile-time

sortByFirstName(head);

break;

case 3: // compile-time

sortByLastName(head);

break;

case 4: // compile-time

sortByDOB(head);

break;

case 5: // compile-time

sortByGpa(head);

break;

default:

std::cout << "Invalid Sorting Criteria" << std::endl;

break;

};

input = "output11.txt";

write(input, head); //delete duplicates and output the list

//system("pause");

int x;

cin >> x;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote