Implementing cpp file

Write your question here.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
HEADER
#pragma once
#include <iostream>
#include <string>

using namespace std;

class cVehicle
{

private:
	int Model_no;
	string model;
	int MPG;
	double Price;
	int Inventory;




public:
	cVehicle();

	cVehicle(int, string, int, double, int);
	void setVehicle(int, string, int, double, int);
	void printVehicle() const;
	
	int getInven()const;

	void updateInven(int);


	~cVehicle(); // destructor
};


CPP/IMPLEMENTATION FILE

#include "stdafx.h"
#include "cVehicle.h"
#include <iostream>
#include <string>

//int Model_no;
//string model;
//int MPG;
//double Price;
//int Inventory;

using namespace std;

cVehicle::cVehicle()
{

	Model_no = 1;
	model = "";
	MPG = 1;
	Price = 0.0;
	Inventory = 1;
}

cVehicle::cVehicle(int mnum, string mod, int miles, double cost, int inven)
{
	Model_no = mnum;
	model = mod;
	MPG = miles;
	Price = cost;
	Inventory = inven;
}

void cVehicle::setVehicle(int MNUMm, string MODd, int MILESs, double COSTt, int INVENn)
{
	Model_no = MNUMm;
	model = MODd;
	MPG = MILESs;
	Price = COSTt;
	Inventory = INVENn;
}


void cVehicle::printVehicle() const
{

	cout << "MODEL NUMBER is" << Model_no << endl;
	cout << "MODEL IS" << model << endl;
	cout << "MILEAGE IS" << MPG << endl;
	cout << "COST OF THE VEHICLE " << Price << endl;
	cout << "AMOUNT IN INVENTORY IS" << Inventory << endl;
}




void cVehicle::updateInven(int x)
{
	Inventory = Inventory + x;

	cout << " The updated inventory is " << Inventory << endl;
	
	
}so



int cVehicle::getInven() const
{

	return Inventory;

}



cVehicle::~cVehicle()
{
}


MAIN FILE


// ConsoleApplication16.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cVehicle.h"
#include <iostream>
#include <string>

using namespace std;
const int vehicles = 2;


int main()
{

	cVehicle vehicle[vehicles];
	int mnum = 0;
	string mod ;
	int miles = 0;
	double cost;
	int inven =0;
	int counter = 0;
	const string YES ="yes";
	const string NO = "no";
	
	string answer;

	cout << " Do you want to enter the vehicle information: ENTER yes or no" << endl;
	cin >> answer;
	if (answer == NO)
	{
		cout << " Thank you, Have a good day" << endl;
	}

	else if (answer == YES)
	{
		while (answer == YES)
		{
			cout << "Please enter the vehicle information" << endl;
			cout << "Enter the model number " << endl;
			cin >> mnum;
			cout << "Enter the maker and year of the vehicle" << endl;
			cin >> mod;
			cout << "Enter the mileage of the car" << endl;
			cin >> miles;
			cout << "Enter the price of the vehicle " << endl;
			cin >> cost; 
			cout << "Enter the invetory/amount in stock of this vehicle" << endl;
			cin >> inven;
			vehicle[counter].setVehicle(mnum, mod, miles, cost, inven);
			counter++;
			
					cout << "Do you want to enter information for another vehicle" << endl;
					cout << "Enter yes or no" << endl;
					cin >> answer;
					
		}
	}

	int view;
	cout << " Do you want to view the information on vehicle" << counter << endl;
	cout << "which one would you like to view " << endl;
	cin >> view;
	vehicle[view].printVehicle();
	
    return 0;
}






So within the cVehicle.cpp file where the getInven and updateInven is how would you go about implementing that within the main file? Can anyone point me within the right direction?
Hello BITCONNECTLUL,

Welcome to the forum.

The same way you did "printVehicle()" example on line 185. The functions "getInven and updateInven" are member functions of the class just like "printVehicle()".

You would define ClassName variableName;. This creates an object of the class with a "variableName" that you choose. Now you would access any member function by coding variableName.FunctionName();. As long as "FunctionName" has been defined in the class.

Two things you should never do:

1. A header file is the wrong place for "#include" files these should be in the ".cpp" files.

2. NEVER put using namespace std; in a header file. Its use is bad as it is, but in a header file it is asking for disaster. See this for a better explanation:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

I have had some rare occasions where I had to use a "#include" in a header file, but it was tempory and I did remove later.

In the main file I like to put the "#include"s first followed by any header files. I am not sure if it makes any difference, but it seams to work a little better.

So what you have :
1
2
3
4
5
6
#include "stdafx.h"
#include "cVehicle.h"
#include <iostream>
#include <string>

using namespace std;  // <--- Best not to use. 


Might work better as:
1
2
3
4
5
#include "stdafx.h"
#include <iostream>
#include <string>

#include "cVehicle.h" 


I will load up the code and see if I find anything else.

Hope that helps,

Andy
Hello BITCONNECTLUL,

I offer some suggestions if you are interested.

In main I set up the file like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

#include "cVehicle.h"  // <--- I like to put this here after the "#includes".

//using namespace std;

const int vehicles = 2;


