Question: So i have a problem with this program, I tried to add a loop that lets the user inputs his results in the console instead

So i have a problem with this program, I tried to add a loop that lets the user inputs his results in the console instead of retrieving from a text file, but I cant figure out how to change the program in order to do so. I put my prompt below + program, and my for loop, can someone help me fix this program?

Loop: trying to input.. to replace the file retrieving

for (int i = 0; i < 15; i++) { cout name; cin >> weight; l.insert(name, weight); cin.ignore(15, ' '); }

C ++ Program Specification: 1. Read data for names and weights for 15 people from the console where there is a name on a line followed by a weight on the next line, like in names.txt. 2. Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list. 3. This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order. 4. You need to build the list as you go maintaining this ordering, so at any time a print method was called it would print the related field in order. (This means nodes are added to the list in sorted order, elements are not added to the list followed by a sort called on the list.) For example after 3 elements are added for (Name Weight): Michael 275, Tom 150, Abe 200. Output: Names & weights sorted(ascending) by name. : Abe 200, Michael 275, Tom - 150 Names & weights sorted(ascending) by weight. : Tom 150, Abe 200, Michael - 275

________________________________________________________________________________________________________________________________________

Program:

#include

#include

#include

using namespace std;

// Declare the class Node

class Node

{

//Access Specifier

public:

//Declare the variables

int weight;

string name;

Node *prev;

Node *next;

//Access specifier

public:

//Constructor

Node() {};

//Definition of function 'SetData'

void SetData(string sname, int sweight)

{

name = sname;

weight = sweight;

};

//Definition of function 'SetNext'

void SetNext(Node* aNext)

{

//Assign 'aNext' to 'next'

next = aNext;

};

//Definition of function 'SetPrev'

void SetPrev(Node* aPrev)

{

//Assign 'aPrev' to 'prev'

prev = aPrev;

};

//Definition of next

Node* Next() { return next; };

//Definition of Prev

Node* Prev() { return prev; };

};

//Create a class list

class List

{

//Create the 'head' as pointer

Node *head;

public:

//Constructor

List() { head = NULL; };

//Declaration of 'insert()'

void insert(string name,int weight);

//Declaration of 'print()'

void Print();

//Declaration of 'sort_weight()'

void sort_weight();

//Declaration of 'sort_name()'

void sort_name();

};

//Definition of 'insert()'

void List::insert(string name, int weight)

{

// Create a new node

Node* newNode = new Node();

//Insert the data into the class

newNode->SetData(name,weight);

//Assign null to object

newNode->SetNext(NULL);

newNode->SetPrev(NULL);

// Create a temp pointer

Node *tmp = head;

//Check if 'tmp' not equals to 'NULL'

if (tmp != NULL)

{

/* Nodes already present in the list Parse to end of list */

while (tmp->Next() != NULL)

{

//Assign 'tmp->Next()' to 'tmp'

tmp = tmp->Next();

}

// Point the last node to the new node

tmp->SetNext(newNode);

newNode->SetPrev(tmp);

}

else

{

// First node in the list

head = newNode;

}

}

void List::Print()

{

// Assign 'head' to 'tmp' pointer

Node *tmp = head;

// Checking condition for empty node

if (tmp == NULL)

{

cout << "EMPTY" << endl;

return;

}

//Check if list contains only one node

if (tmp->Next() == NULL)

{

//Print the list details

cout << " ";

cout <name<<"-"<< tmp->weight;

cout << endl;

}

else {

// Parsing and printing the list

do {

//Print the list details

cout << " ";

cout << tmp->name << "-" << tmp->weight;

cout << endl;

//Call the function 'Next()'

tmp = tmp->Next();

}

//Loop till 'tmp' not equal to 'NULL'

while (tmp != NULL);

}

}

//Definition of 'sort_name()'

void List::sort_name()

{

//Assign 'head' to pointer

Node * temphead1 = head;

//Crete an object

Node *temproll1 = new Node;

//Declare the variable 'counter' as integer

int counter = 0;

//Loop till 'temphead1' valid

while (temphead1)

{

//Assign 'temphead1->next' to 'temphead1'

temphead1 = temphead1->next;

//increment the 'counter'

counter++;

}

//Assign 'head' to 'temphead1'

temphead1 = head;

for (int j = 0; j

{

//Loop till the list is null

while (temphead1->next)

{

//Traversing each node

if (temphead1->name > temphead1->next->name)

{

//swapping the number

temproll1->name = temphead1->name;

temphead1->name = temphead1->next->name;

temphead1->next->name = temproll1->name;

//Swapping the last name

temproll1->weight = temphead1->weight;

temphead1->weight = temphead1->next->weight;

temphead1->next->weight = temproll1->weight;

}

else

//increment the node

temphead1 = temphead1->next;

}

//resetting the 'temphead'

temphead1 = head;

}

}

//Definition of 'sort_weight()'

void List::sort_weight()

{

//Assign the 'head' tp pointer

Node * t = head;

//Assign the 't' value to pointer

Node * temphead = t;

//creating the object

Node *temproll = new Node;

//Decalre the variable counter as integer

int counter = 0;

//Loop till 't' is valid

while (t)

{

//Assign 't->next' to 't'

t = t->next;

//Increment the counter

counter++;

}

//Loop till 'j' less than counter

for (int j = 0; j

{

//Loop till the list is null

while (temphead->next != NULL)

{

//Traversing each node

if (temphead->weight > temphead->next->weight)

{

//swapping the number

temproll->name = temphead->name;

temphead->name = temphead->next->name;

temphead->next->name = temproll->name;

//Swapping the last name

temproll->weight = temphead->weight;

temphead->weight = temphead->next->weight;

temphead->next->weight = temproll->weight;

}

//increment the node

temphead = temphead->next;

}

//resetting the 'temphead'

temphead = head;

}

}

//Definition of main()

int main()

{

/* Declare the variables */

int weight;

/* Declare the variables */

string name;

//Declare the variable 'ch' as 'integer'

int ch;

// Open input file

ifstream din;

din.open("a.txt");

//Check if file open

if (din.fail())

{

cerr << "Could not open file: " << endl;

return false;

}

//Create an object

List l;

//loop till 'din' not equals to end of file

while (!din.eof())

{

//Reading the content from file

din >> name>>weight;

//Inserting the content to file

l.insert(name,weight);

}

//Close the file

din.close();

//To sorting the list by name and printing the list

l.sort_name();

cout << " Names & weights sorted(ascending) by name";

l.Print();

/* To sorting the list by weight and printing the list */

l.sort_weight();

cout << " Names & weights sorted(ascending) by weight";

l.Print();

//pause the screen

system("pause");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!