I am having trouble reading in a file that holds info about cars (year,make,model). I need to create a vector of objects for each element including one vehicle object. I want to call the fxn that reads in the file and save the file into a vector before I sort and search recursively and iteratively.
my problem is figuring out to use getline. I've been reading on what getline is. (i know the name explains what the fxn actually does). If someone can help me out on how to exactly use getline for my code that would be great.
//main.cpp
#include <iostream>
#include <vector>
#include <fstream>
#include "Vehicle.h"
usingnamespace std;
int main() {
vector<Vehicle>vector1;
int input;
ifstream infile;
readFile(ifstream & infile, vector<Vehicle> & vector1);
// create menu with while statement
while (input != 8)
{
cout << "1.) Sort by make then search by make recursively." << endl
<< "2.) Sort by model then search by model recursively." << endl
<< "3.) Sort by year then search by year recursively." << endl
<< "4.) Sort by make then search by make iteratively." << endl
<< "5.) Sort by model then search by model iteratively." << endl
<< "6.) Sort by year then search by year iteratively." << endl
<< "7.) Exit" << endl;
// Receive user input
cin >> input;
//vehicle in.txt
2010
Ford
Escape
2014
BMW
328xi
2014
BMW
428xi
2012
Ford
Fusion SE
2014
Lamborghini
Gallardo
1967
Pontiac
GTo
1983
DeLorean
DMC-12
1990
Audi
80 Sedan
//functions.cpp
void readFile(ifstream &infile, vector<Vehicle>& vector1)
{
// Open file
infile.open("vehiclein.txt");
int year;
string make;
string model;
if(infile.is_open())
{
getline() //okay now what :)
}
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
usingnamespace std;
/* I am going to assume that your Vehicle class looks something like this, and
* because you didn't provide us your Vehicle.h, I'm going to use the below
* data structure as an example. */
struct Vehicle {
int vehicleYear; // A integer to hold for the vehicle year
string vehicleName; // A string for the vehicle name
string vehicleModel; // A string for the vehicle model
};
/* Prototype the readFile function here that has a parameter of
* vector<Vehicle>& */
bool readFile(vector<Vehicle>&);
int main() {
vector<Vehicle> vehicleObject;
if(!readFile(vehicleObject))
return 1;
else {
// Simple way of printing the vector
for(auto iter = vehicleObject.cbegin(); iter!= vehicleObject.cend(); iter++)
cout << iter->vehicleYear << " "
<< iter->vehicleName << " "
<< iter->vehicleModel << endl;
}
return 0;
}
bool readFile(vector<Vehicle>& vehicleObject) {
/* The reason why I only pass one parameter into this function is because you
* named this function readFile. Because it's named readFile, why would you
* want to pass an std::ifstream object into it? Unless you are going to be
* reading from the file in some other function, let's not pass it the
* std::ifstream object as a parameter */
ifstream inFile("veh.txt");
if(!inFile)
returnfalse; // If the object was unable to open the file
else {
/* We know the text file containing the vehicles looks something like this
* Int
* String
* String
* So we have to read it as such. */
Vehicle temp;
/* EDIT: Forgot to explain this part...
It basically says while our object inFile can read the from the file and input
it into our temp data structure, let's also perform the follow - which is getline
of the vehicle name and model. The ignore() is just a function I like to call to
prevent getline "skipping over". If you remove the ignore(), things will not read
properly. */
while(inFile >> temp.vehicleYear) {
inFile.ignore();
getline(inFile, temp.vehicleName);
getline(inFile, temp.vehicleModel);
vehicleObject.push_back(temp);
}
inFile.close();
returntrue;
}
}
And when I run it, I will get the following:
2010 Ford Escape
2014 BMW 328xi
2014 BMW 428xi
2012 Ford Fusion SE
2014 Lamborghini Gallardo
1967 Pontiac GTo
1983 DeLorean DMC-12
1990 Audi 80 Sedan
So, my Vehicle.h does look something similar to that. I just used a class instead of a struct. As far as my readFile fxn.. I believe i will be passing it into other fxn if you count sorting, search by recursive/iterative fxns as well. Anywho, for my prototype fxn I changed it to the following:
readFile(vector<Vehicle>& vector1);
and when I build my code i keep getting errors on that line. The following error appears:
error: expected '(' for function-style cast or type construction
readFile(vector<Vehicle>& vector1);
~~~~~~~~~~~~~~~^
do you have an idea why it is telling me this?
Thank you for your help so far
I followed your example on using a while statement but i am getting an error for some reason. I used getter and setters for my code in my vehicle.h file.
EDIT: For some reason, I accidentally removed the part explaining your while loop. Let's analyze it...
On line 12, while(infile >> temp.setYear(int _year), you are inputting into a function instead of a data type. You are thinking correctly, but the way you are doing it is wrong. You can't set values to a function, so that's why we use parameters. The same thing goes with your getline functions. Consider the following:
bool readFile(vector<Vehicle>& vehicleObject) {
ifstream inFile("veh.txt");
if(!inFile)
returnfalse; // If the object was unable to open the file
else {
int year;
string make, model;
while(inFile >> year) {
inFile.ignore();
getline(inFile, make);
getline(inFile, model);
vehicleObject.push_back(Vehicle(year, make, model));
/*
You can either push_back by pushing back a constructor which you have
made, or you can do the following. Give it a try, though I personally prefer the former.
Vehicle foo;
foo.setYear(year);
foo.setMake(make);
foo.setModel(model);
vehicleObject.push_back(foo);
*/
}
inFile.close();
returntrue;
}
}