int main()
{


As you can see I put a comment on line 6. After some time and a lot of reading here I came to the conclusion that is is best not to use this line and learned to qualify what is in the standard name space with "std::". After a week or so of typing "std::" you will realize that you do not even think about it. I can guarantee that using this line WILL get you in trouble some day. It is not a matter of if, but when.

For line 8 something like this is most often written as const int MAXSIZE{ 2 };. The capital letters will tell you that this is a constant variable that can not be changed. It also lets you know that when used in the program, e.g., as the size of an array or in some kind of loop, that this is the maximum size it can be. I am not saying that there is anything wrong with what you did it is OK and works fine.

In main where you define your variables I like to put the constant variables at the beginning. Call it a personal preference, but when the constant variables are at the top they are easier to find and change. This is a unique way of dealing with user input, but what would happen if someone happened to have the 'Caps Lock" on and typed "NO"? Right now "NO" would not be equal to "no". I would define "answer" as a "char", include the header file "cctype" and write the code as:

1
2
3
std::cout << " Do you want to enter the vehicle information: ENTER yes or no" << std::endl;
std::cin >> answer;
answer = std::tolower(answer);


Now it will not matter if the user enters "N", "n", "Y" or "y" it all comes out as a lower case letter and you do not have to worry about how some user may type this in.

Just so you know and I use this as an example:
1
2
3
4
if (answer == NO)
{
	std::cout << " Thank you, Have a good day" << std::endl;
}


With a single line of code you can write this as:
1
2
if (answer == NO)
	std::cout << " Thank you, Have a good day" << std::endl;

Unless your intention was to add more later. Even if you follow with an else or else if it works the same.

In the while loop I write a prompt and "std::cin >>" like this:
1
2
3
4
std::cout << "Enter the model number: ";
std::cin >> mnum;
std::cout << "Enter the maker and year of the vehicle: ";  // <--- Wouldld be as two separate variables.
std::cin >> mod;

Notice the lack of "std::endl"s in the cout statements. Another personal preference here. I like the input to follow the prompt and not be on the next line.

Consider this "Enter the model number: ". This is stored in an "int" variable, but what would happen if the model number has a letter or more in it? You mighr an opening, but it would stop at the first character. If it started with a character then "cin" would fail and if not dealt with no other input would happen because the stream has failed. A model number is not something you would do any math on, so a "std::string" would work better.

This part "Enter the maker and year of the vehicle: ". I do not completely understand what you had in mind when you set this one up, but I am thinking that two separate variables may work better here. Especially if you should need to do something with the year like a search or like everything in a given year. As it is it would be more work to extract the year.

This is part of a cout: "\nDo you want to enter information for another vehicle". Notice the "\n" at the beginning. This helps to separate lines on the display for easier reading instead of having everything run together.

The lines following the while loop and before the return will display even if you may not need them because you exited the program early. For something quick I did this:
1
2
3
4
5
6
7
8
if (counter)
{
	int view{};  // <--- Should be initialized.
	std::cout << " Do you want to view the information on vehicle" << counter << std::endl;
	std::cout << "which one would you like to view " << std::endl;
	std::cin >> view;
	vehicle[view].printVehicle();
}

This way if counter is zero there is nothing to print. In the end this would be a good start to a function.

In the header file "cVehicle.h" after removing the "#include"s and the "using namespace std;" The rest of the file is OK. A few to many blank lines, but not a problem.

In the ".cpp" file I put the "cVehicle.h" after the "#include"s.

The default ctor has the line "model = "";". Keep in mind that a "std::string" is empty when defined and does not need to be initialized. This is along with a "std::vector", which would be very useful in your program, and there may be one or two others that do not need to be initialized.

A simple way to initialize a variable is to use a set of empty {}s. This will initialize "int" types to zero and "floats and doubles" to 0.0. A "char" would initialize to '\0'. if you need something else just put a number between the {}s. For a string between the {}s just the variable name or what you need between ""s.

I have not checked out all the functions of the ".cpp" file and all the member functions, but the look OK.

Hope that helps,

Andy
Yo sorry for the late reply thanks I didn't expect this type of response I ended up figuring out my error.

1
2
3
4
5
6
7
int x;
	cout << "do you want to change the inventory enter a number whether that be positive or negative" << endl;
	cin >> x;
	vehicle[x].updateInven(int x);

i had int x instead of just x in there. So i removed it from the code since it didn't work. Then I just had to restart VS
 


But also thanks for the tips my professor does say I have too much spacing between my code, I've been reading your response and this is some great advice that I will definitely implement on future projects.
A header file is the wrong place for "#include" files these should be in the ".cpp" files.

It's a little different I think. A header file should only #include the other header files that it needs to compile successfully. In the OP, the .cpp file needs <iostream> and <string> but the header file doesn't, so it's more appropriate to #include <iostream> and <string> in the cpp file.

But if the header file used std::string, then the header file should #include <string>. Otherwise the poor person using the header file has to remember which header file (or files!) have to be #included before it.

Regarding using namespace std; I'll suggest an alternative: if your program uses std::string then add using std::string at the top. Do the same with other symbols. This was you std::don't std::have std::to std::add std::std std::frickin std::everywhere.

But really it's a matter of personal taste, unless you end up using a symbol that conflicts with one in std. In that case you'll need to qualify it.
Topic archived. No new replies allowed.