Question: Hi! I have a c++ program that takes user input from the console and store the data into a linked list. The input made of
Hi! I have a 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. Can someone look at it and give me some feedbacks on where might be my mistake?
------------------------------------------------------------------------------------
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 <<" list consists of: ";
display(myCar);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
