Hey guys, I have been trying to do some stuff outside of my class to learn a bit more and I have come across something called a vector. And I read online that its similar to an array! But I am not sure how to do this. I have been able to find ways to convert and array of int's to a vector and how to convert an array of strings to a vector but nothing on how to convert an array of strings and ints to a vector. Here is the current code I have, you may have seen it before in my previous post just because I decided to use some code I had before that previously worked!!

class

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
#include <vector>  // this allows user to create a vector
using namespace std;

//Vehicle 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, type,bedsize, bike, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
	Car::make =  make;
	Car::model = model;
	Car::color = color;
	Car::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 File

#include "CarClass.h"
using namespace std;

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



	return 0;
}

This is also posted on Dreamin in Code in case anyone see's it there!

http://www.dreamincode.net/forums/index.php?showtopic=164627

#include<iostream>
#include<string>
#include<vector>
using namespace std;

class test
{
 public://Since you want to store ints and strings in your vector,
 int age;//Just create a class containing the ints and strings
 string name;
 test(int a,string b){//Standard ctor
 age=a;name=b;
 }
 test()//Default ctor
{
 age=0;name="Uknown";
 }
};

test* pushback(int i){//We are creating our own sort of push_back() for our class
test* tptr= new test[i];//Since arrays on stack can't take variables, we declare them on the heap
 return tptr;
 }


int main()
{
 cout<<endl<<endl;
 vector<test>vt;//you can create a vector of your class;
 test* top=pushback(3);//This creates 3 objects of class test in the vector
 for(int k=0;k<3;k++){
 top[k].age=k+5;//We are accessing the member variables of out test objects thru the pointer
 top[k].name="test";
 }
 cout<<top[0].age<<endl<<top[1].age<<endl<<top[2].age<<endl;
 cout<<top[0].name<<endl;
 return 0;
}

Hope this helped!!

I ran my code and everything looks perfect on it, but it keeps giving me errors!

class

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>


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) {
	Car::make =  make;
	Car::model = model;
	Car::color = color;
	Car::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 file

#include "CarClass.h"
#include <vector>  // this allows user to create a vector

using namespace std;

int main() {
    
	const int SIZE = 6;
			
	//Array of 6 cars
	Car Car_array[SIZE] = { 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("Nisson", "Sentra", "Red", 2002, 76046),
							Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};

		// Declare a vector capable of storing classes
	// In your case, it will hold classes of type Car
	std::vector<Car> CarVector;
	// Assign individual objects of Car from Car_array,
	// to your vector
	for (int i = 0; i < SIZE; i++)
	    CarVector.push_back(Car_array[i]);

	for (int i = 0; i < SIZE; i++)
	    // Treat CarVector[i] as Car_array[i] here. Since CarVector[i]
	    // is actually a class (of type Car), you can use every public
	    // member functions/variables from Car class
	    std::cout << CarVector[i].getMake() << std::endl;
		std::cout << CarVector[i].getModel() << std::endl; 
		std::cout << CarVector[i].getColor() << std::endl; 
		std::cout << CarVector[i].getYear() << std::endl; 
		std::cout << CarVector[i].getMileage() << std::endl;

	for (int i = 0; i < SIZE; i++)
    CarVector[i].car_details();


	return 0;
}

ERRORS:

1>------ Build started: Project: vector, Configuration: Debug Win32 ------
1> Car.cpp
1>\vector\car.cpp(11): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(12): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(13): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(14): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(15): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(16): error C2661: 'Car::Car' : no overloaded function takes 5 arguments
1>\vector\car.cpp(31): error C2065: 'i' : undeclared identifier
1>\vector\car.cpp(31): error C2228: left of '.getModel' must have class/struct/union
1>\vector\car.cpp(32): error C2065: 'i' : undeclared identifier
1>\vector\car.cpp(32): error C2228: left of '.getColor' must have class/struct/union
1>\vector\car.cpp(33): error C2065: 'i' : undeclared identifier
1>\vector\car.cpp(33): error C2228: left of '.getYear' must have class/struct/union
1>\vector\car.cpp(34): error C2065: 'i' : undeclared identifier
1>\vector\car.cpp(34): error C2228: left of '.getMileage' must have class/struct/union
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

\vector\car.cpp(10): error C2661: 'Car::Car' : no overloaded function takes 5 arguments


I am getting this error of all 6 of my cars in the array


Just in case you guys need to see my code

#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();
	}
#include "CarClass.h"
#include <vector>  // this allows user to create a vector
using namespace std;

int main() {
    
	const int SIZE = 6;
			
	//Array of 6 cars
	Car Car_array[SIZE] = { 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("Nisson", "Sentra", "Red", 2002, 76046),
							Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};

		// Declare a vector capable of storing classes
	// In your case, it will hold classes of type Car
	std::vector<Car> CarVector;
	// Assign individual objects of Car from Car_array,
	// to your vector
	for (int i = 0; i < SIZE; i++)
	    CarVector.push_back(Car_array[i]);

for (int i = 0; i < SIZE; i++)
{
            // Treat CarVector[i] as Car_array[i] here. Since CarVector[i]
            // is actually a class (of type Car), you can use every public
            // member functions/variables from Car class
            std::cout << CarVector[i].getMake() << std::endl;
            std::cout << CarVector[i].getModel() << std::endl; 
            std::cout << CarVector[i].getColor() << std::endl; 
            std::cout << CarVector[i].getYear() << std::endl; 
            std::cout << CarVector[i].getMileage() << std::endl;
}


	for (int i = 0; i < SIZE; i++)
    CarVector[i].car_details();


	return 0;
}

Ok guys my code works but I got a question. How would I make a change to a year or something in this vector and print it for the user??

How would I make a change to a year or something in this vector

See vector::operator[]. At the bottom of that page are links to other vector accessors. Also you might use iterators, also explained there.

>> and print it for the user??
You are doing it already ..

std::cout << CarVector[i].getMake() << std::endl;
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.