Input file not being read (yet not error)

Sorry to bother this forum once again but I've got a program to read the input file as records. The problem is all it does is cout Record Number etc but it doesn't grab the data. I'm sure I've made a mistake and am not asking for hand outs, just a point in the right direction.

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

int main()
{
	const int RANGE = 12;
	string tab[RANGE];
	int i = 0, j = 0;

	ifstream reader ( "records.txt" );

	{
		if ( ! reader )
		{
			cout << "Error opening input file" << endl;
			return -1;
		}

		while ( ! reader.eof() )
		{
			if ( ( i + 1 ) % 4 == 0 )
				getline( reader, tab[ i++ ], '\n' );
			else
				getline( reader, tab[i++], 't' );
		    	        reader.close();
				i = 0;
		}

		while ( i < RANGE )
		{
			cout << endl << "Record Number: " << ++j << endl;
			cout << "Foreman: " << tab[ i++ ] << endl;
			cout << "Surname: " << tab[ i++ ] << endl;
			cout << "Department: " << tab[ i++ ] << endl;
			cout << "Telephone: " << tab[ i++ ] << endl;
		}

	}
}
Last edited on
Your tabs are wrong. Here is the exact same code with better whitespace. This might help you see the problem more clearly:

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
int main()
{
    const int RANGE = 12;
    string tab[RANGE];
    int i = 0, j = 0;

    ifstream reader ( "records.txt" );
    {
        if ( ! reader )
        {
            cout << "Error opening input file" << endl;
            return -1;
        }

        while ( ! reader.eof() )
        {
            if ( ( i + 1 ) % 4 == 0 )
                getline( reader, tab[ i++ ], '\n' );
            else
                getline( reader, tab[i++], 't' );
            
            reader.close();
            i = 0;
        }

        while ( i < RANGE )
        {
            cout << endl << "Record Number: " << ++j << endl;
            cout << "Foreman: " << tab[ i++ ] << endl;
            cout << "Surname: " << tab[ i++ ] << endl;
            cout << "Department: " << tab[ i++ ] << endl;
            cout << "Telephone: " << tab[ i++ ] << endl;
        }
    }
}



EDIT:

Also... is 't' really the separation character? Did you mean for that to be a tab ('\t')?
Last edited on
The books has it has tab so it outputs right. Tried your code but it throws up a bunch of errors I had before.
But by tabs do you mean the white space tab, that's intentional (book teaching me bad habits).
To be honest I don't understand this bit of code
( ( i + 1 ) % 4 == 0 )

No idea what it is suppose to do. Which makes it harder to understand the entire programs and solve errors.
Tried your code but it throws up a bunch of errors I had before.


My code was exactly the same as your code. There is no functional difference.

The only thing I changed was I changed some whitespace so the problem is easier to see.

You are indenting your code in a way which does not reflect what the code is actually doing. I changed the indentation to reflect what the code is actually doing.

Hint: look around line 20 in my code.

To be honest I don't understand this bit of code
( ( i + 1 ) % 4 == 0 )


% gives you the remainder after division. IE: 7 % 4 equals 3 because 7/4 is 1 remainder 3.

In that code... i is a loop counter that increments each loop iteration. So %4 will give you 0 every 4 iterations. So every 4th iteration that condition will be true... and on the other 3 iterations it will be false.
I see which makes sense since the records.txt is seperates by 4 tabs. I see your code and still cannot figure out my issue. Says in the book "Reading up to a \t tab for the first the items and up to \n newline for the fourth item on each line.

EDIT: Oh I added the \t on line 20 for the tab. Still isn't reading the reading and outputting the data, god I hate being a noob lol.
Last edited on
Fixed
1
2
reader.close();
i = 0;


Was in the wrong place, runs fine now like this:

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

int main()
{
	const int RANGE = 12;
	string tab[RANGE];
	int i = 0, j = 0;

	ifstream reader ( "records.txt" );

	{
		if ( ! reader )
		{
			cout << "Error opening input file" << endl;
			return -1;
		}

		while ( ! reader.eof() )
		{
			if ( ( i + 1 ) % 4 == 0 )
				getline(reader, tab[i++ ], '\n' );
			else
				getline( reader, tab[i++], '\t' );
			
			
		}

		reader.close();
		i = 0;
		while ( i < RANGE )
		{
			cout << endl << "Record Number: " << ++j << endl;
			cout << "Foreman: " << tab[ i++ ] << endl;
			cout << "Surname: " << tab[ i++ ] << endl;
			cout << "Department: " << tab[ i++ ] << endl;
			cout << "Telephone: " << tab[ i++ ] << endl;
		}

	}
}


Thank you for the help, this is what I got stuck on last time I tried. Finally seem to be getting me head round it.
Last edited on
Topic archived. No new replies allowed.