Hey guys I am trying to work on some linked list stuff and I have my code put together but I wanted to post it on here and just check to make sure I am going in the right direction. Here is what I am trying to do:

Create a structure called ListNode that can hold an object of the car class as its value.
Then create a linked list to hold the following cars in inventory(6 cars that are in the array). Then I have to print the information for the user.

Header File

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }

CPP

#include "CarClass.h"
using namespace std;

struct ListNode
	{
		Car Car_array[6];
		ListNode *Next;
	};	

int main() {
		
		//Array of 6 cars
        ListNode = { Car("Porsche", "911", "Silver", 2005, 45000), 
                                Car("Ford", "Mustang", "Red", 2007, 12600),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                Car("Jeep", "Cherokee", "White", 2000, 98322),
                                Car("Nissan", "Sentra", "Red", 2002, 76046),
                                Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};

Re-post: http://www.dreamincode.net/forums/topic/167404-linked-list/

Recommended Answers

All 21 Replies

Here is what I have

#include "CarClass.h"
using namespace std;

struct ListNode
	{
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
        ListNode *next;   //Pointer to next node

        ListNode(void)
                :next(NULL)
        {}
};      

ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;
        cout << "Please enter the make of the vehicle: ";
        cin >> temp->make;

		start_ptr = new ListNode;
        ListNode *temp = start_ptr;
        cout << "Please enter the model of the vehicle: ";
        cin >> temp->model;

		start_ptr = new ListNode;
        ListNode *temp = start_ptr;
        cout << "Please enter the color of the vehicle: ";
        cin >> temp->color;

		start_ptr = new ListNode;
        ListNode *temp = start_ptr;
        cout << "Please enter the year of the vehicle: ";
        cin >> temp->year;

		start_ptr = new ListNode;
        ListNode *temp = start_ptr;
        cout << "Please enter the mileage of the vehicle: ";
        cin >> temp->mileage;


	    return 0;
	}

I now have 2 questions. I have to enter the following 6 vehicles,

Car("Porsche", "911", "Silver", 2005, 45000), 
                                Car("Ford", "Mustang", "Red", 2007, 12600),
                                Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                Car("Jeep", "Cherokee", "White", 2000, 98322),
                                Car("Nissan", "Sentra", "Red", 2002, 76046),
                                Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};

How does this code that I currently have know that I have 6 to enter and how can i print them all when I am done using the cardetails function set up in the header file??

I got it!!! I just need to figure out now how to set a variable to recognize how many vehicles were intered in

#include "CarClass.h"
using namespace std;



ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;

        char again;

        while(true)
        {
                cout << "Please enter the make of the vehicle: ";
                cin >> temp->make;
                cout << "Please enter the model of the vehicle: ";
                cin >> temp->model;
                cout << "Please enter the color of the vehicle: ";
                cin >> temp->color;
                cout << "Please enter the year of the vehicle: ";
                cin >> temp->year;
                cout << "Please enter the mileage of the vehicle: ";
                cin >> temp->mileage;
                
                cout<<"Would You like To Enter Another Car (Y/N) :";
                cin>>again;

                if(toupper(again) != 'Y')
                        break;
                temp->next = new ListNode;
                temp = temp->next;
        }

		for (int x=0; x<6; x++) 
		{
        start_ptr[x].car_details();
        }

	    return 0;
	}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


struct ListNode
	{
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
        ListNode *next;   //Pointer to next node
		
		void car_details();
        ListNode(void)
                :next(NULL)
        {}
};      
//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void ListNode::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }

Ok I got it but I have one final question I am curious about! If I wanted to add 3 more cars how would I do this! And if I wanted to delete a specific one for instance the second car...how would i do this??

#include "CarClass.h"
using namespace std;



ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;
		ListNode Car[6];

        for (int i = 0; i<6; i++)
			{
                cout << "Please enter the make of the vehicle: ";
                cin >> temp->make;
				Car[i].make;
                cout << "Please enter the model of the vehicle: ";
                cin >> temp->model;
				Car[i].model;
                cout << "Please enter the color of the vehicle: ";
                cin >> temp->color;
				Car[i].color;
                cout << "Please enter the year of the vehicle: ";
                cin >> temp->year;
				Car[i].year;
                cout << "Please enter the mileage of the vehicle: ";
                cin >> temp->mileage;
				Car[i].mileage;
                
                cout << "__________________________________________________________\n"<< endl;

		}

		for (int i=0; i<6; i++) 
		{
        Car[i].car_details();
        }


		// Change and print Information
		Car[3].year(2004);

	    return 0;
	}

