Getting incorrect reads from file read into array

Apr 18, 2013 at 4:31pm
When i run my program i get a long string of numbers and letters when i should be getting normal data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ifstream fin;
	fin.open("data.txt");
	if(!fin)
	{
		cout << "Error opening file. Closing." << endl;
		exit(1);
	}

	for (int i = 0; i< COUNT; i++)
	{
		fin >> arrayOne[i]
		for (int j= 0; j< COUNT_TWO; j++)
		{
			fin >> arrayTwo[i][j]
		}

	}
	fin.close();


Here is the raw .txt data


2002
2.4 2.7 2.7 2.9 2.8 3.4 3.4 2.2 .9 1.1 1.7 1.9
2003
2.2 2.9 3.3 3.3 3.1 3.3 3.3 2.6 1.5 1.4 1.8 1.8
2004
2.2 2.7 3.1 3.8 3.6 4.0 3.6 2.9 1.7 1.8 2.4 2.1
2005
2.0 2.2 2.2 2.6 2.8 3.2 2.8 2.3 1.2 1.3 2.0 1.9
2006
2.0 2.2 2.2 2.6 2.9 3.1 2.4 1.7 1.0 1.9 2.3 1.9
2007
2.6 2.6 2.4 2.4 2.6 3.1 3.2 3.1 2.0 2.1 2.7 2.6
2008
2.3 2.5 2.2 2.3 2.5 2.5 1.7 1.7 1.0 1.3 1.5 1.9
2009
2.1 2.2 2.4 2.4 3.1 2.7 1.9 1.5 .9 1.9 2.7 2.3





Now i cannot figure out what is doing this. Any hints? My only guess right now is it has something to do with me needing an .ignore

The file reads in 2002 then 032FADC032FB3C and goes on and on and on.

EDIT: Also when i view the .txt in notepad, it shows what looks like a space, then a tab between each piece of data, and what seems to be a space and two tabs between both ".9"'s

Is there a way i can attatch the .txt to this post so everyone can see what im referring to? I cannot seem to find one
Nevermind, even erasing the tabs and leaving a single space doesnt fix it.
Last edited on Apr 18, 2013 at 4:44pm
Apr 18, 2013 at 4:39pm
How do you know that you read 032FADC032FB3C ?
It looks like a pointer.

Also, what is declaration of your arrays?
Apr 18, 2013 at 4:43pm
I know because when i run the program it looks like that. 2002, then a new line, then the string of numbers, then reads 2003 another long string, then 2004 etc.

It seems instead of reading 2.4 then 2.7 it reads in "0032DADC" and "0032FB3C"

My declarations are

int arrayOne[COUNT];
double arrayTwo[COUNT][COUNT TWO];

also my constants
const int COUNT = 8
const int COUNT_TWO = 12
Last edited on Apr 18, 2013 at 4:46pm
Apr 18, 2013 at 4:48pm
Can you modify your loop slightly:
1
2
3
4
5
6
7
8
		fin >> arrayOne[i]
		for (int j= 0; j< COUNT_TWO; j++)
		{
			fin >> arrayTwo[i][j];
                        cerr << arrayTwo[i][j] << " ";
		}
                cerr << endl;

and show output.
Apr 18, 2013 at 4:56pm
2002
2.42.4 2.72.7 2.72.7 2.92.9 2.82.8 3.43.4 3.43.4 2.22.2 0.90.9 1.11.1 1.71.7 1.91.9
2003...

looks better, but its giving me numbers with two decimal points in them.
Last edited on Apr 18, 2013 at 5:00pm
Apr 18, 2013 at 5:05pm
You are making double output (like << array[i][j] << array[i][j]), correct your code.
Apr 18, 2013 at 5:38pm
That got it. Forgot to erase the cout. However, my professor does not allow bits of code he has not gone over yet, and cerr is not part of any code we have discussed, so i cannot use it in my program. Is there another solution?
Apr 19, 2013 at 5:48pm
Use cout instead of cerr.
I prefer use cerr since it flushes immediately.
Topic archived. No new replies allowed.