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.
#include <string>
#include <iostream>
#include "Automobile.h"
usingnamespace 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;
}
usingnamespace std;
It's a bad idea to put usingin a header file because all other files that you #include after that one will be using it too. Put the usingstatement 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:
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.
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.