And guys I am unsure but by running this program I do not feel that I may be doing this right...I am going to post what I am trying to do an someone please inform me if I have it all wrong if you can!

1. Create a car class to hold information
Attributes:
make (you can also think of this as the Manufacturer)
model
color
year
mileage
(Note: All attributes should be private.)
Behaviors:
1) Create mutator (Set) and accessor (Get) functions for all attributes of this class.
2) Create a default constructor that initializes all of the attributes to default values (blanks in the case of strings or 0 in the case of numbers.)
3) Make sure to have validation to ensure that the mileage can’t be set less than 0.
4) Create a constructor that takes the make, model, year, color, and mileage values and sets them for a newly created car.
5) Create another function called car_details that prints all of the attributes of the car in an attractive format.
(Example on an output line of a cars details:
“The current car is a 2008 Red Ford Mustang with 5000 miles.”)
2. Create a structure called ListNode that can hold an object of the car class as its value.
3. Create a Linked List to hold the following cars in inventory. Then use a loop to print all of the information about them for the user.
Make Model Color Year Mileage
1) Porsche 911 Silver 2005 45000
2) Ford Mustang Red 2007 12600
3) Voltzwagon Jetta Black 2006 20218
4) Jeep Cherokee White 2000 98322
5) Nisson Sentra red 2002 76046
6) Voltzwagon Beetle Black 2005 28031
4. Change the Jeep Cherokee’s year to be 2001. Change the Sentra’s mileage to be 80000. Use the loop to print out the information for the user.
5. You need to be able to see the average miles for all cars in inventory. Write the code to find the average miles and display it for the user by adding up the miles for all of the cars in inventory and dividing by the size of the Linked List holding inventory. Make sure to test if the Linked List holding the inventory has nothing in it yet to prevent a divide by zero problem!
6. Add the following cars to inventory. Then use a loop to print all of the information about them for the user.
Make Model Color Year Mileage
1) Chevrolet Corvette Black 2003 11903
6) Ford Explorer Grey 2004 73922
7) Honda Civic White 2007 12045

7. Delete the following car out of inventory. Then use a loop to print all of the information about them for the user.
Voltzwagon Jetta Black 2006 20218
(Hint: This is not the last car! You will have to delete this node and set the previous node’s pointer to reference the next object in the list.)

Anyone got some help for me??

>> If I wanted to add 3 more cars how would I do this!

Depends. If the data from a previous run of the program had been run is to be added to, then you need to read the data back from file and run an insert function three times passing in the desired data for each new car to be added. If the list is already in the program and you wanted to add three more cars, just run the insert protocol you want to use 3 more times.

>>if I wanted to delete a specific one for instance the second car...how would i do this??

Obviously you need to create a delete function. The Hint in your instructions gives you the common basic psuedocode to do this.

>>I do not feel that I may be doing this right...I am going to post what I am trying to do an someone please inform me if I have it all wrong if you can

Well, it's not the way I'd write a list class if that's what you mean. Most people think of lists as being of indeterminate length. It expands and contracts in size with each new node added/deleted to the list. You have hardcoded the number of nodes by using an array to hold the data. Usually, each node will have a data section, meaning one or more data members, and one or more pointers to self.

struct ListNode
{
    Car car;
    ListNode next;
};

Then the list itself is often an object a list class with a ListNode pointer called root or something like that to indicate where the list starts and member functions to insert new nodes to the list, delete nodes from the list, search the list, display information stored in the list, etc.

struct CarList
{
    ListNode * root;
    CarList() : root(0) {};
    void insert(Car);
    void delete(Car);
     void findCar(Car);
     void printList();
};

Additional functionality can be added by other member variables/functions as needed/desired/instructed. There are a number of tutorials on the net to help you work out the definition of the member functions. For that matter, lists are the subject of common posts to the board, so reviewing some of the posts by running a search using list as the keyword will probably yield multiple posts that you can review for applicability. However, the first thing you should do is review your class notes and reference text.

