Cant read in multiple files and store into struct?

Not sure what's going on here, but I'm trying to read in data from multiple files and then store the data into a struct. The first file is read in correctly every time, however when I try to access the contents of the 2nd, 3rd, and 4th nothing is being read in. As far as I'm concerned, you read in a file, then close it. Then on to the next. I'm probably just looking over something here but any help would be great thanks!

Zachary Law

Note: Please exclude my comments, those are just for my professor

Edit: I can post the entire program, but its quite lengthy.

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

struct
{
	string appName, desName, drinkName, mcName; // create variables for appetizers
	double appPrice, desPrice, drinkPrice, mcPrice;
	int appQuantity, desQuantity, drinkQuantity, mcQuantity;
}menu[100];

struct
{
	double tempSubTotal; // used as a temp variable to hold whatever price that we will push into our finalSubTotal.
	double finalSubTotal;

}subTotal;

int mainMenu();
void appetizer(); // function prototypes
void appSelection();
void dessert();
void dessertSelection();
void SubTotal();


int main()
{
	int counter = 1;

	ifstream appInfo, desInfo, drinkInfo, mcInfo;

	// Below reads from our file and stores our information into our struct
	
	appInfo.open("app.txt");
	

	while (!appInfo.eof())
	{
		getline(appInfo, menu[counter].appName);
		appInfo >> menu[counter].appPrice;
		appInfo >> menu[counter].appQuantity;
		appInfo.ignore();
		counter++;
	}
	
	
	appInfo.close();
	desInfo.open("des.txt");
	
	

	while (!desInfo.eof())
	{
		getline(desInfo, menu[counter].desName);
		desInfo >> menu[counter].desPrice;
		desInfo >> menu[counter].desQuantity;
		desInfo.ignore();
		counter++;
	}

	
	desInfo.close();
	drinkInfo.open("drink.txt");
	

	while (!(drinkInfo.eof()))
	{
		getline(drinkInfo, menu[counter].drinkName);
		drinkInfo >> menu[counter].drinkPrice;
		drinkInfo >> menu[counter].drinkQuantity;
		drinkInfo.ignore();
		counter++;
	}

	drinkInfo.close();
	mcInfo.open("mc.txt");
	

	while (!(mcInfo.eof()))
	{
		getline(mcInfo, menu[counter].mcName);
		mcInfo >> menu[counter].mcPrice;
		mcInfo >> menu[counter].mcQuantity;
		mcInfo.ignore();
		counter++;
	}

	mcInfo.close();

	mainMenu();

	system("pause");
	return 0;
}
Last edited on
As far as I'm concerned, you read in a file, then close it. Then on to the next.
In your case it is not so. You have the [unusual] case that data for the same record is distributed across multiple files.

The data has been read, but it is appended. So either you put the reading in a single loop or you reset the counter to 0 each time you start another loop/read.
Of course, its the counter! Thank you sir. :)
@coder777
Last edited on
Topic archived. No new replies allowed.