Reading Text from File into Struct Array

Hello.
I'm having trouble reading text from a file into a struct array.
I have to do much more with this program, but I first need to load the array with the data from the file.

It seems as though it will only get the data from the first college and then when it runs through the loop for the second college, it doesn't proceed to get the data from the file correctly. I feel like this has something to do with the way in which it's reading the text from the file.

The file looks like this:
University of Illinois
0.50 6
0.75 5
0.50 9
0.75 4
1.00 3
University of Iowa
0.90 4
1.00 6
0.90 3
1.00 2
1.25 4
Syracuse University
1.00 3
1.25 5
1.00 2
1.25 6
2.00 4
The Ohio State University
0.80 5
0.90 4
0.80 7
0905 3
1.50 2
University of California
0.75 1
0.85 2
0.75 4
0.85 2
1.00 5
University of Arizona
1.00 2
1.25 6
1.00 7
1.25 3
1.50 2

The file includes the college location name then under each name is 5 lines which contain the soda price and number of sodas in the machine.

Side note: I'm only having the loop run through 2 colleges at the moment - just for testing purposes.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>     
#include <ctime> 
#define NUM_VENDING 10

using namespace std;

// Enumerated type SodaBrands to hold 5 brands
enum SodaBrands { COKE, DIET_COKE, SPRITE, SPRITE_ZERO, DASANI };

//  Data structure Drinks
struct Drinks
{
	double price;
	int numSodas;
};

// Const int for maximum number of soda brands
const int MAX_SODA_BRANDS=5;

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

vendingMachine array[NUM_VENDING];

// Function Prototypes
void loadVendingMachines (vendingMachine[]);
void showVendingMachines (vendingMachine[]);
SodaBrands convertDrink (string);
void getDrinkName (SodaBrands, string&);
int getLocation(vendingMachine[]);
int getDrinkSelection ();
void makePurchase (vendingMachine[], int, int);


int main()
{
	loadVendingMachines(array);
	
	
	system("Pause");
	return 0;
}

void loadVendingMachines(vendingMachine array[])
{

    ifstream statsIn;
    statsIn.open("vendingMachines.txt"); //open the file
    if (statsIn.fail()) {
        // if statement to announce an error opening the file
        cout << "Error opening the file\n";
    } //end if statement

	if(statsIn.is_open()) {
		for (int count = 0; count < 1; count++){
		getline(statsIn, array[count].location);
		
		statsIn >> array[count].sodas[count].price;
		statsIn >> array[count].sodas[count].numSodas;

		statsIn >> array[count].sodas[count+1].price;
		statsIn >> array[count].sodas[count+1].numSodas;

		statsIn >> array[count].sodas[count+2].price;
		statsIn >> array[count].sodas[count+2].numSodas;

		statsIn >> array[count].sodas[count+3].price;
		statsIn >> array[count].sodas[count+3].numSodas;

		statsIn >> array[count].sodas[count+4].price;
		statsIn >> array[count].sodas[count+4].numSodas;
		} // end for loop
       
	} // end if statement
	
    statsIn.close(); //close the file
	
	// Testing ouptut to see if array stored correctly
	cout << fixed << showpoint << setprecision(2);
	cout << "location 1: " << array[0].location;
	cout << "\nprice 1: " << array[0].sodas[0].price << endl;
	cout << "num sodas 1: " << array[0].sodas[0].numSodas << endl;

	cout << "\nprice 2: " << array[0].sodas[0+1].price << endl;
	cout << "num sodas 2: " << array[0].sodas[0+1].numSodas << endl;

	cout << "\nprice 3: " << array[0].sodas[0+2].price << endl;
	cout << "num sodas 3: " << array[0].sodas[0+2].numSodas << endl;

	cout << "\nprice 4: " << array[0].sodas[0+3].price << endl;
	cout << "num sodas 4: " << array[0].sodas[0+3].numSodas << endl;

	cout << "\nprice 5: " << array[0].sodas[0+4].price << endl;
	cout << "num sodas 5: " << array[0].sodas[0+4].numSodas << endl;

	cout << "--------------------------------------------------------" << endl;
	
	// This is where it stops working correctly
	cout << "location 2: " << array[1].location << endl;
	cout << "price 1 at location 2: " << array[1].sodas[1].price << endl;
    
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	if(statsIn.is_open()) {
		for (int count = 0; count < 1; count++){
		getline(statsIn, array[count].location);
		
		statsIn >> array[count].sodas[count].price;
		statsIn >> array[count].sodas[count].numSodas;

		statsIn >> array[count].sodas[count+1].price;
		statsIn >> array[count].sodas[count+1].numSodas;

		statsIn >> array[count].sodas[count+2].price;
		statsIn >> array[count].sodas[count+2].numSodas;

		statsIn >> array[count].sodas[count+3].price;
		statsIn >> array[count].sodas[count+3].numSodas;

		statsIn >> array[count].sodas[count+4].price;
		statsIn >> array[count].sodas[count+4].numSodas;
		} // end for loop
       
	} // end if statement 

is only reading

University of Illinois
0.50 6
0.75 5
0.50 9
0.75 4
1.00 3


You're not utilising the full potential of for loops.
1
2
3
4
getline(statsIn, array[count].location);
for (int count = 0; count < 4; count++){ 
    statsIn >> array[count].sodas[count].price >> array[count].sodas[count].numSodas;
}

does the same thing as the code above.

You can do the same with line 87 to line 102.
You're not utilising the full potential of for loops.
1
2
3
4
getline(statsIn, array[count].location);
for (int count = 0; count < 4; count++){ 
    statsIn >> array[count].sodas[count].price >> array[count].sodas[count].numSodas;
}



If line getline(statsIn, array[count].location); is outside of the for loop like that, count will not work correctly for the array.

Also, that still does not allow the array to be loaded correctly - doing that will only save the first location, first price, and first number of sodas:
location 1: University of Illinois
price 1: 0.50
num sodas 1: 6

price 2: 0.00
num sodas 2: 0

price 3: 0.00
num sodas 3: 0

....


I know that I'm not using the for loop to it's full potential, but right now I'm just testing to see if the first two locations save, and right now only the first one does.
Topic archived. No new replies allowed.