Structure of Vectors

Ok so I'm having a bit of a problem with reading data in from a file into a vector of type struct. For some reason when I try to read data in from my file handle "fin" I get the errors like:

'class std::vector<CoffeeData, std::allocator<CoffeeData> >' has no member named 'productNum'

and I have no idea why. I've searched around a lot for a similar type of problem someone might have had but I had no luck. Any advice?

Code:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <fstream>
#include "myInput.h"
using namespace std;

struct CoffeeData
{
    string coffeeName;     //Name of coffee
    string body;           //Values are Medium, Full, Very Full
    string descript;       //Description of coffee
    char coffeType;        //Regular or Decaf (R or D, respectively)
    
    int productNum;        //Product number of the coffee
    int QOH;              //Quantity on hand
    double price;          //Price of coffee
};

int main()
{
    //Variables
    
    //Strings, Chars, & File Handles
    ifstream fin;                       //File handle for opening a file
    
    //Ints & Doubles
    int option;
    
    //Arrays, Vectors, & Bools
    vector<CoffeeData> cof;          //Vector of type CoffeeData struct
    bool go = true;                  //Bool statement for main program while loop

    //Open File (coffee.txt)
    openFile("Name of Coffee File: ", fin);
    
    while(!fin.eof())
    {
        int k = 0;
        
        fin >> cof.productNum[k];
        getline(fin, cof.coffeeName[k]);
        fin >> cof.coffeType[k];
        fin >> cof.body[k];
        geline(fin, cof.desscript[k];
        fin >> cof.QOH[k];
        fin >> cof.price[k];
        
        cof.pushback(k);
    }//while

    //freeze shell and then halt program
    cout << endl;
    system("pause");
    return 0;
}//main  


Also I'm a new guy so don't kill me too much.
First of all, this: while(!fin.eof()) is incorrect, if you got this loop structure from a book, get rid of it (if it's from a teacher, well, no such luck)

Second, vector indeed has no member productNum. Vector's k'th element does: the line fin >> cof[k].productNum; would compile, but it would be an error at runtime because when the vector is empty, it has no kth element.

In brief, this is what I think would be closer to what you meant to write:

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

struct CoffeeData
{
    string coffeeName;     //Name of coffee
    string body;           //Values are Medium, Full, Very Full
    string descript;       //Description of coffee
    char coffeType;        //Regular or Decaf (R or D, respectively)
    
    int productNum;        //Product number of the coffee
    int QOH;              //Quantity on hand
    double price;          //Price of coffee
};

istream& operator>>(istream& is, CoffeeData& cof)
{
    is >> cof.productNum;
    getline(is, cof.coffeeName);
    is >> cof.coffeType >> cof.body;
    getline(is, cof.descript);
    is >> cof.QOH >> cof.price;
    return is;
}

int main()
{
    vector<CoffeeData> cof;
    ifstream fin("coffee.txt");

    CoffeeData k;
    while( fin >> k)
        cof.push_back(k);
}

but note that you're mixing up getlines and operator>>'s. Without seeing your input file, it's hard to tell if that's meaningful or not.
Well while(!fin.eof()) does work. As you read in data from the file, the loop will continue until the file reaches the eof state. As for using getlines and operator >>'s, I'm trying to read lines of text in from a file and numbers.

The data in the text file would look like this:

1000
Western Brew
This is a coffee that comes from the west
R
Full
100
15.99

Its basically strings, ints, and doubles. And I was trying to read it in line by line.
Well while(!fin.eof()) does work. As you read in data from the file, the loop will continue until the file reaches the eof state.


That's not true. Your loop continues execution after the file reached the eof state, for another whole iteration, producing one entirely garbage coffee.

It also becomes an endless loop if at any point it encounters a letter when it expects a digit.

The data in the text file would look like this:

1000
Western Brew


So after fin >> productNum reads the characters '1', '0', '0', '0' (and stores the integer 1000), the next character to read from the file will be '\n';
The following getline(fin, cofeeName) will read that endline and return an empty string. That's a common catch when mixing operator>> and getlines: if you have to do that, you'll have to consume the endlines before each getline().
Topic archived. No new replies allowed.