0

So the first part of this program is to pull int data from a .txt file and put them into an array and then output then on the screen, but it seems that the code I have will only display very odd numbers (i.e. -808459), I do not know where this is coming from.

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	int Array1[10];
	int index;

	ifstream FileIn;
	ofstream FileOut;

	FileIn.open ("E:\\fileinput.txt");
	

	
	for (index = 0; index < 10; index++ ){
		FileIn >> Array1 [index];
		cout << Array1 [index] << " ";
	}
	

return 0;
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by VernonDozier
0

All signs point to a bad input stream. You should test that it in fact opens correctly and that it's "good".

http://www.cplusplus.com/reference/iostream/fstream/is_open/
http://www.cplusplus.com/reference/iostream/ios/good/

You should also make sure that it's actually reading things in. Quick way to do this is to initialize your array to some nonsense value like -999999999. Stick this after line 9.

for(index = 0; index < 10; index++)
{
    Array1[index] = -999999999;
}

If you get a display of -999999999 anywhere, you'll know that nothing was ever read from the file.

Also check to make sure that Windows uses / instead of \ as a separator. I can never remember.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.