Is this what you are saying??

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


      struct ListNode
      {
      Car car;
      ListNode next;
      };

      struct CarList
      {

      ListNode * root;
      CarList() : root(0) {};
      void insert(Car);
      void delete(Car);
      void findCar(Car);
      void printList();

      };
//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void ListNode::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }

And if so how would I set up my cpp file to even get the information?? Like this??

#include "CarClass.h"
using namespace std;



ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;
		char again;

        while(true)
        {
                cout << "Please enter the make of the vehicle: ";
                cin >> temp->make;
                cout << "Please enter the model of the vehicle: ";
                cin >> temp->model;
                cout << "Please enter the color of the vehicle: ";
                cin >> temp->color;
                cout << "Please enter the year of the vehicle: ";
                cin >> temp->year;
                cout << "Please enter the mileage of the vehicle: ";
                cin >> temp->mileage;

                cout<<"Would You like To Enter Another Car (Y/N) :";
                cin>>again;

                if(toupper(again) != 'Y')
                        break;
                temp->next = new ListNode;
                temp = temp->next;
				cout <<"____________________________________________________\n\n";
        }

		cout <<"____________________________________________________\n\n";

        ListNode *details = start_ptr;

        for (int i=0; i<6; i++) 
        {
                details->car_details();
                details = details->next;
        }



	    return 0;
	}

Or would I create and array

const int SIZE = 9;
			
	//Array of 9 cars
	Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 18990, 1237362727), 
							Car("Ford", "Mustang", "Red", 2007, 49842, 7337372239),
							Car("Chevrolet", "Beretta", "Black", 1989, 90332, 2873644922),
							Car("Ford", "Focus", "White", 2008, 150, 9236498273),
							Car("Voltzwagon", "Jetta", "Black", 2006, 28002, 4673992056),
							Car("Rolls Royce", "Ghost", "Silver", 2005, 10000, 9292983855),
							Car("Mazda", "626", "Blue", 2002, 84754, 7364646463),
							Car("Toyota", "Camry", "Red", 2004, 50332, 2133737227),
							Car("Bugatti", "Veyron 16.4", "White", 2010, 5, 5712893401)};

It appears to me that you are not using a linked list at all? Maybe I am missing that but aren't you just using an array to hold your Car objects? Read about what linked lists are. They are a data structure all their own. Think about it like a train. In one car of the train you contain a datatype(Car object) This is your node. On your node there is a way to connect this train car to another, the next pointer. In this way you can create an ever changing list of data. By adding new cars to the train (insertion()), Removing cars from the train(deletion()). I hope this makes sense, it does to me. Like I said at the beginning though, read more about what linked lists are as they are damn handy.

EDIT: After looking at your code again I see that you have a struct carlist. Implement those prototypes.

Just one is using a linked list correct??

#include "CarClass.h"
using namespace std;



ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;
		char again;

        while(true)
        {
                cout << "Please enter the make of the vehicle: ";
                cin >> temp->make;
                cout << "Please enter the model of the vehicle: ";
                cin >> temp->model;
                cout << "Please enter the color of the vehicle: ";
                cin >> temp->color;
                cout << "Please enter the year of the vehicle: ";
                cin >> temp->year;
                cout << "Please enter the mileage of the vehicle: ";
                cin >> temp->mileage;

                cout<<"Would You like To Enter Another Car (Y/N) :";
                cin>>again;

                if(toupper(again) != 'Y')
                        break;
                temp->next = new ListNode;
                temp = temp->next;
				cout <<"____________________________________________________\n\n";
        }

		cout <<"____________________________________________________\n\n";

        ListNode *details = start_ptr;

        for (int i=0; i<6; i++) 
        {
                details->car_details();
                details = details->next;
        }



	    return 0;
	}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


struct ListNode
	{
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
        ListNode *next;   //Pointer to next node
		
		void car_details();
        ListNode(void)
                :next(NULL)
				
        {}
};      
//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
    void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void ListNode::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }

Ok heres 2 hints after this study linked lists, because you seem really confused on what they are, and by that I mean no disrespect.
The listNode for a singly linked list has one pointer, here your calling it next. Aside from this it should contain a datatype rather its an int, an array, a class object whatever. Once you have a node you can start to implement your linked list class. Using the prototypes you had a couple posts upfor a carlist was a good sort( constructor insert, delete etc)

