### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` `````` #include #include 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"<>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<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<

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.