Reading in a file (with ints and strings) using getline

Hello out there.

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.

Thank you in advance!!!!

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
//main.cpp
#include <iostream>
#include <vector>
#include <fstream>
#include "Vehicle.h"

using namespace 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 :)
    }
This will give you an example. It's similar to what you have given us.
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace 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)
    return false; // 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();
    return true;
  }
}


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
Last edited on
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
Last edited on
I KNOW WHY IT GIVES ME THAT ERROR... I didn't write a data type before the function name. :)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Vehicle {

private:
    int year;
    string make;
    string model;

public:
    Vehicle();  //Default constructor

    Vehicle(int _year, string _make, string _model);

//    Set setters

    void setYear(int _year) {year = _year;}
    void setMake(string _make){make = _make;}
    void setModel(string _model){model = _model;}

//    Set Getters
    int getYear(){return year;}
    string getMake(){return make;}
    string getModel(){return model;}
};


with this said and following your example i set my code to look like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

bool readFile(vector<Vehicle>& vector1)
{
//    Open file
ifstream infile;

    if(!infile)
        return false;
    else
    {
        Vehicle temp;
        while(infile >> temp.setYear(int _year)
        {
            infile.ignore();
            getline(infile, temp.setMake(string _make));
            getline(infile, temp.setModel(string _model));
            vector1.push_back(temp);
        }
        infile.close();
        return true;
    }


and now the compiler gives me this error:
error: expected '(' for function-style cast or type construction
while(infile >> temp.setYear(int _year)

any tips on a solution?
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:

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
bool readFile(vector<Vehicle>& vehicleObject) {
  ifstream inFile("veh.txt");
  if(!inFile)
    return false; // 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();
    return true;
  }
}
Last edited on
It worked! Thank you for your help fiji.
Topic archived. No new replies allowed.