reading .txt file into array of nested structs

closed account (j1354iN6)
Hey guys, I don't do a lot of C++ programming so this is starting to drive me a little crazy.

so basically I'm trying to read in 'vending machine data' to an array of "vendingMachine" structs that has a nested "drinks" struct. The text file is as such:

Pine Hills Mall
0.75 6
1.00 3
0.50 2
0.75 3
1.00 4
Maple Woods Library
0.50 2
0.75 3
etc..

There are six locations, all with five drinks and prices.
I can get the info for the first vending machine in, and after that it doesn't read and I can't figure out why. Thanks for any help. This is the struct definitions:
1
2
3
4
5
6
7
8
9
 struct Drinks {
	double price;
	int numSodas;
};

struct vendingMachine {
	string location;
	Drinks sodas[MAX_SODA_BRANDS];
};


And this is my attempt to read in the information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void getData(vendingMachine vMachine[]) {
	//Imports data for vending machines


	ifstream inputFile;
	inputFile.open("Vending_Machine_Data.txt", ios::in);


	for (int i = 0; i <= 6; i++)  {
			getline(inputFile, vMachine[i].location);
			for (int j = 0; j < 5; j++) {
			    inputFile >> vMachine[i].sodas[j].price;
			    inputFile >> vMachine[i].sodas[j].numSodas;
				
			}
		}
}


I'm just not sure why it doesn't continue to read after the first iteration through the inner loop. Any help would be appreciated.
There will still be a newline on the row 1.00 4 (fifth row of Pine Hills Mall's data) so the next getline() call will read a blank line, after which you'll try and read Maple Woods Library into a double which will put the input stream into an error state.

Use istream::ignore() so after reading your price data.
http://www.cplusplus.com/reference/istream/istream/ignore/
(read info on Parameters carefullyl!!)

Andy
Last edited on
closed account (j1354iN6)
for(int i = 0; i != endOfTime; i++)
facepalm();

Wow, thanks Andy.
I don't know why that didn't occur to me.
You're a lifesaver.
1
2
while(true)
    facepalm();


???
Topic archived. No new replies allowed.