Function not reading in Data

I cant get my function to read in data from my file. Ive tested it with multiple couts (mainly if(!inData) cout<<"no data"; and itll print no data. No idea why it wont work.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <cmath>

using namespace std;

float totalTest(ifstream& inData, string category);

float totalR,totalGr,totalEn,totalGa,totalMo,num1;
int number;

int main()
{
	//declare variables section
	
	
	ifstream inData;
	ofstream outData;
	
	
	
	inData.open("ola6FinancialData.txt");
	
	
	
	
	
	

		
	
	

		
	

	
	do
	{
		cout << "Enter the menu number for the category you are interested in" << endl;
		cout << "And the program will show you how much was spent for that category" << endl;
		cout << endl;
		cout<< "1. Restaurants " << endl << "2. Groceries" << endl <<"3. Mortgage" << endl << "4. Gas" << endl <<
		"5. Entertainment" << endl << "6. Quit the program" << endl;
		cin >>number;
		
		switch (number)
		{
			case 1:  cout << "The total spent for Restaurants is $"<< fixed << setprecision(2) << totalTest(inData,"Restaurants")<< endl << endl; break;
			case 2:  cout << "The total spent for Groceries is $"<< fixed << setprecision(2) << totalTest(inData,"Groceries") << endl << endl; break;
			case 3: cout << "The total spent for Mortgage is $"<< fixed << setprecision(2) << totalTest(inData,"Mortgage") << endl << endl; break; 
			case 4: cout << "The total spent for Gas is $"<< fixed << setprecision(2) << totalTest(inData,"Gas ") << endl<< endl; break;
			case 5: cout << "The total spent for Entertainment is $"<< fixed << setprecision(2) << totalTest(inData,"Entertainment") << endl << endl; break;
			case 6: cout << "Goodbye"<< endl; break;
			default: cout << "That was not a correct choice. Try again." << endl<< endl;
		}
	}
	while (number<=100 && number!=6);

	
	
	
	
return 0;
}



float totalTest(ifstream& inData, string category)
	{	
		
		float total=0;
		string categoryfromFile;

		
		
		inData.open("ola6FinancialData.txt");
		inData.ignore(100,'c');
		inData>>categoryfromFile;
		cout <<categoryfromFile;
		
		while(inData)
		{
		
	if (categoryfromFile==category)
		{	inData.ignore(500, '$');
			inData >> num1;
			total=total+num1;
			
		}
		inData>>categoryfromFile;
		
	return total;
	inData.close();
	}
}
	
I believe your problem is occurring because you're trying to open a file when the stream has already opened a file. Remove the inData.open("ola6FinancialData.txt"); from totalTest().

What I think is happening is because you try to open the file again when the stream already has the file open, the call fails and a failbit is set for the stream object. You won't be able to do anything with the stream unless the error is cleared.

Hope this helps.
Ive already tried that unfortunately. Doesnt seem to make a difference. I had it commented out earlier.

It really doesnt seem to be reading in the data at all, no matter where i put the .open.

it IS however executing the function.
Last edited on
Use the whole path to the file and see if that makes a difference.
Topic archived. No new replies allowed.