2

I am trying to read a .txt file into an array of struct in this program and display the content.
The file looks like this:

Smith   Jack    60    45    98  
Harry   Hisk    45    40    78  
Kay     Jacob   35.5  23    45  
Dos      hed    23    20    35  
Noa      Tom    55    12    32  
Joe      Peni   57    49    78  
Vin      San    25.6  23    65.5  
Jes      Dan    24.3  12    78  
Zi       Lee    56    49    99  
Angi     Dev    57    48    97  
Donald   David  60    50    96  
Davis    Lal    47    47    80  
Alvis   Sen     56    46    85  
Jack    Jill    45    45    75  
Messy   Lionel  60    49    100  

The code I'm running:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
    const int SIZE=50;
    int i;
    struct Records {
        string firstname;
        string secondname;
        float test1mark;
        float midtestmark;
        float annualmark;
    }record[SIZE];

    ifstream in("Data.txt");

    if (!in){
    cerr << "File can't be opened! " << endl;
    system("PAUSE");
    exit(1);
    }
    for (int i=0; i < SIZE; i++){
    in >> record[i].firstname >> record[i].secondname 
    >>record[i].test1mark >> record[i].midtestmark >> record[i].annualmark ;
    }
    for (int i=0;i< SIZE;i++) {
        cout << record[i].firstname<<"  ";
        cout << record[i].secondname<<" ";
        cout << record[i].test1mark<<"  ";
        cout << record[i].midtestmark << "  ";
        cout << record[i].annualmark << "   ";
    }   
return 0;
} 

The output I'm getting:

Smith   Jack    60      45      98  
Harry   Hisk    45      40      78  
Kay     Jacob   35.5    23      45  
Dos     hed     23      20      35    
Noa     Tom     55      12      32  
Joe     Peni    57      49      78  
Vin     San     25.6    23      65.5  
Jes     Dan     24.3    12      78  
Zi      Lee     56      49      99  
Angi    Dev     57      48      97  
Donald  David   60      50      96  
Davis   Lal     47      47      80  
Alvis   Sen     56      46      85  
Jack    Jill    45      45      75  
Messy   Lionel  60      49      100  
                nan     0       8.94237e-039  
                4.36192e-039    0       -2.3511e-038  
                0       0       -2.3511e-038  
                0       0       0  
                1.32253e-038    0       1.32251e-038  
                4.2039e-045     0       -2.11122e+037  
                1.32251e-038    0       3.21276e-039  
                1.4013e-045     0       -2.3511e-038  
                1.4013e-045     0       3.76158e-037  
                0       0       3.76158e-037  
                0       0       1.12104e-044  
                4.36195e-039    0       4.36194e-039  
                3.57331e-043    0       6.0615e-039  
                0       0       3.21276e-039  
                4.2039e-045     0       6.41272e-039  
                1.12104e-044    0       6.63812e-039  
                4.36205e-039    0       -2.75237e+038  
                0       0       6.59812e-039  
                6.63426e-039    0       1.4013e-045  
                0       0       6.47961e-039  
                3.21319e-039    0       3.21319e-039  
                6.59812e-039    0       3.21299e-039  
                8.40779e-045    2.24208e-044    6.01433e-039  
                6.6045e-039     0       2.54408e-029  
                0       0       6.6045e-039  
                0       0       6.43946e-039  
                5.88656e-039    0       -4.12495e+011  
                0       0       0
                5.88656e-039    0       2.54408e-029  
                nan     nan     6.43029e-039  
                0       0       0
                5.93823e-039    0       -4.12495e+011  
                0       0       0
                5.93823e-039    0       5.74532e-044  
                nan     nan     5.93837e-039  

Process exited after 0.05447 seconds with return value 0
Press any key to continue . . .

Can someone tell me what's wrong with it? I've tried using pointers but it just got worse. -Beginner

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
Avisaki Usamate
  • 23
  • 1
  • 1
  • 7
  • `for (int i=0; i < SIZE; i++){` -- How do you know you have 50 data items to read? – PaulMcKenzie Aug 04 '16 at 02:34
  • @PaulMcKenzie it's an assignment question and that's in the requirement. – Avisaki Usamate Aug 04 '16 at 02:37
  • I'm not in your class, so I can only answer in general. If you count, you don't have 50 items, thus your loop reads garbage data after the 15th item. You don't write "read loops" like this anyway. You're supposed to loop until end-of-file, or until a preset limit is reached, *whichever comes first*. For you, the eof came first but you kept looping. – PaulMcKenzie Aug 04 '16 at 02:38
  • thanks for that @PaulMcKenzie – Avisaki Usamate Aug 04 '16 at 02:43

3 Answers3

1

Your file has 15 lines, and so you can only read through 15 lines of data. You are using the variable SIZE to control how many lines should be read.

The problem is that SIZE is 50! It is not 15. When you are trying to read past the end of file, the input will not be read past the 16th line. So, the variables after index 15 will be uninitialized, which is undefined.

Either increase the amount of lines in your file to 50, or change SIZE to be 15.

Rakete1111
  • 42,521
  • 11
  • 108
  • 141
1

As the other answer by @Rackete1111 states, you specified too many items, and had your loop that reads the data go over the actual number of items in your file.

Having said this, there really isn't anything wrong (besides wasting space if you presize your array too large) with overstating how many records you have, as long as you write the read loop correctly. The following is the way to write the loop, even if you made the "mistake" of stating 50 items instead of 15:

#include <iostream>
#include <string>
#include <iostream>

using namespace std;

int main(){
    const int SIZE=50;
    int i;
    struct Records {
        string firstname;
        string secondname;
        float test1mark;
        float midtestmark;
        float annualmark;
    };

    Records record[SIZE];

    ifstream in("Data.txt");

    int recCount = 0;  // keep track of actual number of records

    // loop until we reach the end of file, or until we hit SIZE records,
    // whichever comes first
    while (!in.eof() && recCount < SIZE)
    {
        in >> record[recCount].firstname >> record[recCount].secondname 
        >>record[recCount].test1mark >> record[recCount].midtestmark >> record[recCount].annualmark ;
        ++recCount;
    }

    // now recCount == 15 if you have 15 items.

Live Example

Note we have a while loop that will read until the limit is reached (50), or we hit the end-of-file.

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
  • But [while(!eof...) is considered wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) @PaulMcKenzie – Avisaki Usamate Aug 14 '16 at 10:34
-1

And i believe we don't need that

int i;

in the beginning

#include <iostream>
#include <string>
#include <fstream>
using namespace std;



ifstream in("Data.txt");
const int SIZE = 15;
void debugPrint();
void loadData();

struct Records {
    string firstname;
    string secondname;
    float test1mark;
    float midtestmark;
    float annualmark;   
}record[SIZE];

int main()
    {
    loadData();
    debugPrint();
    }

void debugPrint()
{
    for (int i = 0; i < SIZE; i++) 
    {
        cout << record[i].firstname << "  ";
        cout << record[i].secondname << " ";
        cout << record[i].test1mark << "  ";
        cout << record[i].midtestmark << "  ";
        cout << record[i].annualmark << "  " <<endl;        
    }
    system("PAUSE");
}

void loadData()
{   
    for (int i = 0; i < SIZE; i++)
    {
        if (!in)
        {                                               
            cerr << "File can't be opened! " << endl;
            system("PAUSE");
        }

        in >> record[i].firstname >> record[i].secondname
        >> record[i].test1mark >> record[i].midtestmark >> record[i].annualmark;
    }
}
bartbart
  • 1
  • 1
  • 3