Question: I've created a do while loop which takes in a number of values and stores them in a linked list. The problem is I want

I've created a do while loop which takes in a number of values and stores them in a linked list. The problem is I want the user to type in 'done' when they are done inputing, but when the loop ends the word 'done' winds up getting stored in the linked list. How do i end the loop without actually storing the ending loop case word?

Code:

struct Node {

string item;

Node *next;

};

int main(){

cout << "Automated Linked List / Enter Five Values" << endl;

cout << "------------------------------------------" << endl;

string s;

Node *head = new Node(); // head is always first in the list

Node *temp = head; // temp will iterate through list

do{

cin >> s;

temp -> item = s;

temp -> next = new Node();

temp = temp -> next; } while (s!="done");

temp -> next = nullptr;

temp = head;

cout << " ";

cout << "The values in the list are: " << endl;

cout << "---------------------------" << endl;

while (temp -> next != nullptr){

cout << temp -> item << endl;

temp = temp -> next; // temp gets a new address to the next address which points to the next node in list

}

return 0;

}

ex:

orange

green

blue

done

The values in the list are:

orange

green

blue

done // I do not want this value stored

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!