Code not reading array accurately

My program reads an id, math and English score from a file. the math is stored in math[] while English is stored in eng[]. These scores are then passed onto a function which determines the letter grades for each score.

My issue is that the everything but the last two scores are being read inaccurately. I can't seem to find the problem.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  #include <iostream>
#include <fstream>
using namespace std;

char mathSearch( int math[]){
    int i;
    char g;
    for(i=0; i<10; i=i+1){
    if (math[i]<50)
        g='F';
    else
    if ((math[i]>=50) && (math[i]<=59))
        g='D';
    else
    if ((math[i]>=60) && (math[i]<=79))
        g='C';
    else
    if ((math[i]>=80) && (math[i]<=89))
        g='B';
    else
    if ((math[i]>=90) && (math[i]<=100))
        g='A';
    }
    return g;

}
char engSearch(int eng[]){
    int i;
    char g;
        for(i=0; i<10; i=i+1){
    if (eng[i]<50)
        g='F';
    else
    if ((eng[i]>=50) && (eng[i]<=59))
        g='D';
    else
    if ((eng[i]>=60) && (eng[i]<=79))
        g='C';
    else
    if ((eng[i]>=80) && (eng[i]<=89))
        g='B';
    else
    if ((eng[i]>=90) && (eng[i]<=100))
        g='A';
        }
    return g;

}
int main(){
    ifstream in;
    ofstream out;
    in.open("scores.txt");
    int num;
    int amt;
    int id;
    int s1;
    int s2;
    char g1;
    char g2;
    int h1;
    int h2;
    int sCounter1;
    int sCounter2;
    int total1;
    int total2;
    double ave1;
    double ave2;
    int math[10];
    int eng[10];
    int i;
    i=0;
    amt=0;
    s1=0;
    s2=0;
    h1=0;
    h2=0;
    total1=0;
    total2=0;
    ave1=0.0;
    ave2=0.0;
    sCounter1=0;
    sCounter2=0;

        cout<<"Id\tMaths\tGrade\tEnglish\t Grade"<<endl;
        cout<<"-------------------------------------"<<endl;

        in>>id;
        while(id!=0){
            in>>s1;
            in>>s2;
            math[i]=s1;
            eng[i]=s2;

            total1=total1+s1;
            sCounter1=sCounter1+1;
            total2=total2+s2;
            sCounter2=sCounter2+1;

            g1=mathSearch(&math[0]);
            g2=engSearch(&eng[0]);

            cout<<id<<"\t "<<math[i]<<"\t "<<g1<<"\t "<<eng[i]<<"\t  "<<g2<<endl;
            i=i+1;

            if (s1>h1)
                h1=s1;
            if (s2>h2)
                h2=s2;

            amt=amt+1;

            in>>id;

        }//eof

        ave1=total1/(sCounter1*1.0);
        ave2=total2/(sCounter2*1.0);
        cout<<endl;
        cout<<"The total number of candidates are: "<<amt<<endl;
        cout<<"The highest mark in Mathematics is: "<<h1<<endl;
        cout<<"The highest mark in English       : "<<h2<<endl;
        cout<<"The average mark in Mathematics is: "<<ave1<<endl;
        cout<<"The average mark in English is    : "<<ave2<<endl;
        cout<<endl;

return 0;
}


In mathSearch(...) and engSearch(...) you are doing the same. You just evaluate the last field of the array (since g is constantly overwritten). In this context it doesn't make sense that math or eng are arrays at all.

Line 88 does not determine the end of file. The stream does not set id to 0 in that case. So change line 88 to:

while(in>>id){ // Note that the expression in>>id returns false in case of an error like eof

Remove line 112/87.

The file is terminated by a 0 in the id location, that's why its while(id!=0), so it doesn't read the zero. The reason eng and math are arrays is so that I can use pointers (g1=mathSearch(&math[0])). Judging by my output, the last two fields are working. What am I doing wrong that gives me this output?

Id	Maths	Grade	English	 Grade
-------------------------------------
3000	 54	 F	 52	  F
2500	 80	 F	 83	  F
3200	 43	 F	 85	  F
4500	 25	 F	 89	  F
3300	 90	 F	 89	  F
4600	 18	 F	 12	  F
1200	 32	 F	 19	  F
1900	 44	 F	 68	  F
2300	 67	 C	 92	  A
4000	 88	 B	 23	  F
Last edited on
Well, only the last check will be successful because you check in mathSearch() and engSearch() only for the last element in the array. You need to understand what the loops are doing there.

All other checks work by accident since the last element in the arrays contain arbitrary values (because not initialized) at that time.
Topic archived. No new replies allowed.