It appears to me that you are not using a linked list at all? Maybe I am missing that but aren't you just using an array to hold your Car objects? Read about what linked lists are. They are a data structure all their own. Think about it like a train. In one car of the train you contain a datatype(Car object) This is your node. On your node there is a way to connect this train car to another, the next pointer. In this way you can create an ever changing list of data. By adding new cars to the train (insertion()), Removing cars from the train(deletion()). I hope this makes sense, it does to me. Like I said at the beginning though, read more about what linked lists are as they are damn handy.

EDIT: After looking at your code again I see that you have a struct carlist. Implement those prototypes.

So after reading what you said a few times I am kind of confused...

Is this the code I should be using so far(its not done just an idea to make sure I am going in right direction)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


      struct ListNode
      {
      Car Car;
      ListNode *next;
      };

      struct CarList
      {

      ListNode * root;
      CarList() : root(0) {};
      void insert(Car);
      void del(Car);
      void findCar(Car);
      void printList();
	  void car_details();

      };
//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);

    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void ListNode::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }
#include "CarClass.h"
using namespace std;



ListNode *start_ptr = NULL;
int main()
{
        start_ptr = new ListNode;
        ListNode *temp = start_ptr;
		char again;

        while(true)
        {
                cout << "Please enter the make of the vehicle: ";
                cin >> temp->make;
                cout << "Please enter the model of the vehicle: ";
                cin >> temp->model;
                cout << "Please enter the color of the vehicle: ";
                cin >> temp->color;
                cout << "Please enter the year of the vehicle: ";
                cin >> temp->year;
                cout << "Please enter the mileage of the vehicle: ";
                cin >> temp->mileage;

                cout<<"Would You like To Enter Another Car (Y/N) :";
                cin>>again;

                if(toupper(again) != 'Y')
                        break;
                temp->next = new ListNode;
                temp = temp->next;
				cout <<"____________________________________________________\n\n";
        }

		cout <<"____________________________________________________\n\n";

        ListNode *details = start_ptr;

        for (int i=0; i<6; i++) 
        {
                details->car_details();
                details = details->next;
        }



	    return 0;
	}

Anyone got any help

Looks like you are using C style syntax for making a list rather than using a list class. Since this is C++ I'd suggest making your own list class, unless, of course, you are allowed to use the STL list class in your program.

If you are going to declare all the classes and define all the methods in one file then declare the Car class before the lisNode class and declare the listNode class before the CarList class.

If you are going to create stand alone header and cpp files for each class then include the Car.h file in the listNode.h file and include the listNode.h file in the list CarList.h file. (Note, using inclusion gaurds in the header files is very helpful if you use this approach). Then include the CarList.h file in the main cpp program (and that will automatically include the other classes in the main cpp program).

main() could look something like this, assuming you have separate header and implementation files for each class as discussed above :

#include <iostream>
#incldue "CarList.h"

int main()
{
    CarList inventory;
     Car carObject;
     bool addAnotherCar = true;
     while(addAnotherCar)
      {
           //get information for carObject (from user, file, wherever)

           //insert carObject into inventory by calling an insert function like this;
           inventory. insert(carObject);

           //determine whether another car is to be inserted and change value of addAnotherCar to stop input if the answer is no
       }

        //print information in inventory here
        inventory.print();

        //add more cars here if you want

         //get information about a car to remove from list here

        //delete desired car from list
        inventory.deleteCar(carObject);

        //do whatever else you need to do, etc
}

Of course, you have to declare each class, determine what variables and methods you need/want the class to have and define each of the methods to do what you want it to do, etc. But, you've already done that for the Car class, so you've had practice writing some of the basic stuff already.

The functions to insert and delete a node from a list can be a little intimidating when you write them for your first list class. Many people, myself included, feel it helps to set down with pencil and paper and try to draw out what you need to do graphically before actually trying to write the code. For example what would it take to make this:

4->3->17->42

look like this:

4->88->3-17->42

or like this:

4->3->42

Having said all that, here's some very rough pseudocode for steps that might be included in an insertion function.

1) pass objectToInsert to the insertion function
2) declare a new listNode object using dynamic memory
3) load the carObjectToInsert into the new listNode
4) determine where you want to insert the new listNode
5) insert the new listNode into the list

