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