Question

​c++ program that takes user input from the console and store the data into a linked list.

c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. 


Input sample:

3

Honda

Sentra

8900

2017

Toyota

Camri

109827

1999

Nissan

Sentra

8726

1987

current output:

Honda 0 0

Sentra 8900 2017 0

Toyota Camri 109827 1999

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

but the output should be:

Honda Sentra 8900 2017

Toyota Camri 109827 1999

Nissan Sentra 8726 1987

_________________________________________

Car.h

#ifndef Car_h

#define Car_h

#include

struct Car{

std::string model, make;

int mileage;

int year;

Car *next;

};

//void populate(Car *);

//void display(Car *);

//void insert(Car *);

#endif /* Car_h */

_______________________________________________

main.cpp

#include

#include"Car.h"

#include

using namespace std;

void populate(Car *newRecord)

{

getline(cin,newRecord->model);

cin.clear();

getline(cin,newRecord->make);

cin.clear();

cin >> newRecord->mileage;

cin.clear();

cin >> newRecord->year;

cin.clear();

return;

}

void display(Car *content)

{

while(content != NULL)

{

cout << content->model <<" ";

cout<< content->make <<" ";

cout << content->mileage <<" ";

cout << content->year << endl;

content = content->next;

}

cout << endl;

return;

}

//void insert(Car *)

int main()

{

Car *myCar, *current;

myCar = new Car;

current = myCar;

int x = 0;

cout << " How many cars do you want to compare? " << endl;

cin >> x;

cin.clear();

//Car myCar[x];

for(int i = 0; i < x - 1; ++i)

{

populate(current);

current->next = new Car;

current = current->next;

}

populate(current);

current->next = NULL;

cout <<"\nlist consists of:\n";

display(myCar);

  

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

-------------------- I used Visual Studio 2013, C++ Language, Console Application --------------------

-------------------- OUTPUT --------------------

-------------------- CODE --------------------

#include "stdafx.h"
#include
#include
using namespace std;

struct Car
{
   string model, make;
   int mileage, year;
   struct Car *next;
};

/* insert new node at the end of the linked list */
void append(struct Car** head, string make, string model, int mileage, int year)
{
   //Create and allocate node
   struct Car* newNode = new Car;

   struct Car *last = *head;

   //Assign data to the node
   newNode->make = make;
   newNode->model = model;
   newNode->mileage = mileage;
   newNode->year = year;

   //Set next pointer of new node to null as its the last node
   newNode->next = NULL;

   //If list is empty, new node becomes first node
   if (*head == NULL)
   {
       *head = newNode;
       return;
   }

   //Else traverse till the last node
   while (last->next != NULL)
       last = last->next;

   //Change the next of last node
   last->next = newNode;
   return;
}

void display(struct Car *node)
{
   cout << "\nDisplay :: " << endl;
   while (node != NULL)
   {
       cout << node->make << " " << node->model << " " << node->mileage << " " << node->year << endl;
       node = node->next;
   }
}

int main()
{
   struct Car* head = NULL;
   int count, mileage, year;
   count = mileage = year = 0;
   string make, model;
   make = model = "";
   cout << "Please enter how many car(s) : ";
   cin >> count;
  
   while (count>0)
   {
       cout << endl;
       cout << "Please enter car make : ";
       cin.ignore();
       getline(cin, make);
       cout << "Please enter car model : ";
       getline(cin, model);
       cout << "Please enter car mileage : ";
       cin >> mileage;
       cout << "Please enter car year : ";
       cin >> year;
       append(&head, make, model, mileage, year);
       count--;
   }
   display(head);
   system("pause");
   return 0;
}

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

Add a comment
Know the answer?
Add Answer to:
​c++ program that takes user input from the console and store the data into a linked list.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

    Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year : int ----------------------- + Car() + setMake(m: string) : void + getMake() : string + setYear(y: int) : void + getYear() : int ---------------------- */ #ifndef CAR_H #define CAR_H #include <iostream> using namespace std; class Car { private: string make; int year; public: Car(); Car(string); Car(int); Car(string, int); void setMake (string); string getMake() {return make;} void setYear (int); int getYear() {return year;} }; #endif #include...

  • Hi, I need to write a program for linux (putty) and please read carefully (I've asked...

    Hi, I need to write a program for linux (putty) and please read carefully (I've asked the same question 3 times because the experts who answered my questions wrote their own carmain1.cpp file. Please leave it where it is and only create car.cpp and car.h files.) Here's carmain1.cpp /* carmain1.cpp * Please don't rewrite this file * I will be using the exact * same file below when grading * */ #include <iostream> #include "car.h" using namespace std; int main()...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • I did a program in computer science c++. It worked fine the first and second time...

    I did a program in computer science c++. It worked fine the first and second time when I ran the program, but suddenly it gave me an error. Then, after a few minutes, it started to run again and then the error showed up again. This is due tonight but I do not know what is wrong with it. PLEASE HELP ME! Also, the instruction for the assignment, the code, and the error that occurred in the program are below....

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT