Hello liayag,
Sorry I had to do this in two parts.
I am not familiar with "reddit", but here most times it is better to post code that will compile. It saves a lot of time this way. Also sometimes in the code that you do not post may contain a problem that you do not see.
Since you say it is working now this is what I did to get it to read the 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
|
#include <iostream>
#include <iomanip> // <--- Added.
#include <limits> // <--- Added.
#include <string>
//#include<sstream> // <--- Not used yet.
#include <vector>
#include <fstream>
#include "vehicleTransactionAgreement.h"
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
size_t index{}, year{}, creditScr{};
int select{};
double msrp{}, dwnPayment{};
std::string name, model, colour, brand, vin;
std::vector<vehicleTransactionAgreement> cars;
//vehicleTransactionAgreement c1;
//Lease d1;
std::ifstream myFile("cardata.txt");
//myFile.is_open(); // <--- Not needed or properly used. Also covered by the next line.
if (!myFile) //Always test the file open.
{
std::cerr << "\n Error opening intput file " << std::quoted("cardata.txt") << std::endl; // <--- Changed.
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 1;
}
std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once.
while (myFile >> index >> year >> brand >> model >> name >> msrp)
{
cars.emplace_back(index, year, brand, model, name, msrp);
//std::cout << std::endl; // <--- Used as a break point for testing.
}
myFile.close();
for (auto& car : cars)
car.print();
//for (size_t i = 0; i < cars.size(); i++)
// cars[i].print();
std::cout << "\n Which car would you like to buy or lease?\n Use the index to pick: ";
std::cin >> select;
while (!std::cin)
{
std::cerr << "\n Invalid choice! Try again.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n Which car would you like to buy or lease?\n Use the index to pick: ";
std::cin >> select;
}
return 0;
}
|
The comments should explain most of what I did,
I changed some of the variables based on what they are used for.
I used "size_t", AKA "unsigned int", for the variables "index", "year" and "creditScr" because these are all variables the should have only a positive number.
In line 20 "colour" is the only variable that has no use at the moment with the code that you have.
In the if statement using "system" anything is not a good idea. The code I put in place of "system" is what I use.
Line 43 comes from the "<iomanip>" header file and if you are going to use a "double" for a money value then you will need the "std::setprecision". Also in the other ".cpp" file I used "std::setw()".
When I first looked at the while loop I saw the right idea, but later realized it is the wrong way to go about it. What I did here is use regular variables to read the file then inside the while loop I used "emplace_back" to make use of these variables. By putting the variables in the () this will first create an object of the class using the overloaded ctor of the class then put this into the vector.
After the while loop I closed the file stream because it is no longer needed and it is a better place for this line. But still may be considered an optional line.
The range based for loop is handy because you are using a vector, but I also fixed the for loop that you started with so you can see what should be done.
std::cin >> select;
is formatted input which means the "cin" expects that a number should be entered. If not the "cin" will be in a failed state and no longer usable until dealt with. Not as robust as it could be, but will catch a non numeric entry.
The "return 0;" at the end of "main" is not needed, but makes a good break point when debugging the code.
Not having your header file I came up with this to get the program to work.
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
|
#ifndef VEHICLE_TRANCATION_AGGREMENT_H
#define VEHICLE_TRANCATION_AGGREMENT_H
class vehicleTransactionAgreement
{
public:
vehicleTransactionAgreement();
//~vehicleTransactionAgreement();
vehicleTransactionAgreement(size_t index, size_t year, std::string brand, std::string model,
std::string name, double msrp) : carIndex(index), carYear(year), carBrand(brand),
carModel(model), carName(name), carMsrp(msrp) {}
// Get functions.
std::string getcarName() const;
std::string getcarColor() const;
double getcarMsrp() const;
double getcarYear() const;
std::string getcarModel() const;
std::string getcarVIN() const;
std::string getcarBrand() const;
int getyearlyPlan() const;
double getdownPayment() const;
double getcreditScore() const;
// Set functions.
void setcarModel(const std::string& model);
void setcarName(const std::string& name);
void setcarColor(const std::string& color);
void setcarMsrp(double msrp);
void setcarYear(size_t year);
void setcarVIN(std::string vin);
void setcarBrand(const std::string& brand);
void setyearlyPlan(int yrlyplan);
void setdownPayment(double downpaymnt);
void setcreditScore(size_t creditscor);
void print() const;
double calculateCost();
private:
size_t carIndex{};
size_t carYear{};
size_t creditScore{};
int yearlyPlan{};
double carMsrp{};
double downPayment{};
std::string carModel;
std::string carColor;
std::string carName;
std::string carBrand;
std::string carVIN;
};
#endif // !VEHICLE_TRANCATION_AGGREMENT_H
|
Lines 1, 2 and 52 is called a header guard and I believe is consider a more portable way of including this file only once.
Breaking up line 10 as I did should not be a problem, but does make it easier to read.
Line 7 is required, but line 8 is not. Also you did not define a function for line 8 in the ".cpp" file. By leaving out line 8 the compiler will still generate a default dtor for you.
The reason for changing line 10 is so that it will work with the "emplace_back" in main.
Not necessary , but I grouped the "get" and "set" function in separate sections. What you did is fine and if that works for you then use it.
I changes some of the variable types in the private section. Order of the variables makes no real difference. I like to put the variables with the smallest storage space first and the largest storage space last. Personal preference, but you are free to do what works for you.
In the "vehicleTransactionsAgreement.cpp" I made these changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include<iostream>
#include <iomanip>
#include <string>
#include <vector>
#include "vehicleTransactionAgreement.h"
constexpr double MULTIPLIER{ 0.01 };
// The get and set functions are OK, as is they compile with no problems, but untested.
void vehicleTransactionAgreement::print() const
{
std::cout << std::setw(2) << carIndex << ". Car Model: " << carYear << " " << carBrand
<< " " << carModel << " " << carName << '\n' << " Car Color: " << carColor << '\n'
<< " Car MSRP: $" << carMsrp << '\n' << std::endl;
}
double vehicleTransactionAgreement::calculateCost()
{
return (carMsrp - downPayment) * MULTIPLIER;
}
|
1. Car Model: 2019 Honda Civic Sedan
Car Color:
Car MSRP: $23000.00
2. Car Model: 2019 Honda Pilot SUV
Car Color:
Car MSRP: $32000.00
3. Car Model: 2019 Honda Odyssey Sedan
Car Color:
Car MSRP: $27000.00
Which car would you like to buy or lease?
Use the index to pick: 2 |
end part 1.