The details of how you do all that is up to you. The pencil and paper stuff is most helpful figuring out what to do in step 5.

The print method (or the implementation of an overloaded << operator if you prefer) of a list class is usually pretty straight forward. Something like this should come close to doing it, assuming << is overloaded for the Car class, that is:

void CarList::print()
{
    listNode * current = firstNodeInList;
    while(current != NULL)
       cout << current->car;
}

Ok based on what you have told me Lerner...here is where I stand

H file

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);

    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void CarList::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }

	void CarList::car_details();
      {
      ListNode * current = firstNodeInList;
      while(current != NULL)
      cout << current->Car;
	  }

		struct ListNode
      {
      Car Car;
      ListNode *next;
      };   

      struct CarList
      {

      ListNode * root;
      CarList() : root(0) {};
      void insert(Car);
      void del(Car);
      void findCar(Car);
      void printList();
	  void car_details();
      };

CPP

#include "CarClass.h"
#include <iostream>
using namespace std;

int main()
{
    CarList inventory;
     Car carObject;
     bool addAnotherCar = true;
     while(addAnotherCar)
      {
		char again;
		string temp;

	  	ListNode *start_ptr = NULL;
				cout << "Please enter the make of the vehicle: ";
				Car.getMake;
                cout << "Please enter the model of the vehicle: ";
                Car.getModel;
                cout << "Please enter the color of the vehicle: ";
                Car.getColor;
                cout << "Please enter the year of the vehicle: ";
                Car.getYear;
                cout << "Please enter the mileage of the vehicle: ";
                Car.getMileage;

                cout<<"Would You like To Enter Another Car (Y/N) :";
                cin>>again;

                if(toupper(again) != 'Y')
                        break;
                temp->next = new ListNode;
                temp = temp->next;
				cout <<"____________________________________________________\n\n";
	 
	 	inventory. insert(carObject);

		//Information needs to be changed
	 }
	 	
	 	inventory.car_details();

		//Add more cars

		//Information about a car to remove

		//delete desidred car

		inventory.del(carObject);

		//Print information
		inventory.car_details();

	    return 0;
	}

Please let me know how this looks and if i am doing everything right so far!!!

Sorry to be naggy or dumb but has anyone had a chance to look at this and help me out

Updated Code please review here are my errors and code

ERRORS:
1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::del(class Car)" (?del@CarList@@QAEXVCar@@@Z) referenced in function _main
1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::car_details(void)" (?car_details@CarList@@QAEXXZ) referenced in function _main
1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::insert(class Car)" (?insert@CarList@@QAEXVCar@@@Z) referenced in function _main
1>C:\Users\Justin Puckett\Documents\My Stuff\School Work\Second Semester\Spring 2009\C++\Week11Nodes\Test\Debug\Test.exe : fatal error LNK1120: 3 unresolved externals

CPP

#include "CarClass.h"
#include <iostream>
using namespace std;

int main()
{
    CarList inventory;
     Car carObject;
     bool addAnotherCar = true;

	  	ListNode *start_ptr = NULL;
		for (int i = 0; i<6; i++) {
				cout << "Please enter the make of the vehicle: ";
				&Car::getMake;
                cout << "Please enter the model of the vehicle: ";
                &Car::getModel;
                cout << "Please enter the color of the vehicle: ";
                &Car::getColor;
                cout << "Please enter the year of the vehicle: ";
                &Car::getYear;
                cout << "Please enter the mileage of the vehicle: ";
                &Car::getMileage;
	 
			inventory.insert(carObject);
		
		//Information needs to be changed
	 	
	 	inventory.car_details();

		//Add more cars

		//Information about a car to remove

		//delete desidred car

		inventory.del(carObject);

		//Print information
		inventory.car_details();
		}
	    return 0;
	}

H

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
	void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


		struct ListNode
      {
      Car Car;
      ListNode *next;
      };   

      struct CarList
      {

      ListNode * root;
      CarList() : root(0) {};
      void insert(Car);
      void del(Car);
      void findCar(Car);
      void printList();
	  void car_details();

	 
      };

	  //void CarList::car_details();
      //{
      //ListNode * current = firstNodeInList;
     // while(current != NULL)
      //cout << current->Car;
	  //}

