Lines of zeros when reading from file

I am trying to read the following information from a file:

Dean DeFino 88 98 99
Sally Johnson 78 89 82
Bill Benny 75 79 81
Thomas Billows 78 84 89

However, I get the following output and I can't figure out why:

Dean DeFino 88 98 99
0 0 0
Sally Johnson 78 89 82
0 0 0

Here is the code:
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
 
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int NAMESIZE = 15;
const int MAXRECORDS = 50;
struct Grades                             // declares a structure
{
	char name[NAMESIZE + 1];
	int test1;
	int test2;
	int final;
	
};

typedef Grades gradeType[MAXRECORDS];    
 // This makes gradeType a data type
 // that holds MAXRECORDS
 // Grades structures.

void readIt(ifstream&, gradeType, const int);

int main()

{    
	 ifstream indata;
	 indata.open("graderoll.dat");
	 int numRecord = 4;                
	 gradeType studentRecord; 
     
	 if(!indata)
	 {
		cout << "Error opening file. \n";
		cout << "It may not exist where indicated" << endl;
		return 1;
	 }

	readIt(indata, studentRecord, MAXRECORDS); 
	 
	// output the information 
        for (int count = 0; count < numRecord; count++)
	{
	       cout << studentRecord[count].name << setw(10) 
		    << studentRecord[count].test1
		    << setw(10) << studentRecord[count].test2;
	       cout << setw(10) << studentRecord[count].final << endl;
	}                

	return 0;
}

void readIt(ifstream& inData, gradeType gradeRec, const int max)

{
   int total = 0;
  
   inData.get(gradeRec[total].name, NAMESIZE);
   while (inData)
   {
     inData >> gradeRec[total].test1;
     inData >> gradeRec[total].test2;
     inData >> gradeRec[total].final;
	 	 
	 total++;    

     inData.clear(); 
     inData.get(gradeRec[total].name, NAMESIZE);
    
	  
  }

}

That's the output I get:
Dean DeFino 88        98        99         0
Sally Johnson         78        89        82
-1468902216     28208-1473223517
0n-597602142-1471087524     28208


You should use std::vector for dynamic arrays and std::string for names, thats why these classes exist.
Nobody wants to do dirty work, right? ;)

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

struct Grades                             // declares a structure
{
    std::string name;
    int test1;
    int test2;
    int final;
};

void readIt(istream&, std::vector<Grades>&);

int main()
{
    ifstream indata;
    indata.open("graderoll.dat");
     
    if(!indata)
    {
   	cout << "Error opening file. \n";
 	cout << "It may not exist where indicated" << endl;
	return 1;
    }

    std::vector<Grades> studentRecord;
    readIt(indata, studentRecord);

    // output the information
    for (int count = 0; count < studentRecord.size(); ++count)
    {
           cout << studentRecord[count].name << setw(10)
                << studentRecord[count].test1
                << setw(10) << studentRecord[count].test2;
           cout << setw(10) << studentRecord[count].final << endl;
    }

    return 0;
}

void readIt(istream& inData, std::vector<Grades>& gradeRec)
{
   while (inData.good())
   {
        gradeRec.push_back(Grades());
        std::string pname;

        inData >> gradeRec.back().name;
        inData >> pname;
        gradeRec.back().name += pname;

        inData >> gradeRec.back().test1;
        inData >> gradeRec.back().test2;
        inData >> gradeRec.back().final;
   }
}
Last edited on
If I had it my way I would use strings and vectors! However, this is an assignment and I must learn how to do this with character arrays. It's kind of pointless but nevertheless I must figure out a way to do this.
I SOLVED THE ISSUE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void readIt(ifstream& inData, gradeType gradeRec, const int max)

{
   int total = 0;
  
   inData.get(gradeRec[total].name, NAMESIZE);
   while (inData)
   {
     inData >> gradeRec[total].test1;
     inData >> gradeRec[total].test2;
     inData >> gradeRec[total].final;
	 	 
	 total++;    

     inData.ignore(NAMESIZE, '\n');
     inData.get(gradeRec[total].name, NAMESIZE);
    
	  
  }

}


This is because the character array is limited to 15 characters. So if specify to ignore the next 15 characters and or until a newline escape sequence is encountered, each line of the file is read properly.
But it is okey to use ifstream?
Interesting...

Well: When reading the data and you read the name you allways read 15 characters, the 3rd name has 10 characters (including the whitespace) so you accidently also read the integer after it.
The result is that the rest of the protocoll is broken.

I wonder why the first name does not break the protocoll...

Edit: Nice, good job! :D
Edit2: It still does not work here ._.
Last edited on
Topic archived. No new replies allowed.