Help with two dimenstional array and ifstream

Aug 19, 2013 at 4:53pm
Hi everyone this is due in a few hours and have been slaving over it for the past two days.
Basically I'm getting an error when the second function reads the file. It apparently skips the output of the first two Score inputs from the file. See the output to see what I mean.
The top and the bottom should basically be the same with the Averages added. But for some reason it doesn't read it correctly.

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
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<fstream>

using namespace std;

void Input(int Score[5][4],int ID[5]);
void Output(int Score[5][4], int ID[5]);
void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5]);
//int LetterGrade(int Score[5][4], int AvgOne,int AvgTwo, char LGrade1, char LGrade2);

int main()
{
    int Score[5][4];
    int AvgOne[5];
    int AvgTwo[5];
    int ID[5];
    char GradeWeighted[5];
    char GradeAvg[5];
    char LGrade1;
    char LGrade2;
    
    Input(Score,ID);
    Output(Score,ID);
    Avg(Score,ID,AvgOne,AvgTwo);
    system("pause");
    return 0;
   
}

void Input(int Score[5][4],int ID[5])
{ ofstream Student("Student.dat",ios::app);
    for(int i=0;i<5;i++)
        {cout<<"\nEnter ID of Student["<<i<<"]: ";
         cin>>ID[i];
         Student<<ID[i]<<" ";
         for(int j=0;j<4;j++)
             {cout<<"\nEnter Test Score["<<j<<"] of Student["<<i<<"]: ";
              cin>>Score[i][j];
              Student<<Score[i][j]<<" ";}
         Student<<endl;}
    Student.close();
}

void Output(int Score[5][4], int ID[5])
   {
    cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\t"<<endl;
    ifstream Student("Student.dat");
    Student>>ID[0]>>Score[0][0];
    for(int i=0;i<5&&Student.good();i++)
       {
            cout <<ID[i]<<"\t";
            Student>>ID[i];
            for(int j=0;j<4&&Student.good();j++)
            {
                    cout<<Score[i][j]<<" ";
                    Student>>Score[i][j];
            }
            cout<<endl;
        }
       
       Student.close();
}

void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5])
     { 
      cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\tAvg1\tAvg2"<<endl;
      ifstream Student("Student.dat");
      Student>>ID[0]>>Score[0][0];
      for(int i=0;i<5&&Student.good();i++)
        {
              cout <<ID[i]<<"\t";
              Student>>ID[i];
              for(int j=0;j<4&&Student.good();j++)
              {
                      cout<<Score[i][j]<<" ";
                      Student>>Score[i][j];
              }                      
              AvgOne[i]=(Score[i][1]+Score[i][2]+Score[i][3]+Score[i][4])*.25;
              AvgTwo[i]=(0.2*(Score[i][1])+0.3*(Score[i][2])+0.3*(Score[i][3])+0.2*(Score[i][4]))*25;
              cout<<AvgOne[i]<<" "<<AvgTwo[i];
              cout<<endl;
         }
         Student.close();
       
       }


StudID  Score 1 Score 2 Score 3 Score 4
1       100 100 100 100
2       100 0 100 0
3       82 94 73 86
4       64 74 84 94
5       94 84 74
StudID  Score 1 Score 2 Score 3 Score 4 Avg1    Avg2
1       100 100 2 100 75 1765
0       100 0 3 82 39 1002
94      73 86 4 64 59 1360
74      84 94 5 94 66 1582
84      74 54 74 671743 13435190
Press any key to continue . . .
Aug 19, 2013 at 5:53pm
Line 48 is extra I think. Similarly line 68.
Last edited on Aug 19, 2013 at 5:55pm
Aug 19, 2013 at 6:16pm
Tried that, but the misreading still occurred.
Aug 19, 2013 at 9:38pm
Your array works fine, its just the duplicates when populating students here is what I got after removing your student file.

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
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<fstream>

using namespace std;

void Input(int Score[5][4],int ID[5]);
void Output(int Score[5][4], int ID[5]);
void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5]);
//int LetterGrade(int Score[5][4], int AvgOne,int AvgTwo, char LGrade1, char LGrade2);

int main()
{
    int Score[5][4];
    int AvgOne[5];
    int AvgTwo[5];
    int ID[5];
    char GradeWeighted[5];
    char GradeAvg[5];
    char LGrade1;
    char LGrade2;
    
    Input(Score,ID);
    Output(Score,ID);
    Avg(Score,ID,AvgOne,AvgTwo);
    //system("pause");
    return 0;
   
}

void Input(int Score[5][4],int ID[5])
{ ofstream Student("Student.dat",ios::app);
    for(int i=0;i<5;i++)
        {cout<<"\nEnter ID of Student["<<i<<"]: ";
         cin>>ID[i];
         //Student<<ID[i]<<" ";
         for(int j=0;j<4;j++)
             {cout<<"\nEnter Test Score["<<j<<"] of Student["<<i<<"]: ";
              cin>>Score[i][j];
              //Student<<Score[i][j]<<" ";
			  }
         Student<<endl;}
    Student.close();
}

void Output(int Score[5][4], int ID[5])
   {
    cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\t"<<endl;
    ifstream Student("Student.dat");
    //Student>>ID[0]>>Score[0][0];
    for(int i=0;i<5&&Student.good();i++)
       {
            cout <<ID[i]<<"\t";
            //Student>>ID[i];
            for(int j=0;j<4&&Student.good();j++)
            {
                    cout<<Score[i][j]<<"\t ";
                    //Student>>Score[i][j];
            }
            cout<<endl;
        }
       
       Student.close();
}

void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5])
     { 
      cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\tAvg1\tAvg2"<<endl;
      ifstream Student("Student.dat");
      //Student>>ID[0]>>Score[0][0];
      for(int i=0;i<5&&Student.good();i++)
        {
              cout <<ID[i]<<"\t";
              //Student>>ID[i];
              for(int j=0;j<4&&Student.good();j++)
              {
                      cout<<Score[i][j]<<"\t ";
                      //Student>>Score[i][j];
              }                      
              AvgOne[i]=(Score[i][1]+Score[i][2]+Score[i][3]+Score[i][4])*.25;
              AvgTwo[i]=(0.2*(Score[i][1])+0.3*(Score[i][2])+0.3*(Score[i][3])+0.2*(Score[i][4]))*25;
              cout<<AvgOne[i]<<"\t "<<AvgTwo[i];
              cout<<endl;
         }
         Student.close();
       
       }


Aug 20, 2013 at 4:31am
Please show your student.dat file. Do you make sure that it is empty before you run the program? Remember that you are appending to the file not overwriting the contents.

There is a possibility that the earlier bad data is being read everytime.
Topic archived. No new replies allowed.