Try writing the CarList functions before trying to use them.

What do you mean, my CarList stuff is after my car class set up..is this incorrect??

What do you mean, my CarList stuff is after my car class set up..is this incorrect??

At the moment, your CarList only has a constructor defined, all member methods (e.g. void insert(Car) ) lack the implementation (or you haven't posted all of the code).

Then a thing that is quite wrong in multiple places, for example

&Car::getMake;

You probably want to change that code to

carObject::getMake();

and fix the other similar errors too.

I tried changing my code to that and it does not work!

Here is my code

#include "CarClass.h"
#include <iostream>
using namespace std;

int main()
{
    CarList inventory;
     Car Car;
     bool addAnotherCar = true;

	  	ListNode *start_ptr = NULL;
		for (int i = 0; i<2; i++) {
				cout << "Please enter the make of the vehicle: ";
				&Car::getMake;
                cout << "Please enter the model of the vehicle: ";
                &Car::getModel;
                cout << "Please enter the color of the vehicle: ";
                &Car::getColor;
                cout << "Please enter the year of the vehicle: ";
                &Car::getYear;
                cout << "Please enter the mileage of the vehicle: ";
                &Car::getMileage;
	 
			inventory.insert(Car);
		
		//Information needs to be changed
	 	
	 	inventory.car_details();

		//Add more cars

		//Information about a car to remove

		//delete desidred car

		inventory.del(Car);

		//Print information
		inventory.car_details();
		}
	    return 0;
	}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;


//Car Class
class Car
{
protected:
    string make;  //make
    string model; // model
    string color;  // color
    int year;  // year
    int mileage;  // miles on car

public:
                //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Car(); //Default constructor
        Car(string, string, string, int, int);
        //mutator and accessor functions
        void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();

        //Check mileage to see if valid
    void valid_mileage(int);
	void car_details();
    string string_car_details();
};

//Sets to default values
Car::Car() {
    make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}
        // My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
    this->make =  make;
    this->model = model;
    this->color = color;
    this->year = year;
    valid_mileage(mileage);
}


void Car::setMake(string make) {
    Car::make = make;
}

void Car::setModel(string model) {
    Car::model = model;
}

void Car::setColor(string color) {
    Car::color = color;
}

void Car::setYear(int year) {
    Car::year = year;
}

void Car::setMileage(int mileage) {
    valid_mileage(mileage);
}


string Car::getMake() {
    return make;
}
string Car::getModel() {
    return model;
}
string Car::getColor() {
    return color;
}
int Car::getYear() {
    return year;
}
int Car::getMileage() {
    return mileage;
}


void Car::valid_mileage(int mileage) {
    if (mileage>=0)
        Car::mileage=mileage;
    else {
        Car::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
        }
    }

        void Car::car_details() {
            cout << "The current car is a " << year << ' ' << color << ' '
                        << make << ' ' << model << " with " << mileage << " miles.\n\n";
        }



        string Car::string_car_details() {
            stringstream buf;
            buf << "The current car is a " << year << ' ' << color << ' '
            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            return buf.str();
        }


		struct ListNode
      {
      Car Car;
      ListNode *next;
      };   

      struct CarList
      {

      ListNode * root;
      CarList() : root(0) {};
      void insert(Car);
      void del(Car);
      void findCar(Car);
      void printList();
	  void car_details();

	 
      };

	  //void CarList::car_details();
      //{
      //ListNode * current = firstNodeInList;
     // while(current != NULL)
      //cout << current->Car;
	  //}

I am getting the following errors when trying to run it!

1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::del(class Car)" (?del@CarList@@QAEXVCar@@@Z) referenced in function _main
1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::car_details(void)" (?car_details@CarList@@QAEXXZ) referenced in function _main
1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::insert(class Car)" (?insert@CarList@@QAEXVCar@@@Z) referenced in function _main

And I have played with this thing for over a week and I can not get it to do the following:

Change the Jeep Cherokee’s year to be 2001. Change the Sentra’s mileage to be 80000. Use the loop to print out the information for the user.

Add Chevrolet Corvette Black 2003 11903
6) Ford Explorer Grey 2004 73922
7) Honda Civic White 2007 12045
to the list

And delete a car from the list

Can someone please assist me??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.