Changing Class Methods with User Input

Hello,
I am very new to programming and C++ so please forgive me for any glaring issues/errors/ and bad code organization and layout. I am creating a class Automobile on a header file and would like some assistance on how to change the class methods with input given by the user in order to create a new car. I have tried cin and getline but honestly am not sure what else there is out there for me to try and use in order to accomplish this.

---First the .h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include <string>
using namespace std;

class Automobile
{
	//----------------attributes------------------
private:
	string make;
	string model;
	string vin;  // vehicle identification number
	string color;
	string fueltype;
	string vtype;
	int year;
	int tophp;
	double odometer; // mileage of the vehicle
	double fuel; //gallons of fuel in the tank
	double tankSize; // fuel capacity in gallons
	double mpg; //miles-per-gallon

	//-----------------methods-------------------------------
public:
	Automobile(string carMake, string carModel, string carVin, string carColor, string carFueltype, string carType) //constructor
	{
		make = carMake;
		model = carModel;
		vin = carVin;
		color = carColor;
		fueltype = carFueltype;
		vtype = carType;
		year = 2014;
		tophp = 231;
		odometer = 0;
		tankSize = 20;
		fuel = tankSize;
		mpg = 23;
	}
	// ------------------accessor methods "getter" methods---------------------------------
	string getMake()
	{
		return make;
	}
	string getModel()
	{
		return model;
	}
	string getVin()
	{
		return vin;
	}
	string getColor()
	{
		return color;
	}
	string getFueltype()
	{
		return fueltype;
	}
	string getVtype()
	{
		return vtype;
	}
	double getFuel()
	{
		return fuel;
	}
	int getYear()
	{
		return year;
	}
	int getTophp()
	{
		return tophp;
	}
	double getMileage()
	{
		return odometer;
	}
	//--------------- mutator methods "setter" methods------------------------------
	double addFuel(double amount)
	{
		if (amount + fuel > tankSize)
			amount = tankSize - fuel;

		fuel += amount;

		return amount; // return the amount actually put in

	}
	void paint(string newColor)
	{
		color = newColor;
	}
	double drive(double distance)
	{
		if (distance / mpg > fuel)
			distance = fuel*mpg;

		odometer += distance;
		fuel -= distance / mpg;

		return distance;

	}
	void otherMake (string newMake)
	{
		make = newMake;
	} 
	void otherModel(string newModel)
	{
		model = newModel;
	}
	void otherVin(string newVin)
	{
		vin = newVin;
	}
	void otherYear(int newYear)
	{
		year = newYear;
	}
};


Now the .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <string>
#include <iostream>
#include "Automobile.h"

using namespace std;

int main() {

	Automobile myCar("Ford", "Fusion", "3FA6P0HD9ER244868", "Black", "Gasoline","Mid-Size Sedan"); 
// -----------------------Display myCar Attributes-----------------------------------------------------------------------
	cout << "This is the car you currently have: " << endl;
	cout << "Make: " << myCar.getMake() << endl;
	cout << "Model: " << myCar.getModel() << endl;
	cout << "V.I.N. (Vehichle Identification Number): " << myCar.getVin() << endl;
	cout << "Color: " << myCar.getColor() << endl;
	cout << "Fuel Type: " << myCar.getFueltype() << endl;
	cout << "Car Type: " << myCar.getVtype() << "\n" << "------------------------------------------------------------------------" << endl;

//---------------Create New Car----------------------------------------------------------------------------------
	cout << "\nCongratulations, you just won $1 million dollars!! Please type in the information for the new car you wish to purchase with your winnings: " << endl;

// And this is the part I'm stuck on....I have tried cin and getline but don't know what else I can try in order to change the make, model, etc. into whatever the user inputs...don't really know how to set it up to obtain the input and store it.

cin.ignore();
	cin.get();
	return 0;

}
Last edited on
Header files should not have anything in them like this;

Auto.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef   __AUTO__
#define  __AUTO__

#include <string>

class Automobile  {
	std::string  make, model, vin, color, vtype;
	int year, tophp, odometer, fuel, tankSize;
	double mpg;
	
public:
	Automobile ();
	bool details ();
};

#endif 
The reason is you might want to distribute your works as a library, but you don't want to have all the methods exposed.

Auto.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include	"Auto.h"
#include	<iostream>

Automobile::Automobile () {
	make = model = vin = color = vtype = "";	
}

bool Automobile::details () {
	std::cout << " Make: ";
	std::cin >> make;
	std::cout << "Model: ";
	std::cin >> model;
	
	return true;
}


Vehicle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

using namespace std;

#include "Auto.h"

	vector <Automobile> Vehicle;
	
int main (void) {
	
	do {
	cout << "\n---> ";

  // Declaring car here reinitializes everything due to default constructor.
	Automobile Car;
		
		if ( Car.details () )
			Vehicle.push_back (Car);
		else
			break;
		} 	while (1);
	
	return 0;
}


Obviously there is a lot of work to be done to this code, but it does demonstrate how you can build a dynamic array of the class using vectors.
using namespace std;
It's a bad idea to put using in a header file because all other files that you #include after that one will be using it too. Put the using statement in your .cpp file instead.

Header files should not have anything in them... The reason is you might want to distribute your works as a library, but you don't want to have all the methods exposed.

It's true that exposing the methods will expose some of your code, which may be a trade secret etc., but the downside is that methods in a separate compiled file can't be inlined. And since distributing your code as a library invariably means distributing a header file with the library, it's okay and encouraged to put trivial methods in the header file.

You can create a new car with input from the user like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string make, model, vin, color, fuel, type;
    cout << "Enter Make: ";
    getline(cin, make);
    cout << "Enter Model: ";
    getline(cin, model);
    cout << "Enter VIN: ";
    getline(cin, vin);
    cout << "Enter color: ";
    getline(cin, color);
    cout << "Enter fuel type: ";
    getline(cin, fuel);
    cout << "Enter type of vehicle: ";
    getline(cin, type);

    Automobile car(make, model, vin, color, fuel, type);

Thank you both for your explanations and help. I have changed my code so that I can use getline(cin, string); .

Now, one thing I struggle to understand is arrays and vectors. Could you please guide me to good sources I could use to comprehend better? I wish to learn as much as I can about C++ and from as many sources so that I make sure I comprehend everything correctly.
Last edited on
If you like learning by watching, this youtube series is solid for the basics of C++. He has videos on arrays and vectors - https://www.youtube.com/playlist?list=PL2DD6A625AD033D36 Note that you can simply youtube and google "C++ Vectors" and get good videos and materials to study from.

Otherwise, this website's tutorial on array is pretty good - http://www.cplusplus.com/doc/tutorial/arrays/

Last edited on
Great, thank you very much for those links!
Topic archived. No new replies allowed.