bad alloc error when reading from a file

Hi all,

So I am working on a program for my class and running into "bad alloc" whenever I try to run my code. I get the error at the same point at the end of the while loop in my readInFile function on line 17. Could anyone give me some insight into why my code is causing this problem? I've tried it a couple different ways, but the same result happens.

Thanks!

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
struct periodical {
     string name;
     int barcode;
}

void readInFile(queue<periodical>& magazineList){
    ifstream fin("periodicals.txt");
    periodical temp;
    string tempName;
    string bCode;
    while (!fin.eof()){
        getline(fin, tempName, ',');
        getline(fin, bCode);
        temp.name = name;
        temp.barcode = atoi(bCode.c_str());
        magazineList.push(temp);
    }
};

int main(){

queue<periodical> newPeriodicals;

readInFile(newPeriodicals);

}

Last edited on
http://www.cplusplus.com/forum/general/112111/

eof() would stop too late, loop on the reading operation
while( getline(fin, tempName, ',') and getline(fin, bCode) ){
Last edited on
Thanks a lot for the help! So now it runs all the way through without the bad alloc. Still having some other troubles but it's logic related now! Thanks again.
Topic archived. No new replies allowed.