File i/o not working correctly?

I'm having a lot of issues getting the data from my input file, which is opened in main, to give the right input values. The input for the line to be read is:

10 0 S Y 100.00

I inserted a break point at the cout line, and the compiler told me the values were as follows, exactly:

a -858993460
b -858993460
c -52 '|'
d -52 '|'
e -107374176.

The incorrect data seems consistent by data type, but I haven't been able to do anything with that information. I also noted the '.' at the end of the float, but again, no options seem visible to me.

I also have a completely blank error file. The syntax for my data validation is the same, with different parameters. These errors are of course triggered by the ridiculous data I have, and have been every time I've done a test-run, but the error file itself is still empty.

The variable assignment at the end is unnecessary i think, but I was using that for debugging. I included it for variable context.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void myInput(int & adults, int & children, char & type, char & wknd, float & deposit, int & billNumber)
{
	int a, b;
	char c, d;
	float e;

	ifstream inputFile;
	ofstream error;

	if (!inputFile)                                                              
	{
		error << "File could not be found.";
		cout << "An error occured in. Please check error file." << endl;
	}

	inputFile >> a >> b >> c >> d >> e;

	cout << a << ", " << b << ", " << c << ", " << d << ", " << e << endl;

	adults = a;
	children = b;
	type = c;
	wknd = d;
	deposit = e;
Last edited on
input file, which is opened in main

Regardless of what is done in main(), the variable at line 7
 
ifstream inputFile;
is a local variable inside the function myInput() and is never opened.
Likewise the ofstream error at line 8 is never opened.

If you intend to call this function just once, then it makes sense to open the files here, not in main(). But if this is supposed to be called more than once, then open the files in main(), not here. Pass the ifstream and ofstream variables by reference as parameters.
Thanks, I think that did it! It was part of a loop, so I had to figure out passing files as parameters. This has been infinitely helpful!
Topic archived. No new replies allowed.