Student Grades Using File I/O and Arrays

I'm required to input the student id, exercises, total of the ten assignments, midterm, the final, and extra credit points. Combined, the assignments are all out of 400 points. I'm required to output all the info onto a file. But I looked at the output file and realized there was one that's very unusual. Here's the source code and output file. It didn't display 98 and said 3981e+002 without outputting the percentage.

Source 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
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
128
129
130
131
132
133
134
135
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

int asscalc(int [], int);
int calc(int, int, int, int, int);

using namespace std;

int main(){
    ifstream infile;
    ofstream outfile, outfileagain;
    const int outof = 400;
    const int ELEMENTS = 10;
    int ass[ELEMENTS], stdntid, ex, tot = 0, mi, fin, cl, pts, i, countA = 0, countB = 0, countC = 0, countD = 0, countF = 0, sign;
    double pct;
    string gr;
//Input File
    infile.open("C:\\Users\\leewi\\Desktop\\Computer Programs & Projects\\C++\\BentleyCIS22B\\CIS22BAss1\\ass1data.txt");
//Output File
    outfile.open("C:\\Users\\leewi\\Desktop\\Computer Programs & Projects\\C++\\BentleyCIS22B\\CIS22BAss1\\studentreportoutputfile.txt");
//Ignore the outfileagain manipulator for now. This is to output the quantity of As, Bs, Cs, Ds, and Fs.
    outfileagain.open("C:\\Users\\leewi\\Desktop\\Computer Programs & Projects\\C++\\BentleyCIS22B\\CIS22BAss1\\grades.txt");
    outfile << "Stdnt Id  Ex  ------- Assignments ---------  Tot  Mi  Fin  CL  Pts  Pct  Gr" << endl;
    outfile << "--------  --  -----------------------------  ---  --  ---  --  ---  ---  --" << endl;
    if(infile.fail()){
        cout << "Can't Open File";
        exit(0);
    }
        do{
            infile >> stdntid;
            if (stdntid <= 10000000){
                outfile << 0;
            }
            infile >> ex;
            for(i = 0; i < 10; i++){
                infile >> ass[i];
            }
            infile >> mi;
            infile >> fin;
            infile >> cl;
            outfile << stdntid << setw(4) << ex << setw(4);
            for (i = 0; i < 10; i++){
                outfile << ass[i] << setw(3);
            }
            tot = asscalc(ass, i);
            pts = calc(ex, tot, mi, fin, cl);
            pct = (static_cast<double>(pts)/outof) * 100;
            sign = static_cast<int>(pct) % 10;
            if (pct >= 90){
                gr = "A";
                countA++;
            }
            else if (pct >= 80){
                gr = "B";
                countB++;
            }
            else if (pct >= 70){
                gr = "C";
                countC++;
            }
            else if (pct >= 60){
                gr = "D";
                countD++;
            }
            else{
                gr = "F";
                countF++;
            }
            outfile << setw(5) << tot << setw(4) << mi << setw(5) << fin << setw(4) << cl << setw(5) << pts << setw(5) << setprecision(2) << pct << setw(4) << gr;
            if (sign <= 1){
                outfile << "-" << endl;
            }
            else if (sign >= 9){
                outfile << "+" << endl;
            }
            else{
                outfile << endl;
            }
        }while(!infile.eof());

        cout << "Check Student Report on student \"reportoutputfile.txt\" & Grades on \"grades.txt\"" << endl;

            outfileagain << "Number of A's = " << countA << endl;
            outfileagain << "Number of B's = " << countB << endl;
            outfileagain << "Number of C's = " << countC << endl;
            outfileagain << "Number of D's = " << countD << endl;
            outfileagain << "Number of F's = " << countF << endl;

        infile.close();
        outfile.close();
        outfileagain.close();

return 0;
}

int calc(int ex, int tot, int mi, int fin, int cl){

    int pts;
    pts = ex + tot + mi + fin + cl;
    return pts;

}

int asscalc(int ass[], int i){
    int tot = 0;

    int startScan, minIndex, hold;

    const int ELEMENTS = 10;

   for (startScan = 0; startScan < (ELEMENTS - 1); startScan++)
   {
      // find index of the largest value
      minIndex = startScan;
      for(int index = startScan + 1; index < ELEMENTS; index++)
      {
         if (ass[index] > ass[minIndex])
         {
            minIndex = index;
         }
      }
      // swap
      hold = ass[minIndex];
      ass[minIndex] = ass[startScan];
      ass[startScan] = hold;
   }

    for(i = 0; i < 9; i++){
        tot += ass[i];
    }

return tot;
}


Output 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
Stdnt Id  Ex 
--------  --  -----------------------------  ---  --  ---  --  ---  ---  --
77997913  38  19 16 16 16 20 20 16 20 16 16  159  56   60   3  316   79   C+
87969978  30  20 11 14 19 11 13 14  8 19 10  131  60   77   7  305   76   C
81564051  21  14 12 11 18  9 17 11 15  9 20  127  76   90   6  320   80   B-
25744204  37  18 16 19 14 16 13 16 15 17 20  151  68   74   8  338   84   B
53057551  24   9 12 15 10 15 11 20 16 17 20  136  22   62   8  252   63   D
49778442  31   9  8  6  7 20 10 16  7 16  5   99  79   45  10  264   66   D
01234567  17  19 18 15 19 14 14 17 15 16 20  153  37   65   5  277   69   D+
34664035  38  20  7 15 20 17 15 16 16 16  5  142  34   83   9  306   76   C
50077769  28  10 20 13 17 20 13 18 19 11 18  149  55   99   6  337   84   B
94439268  39  17 18 17 17  7 19 10 20 18 14  150  77   82   5  353   88   B
75809789  16  17  6  9 16  8 10 17 19 15 19  130  60   68   7  281   70   C-
93888835  21  19 20  7 16 18 19 14  8 14 18  146  55   83   9  314   78   C
77126205  29  18 16 18 19 15 16 18 20 20 19  164  51   53  10  307   77   C
95209093  24  16 18 12 16 18 19 10 16 20  7  145  78   74   5  326   82   B-
82409983  34  13 18 19  5 10 16  6  7 19 16  124  22   42   1  223   56   F
99219262  28  19 17 18 15  7  4 14 18 16 18  142  25   88   8  291   73   C
45809717  32  19 14 14 20 20 19 18 17 14 17  158  50   55  10  305   76   C
90299943  28  20 16 13 15  1 14 12 12  5 16  123  47   77   7  282   70   C-
76629944  26   5 20 20 14 18 16 11  5 19 12  135  77   92   8  338   84   B
49944436  35  13 20 13 12 17 16 16 18 18  6  143  50   68   5  301   75   C
91396740  40  17 19  9 15 20 16 12 18 15 20  152  27   98   4  321   80   B-
99926844  16  17 20  7  2 16  4 16 14 11 17  122  58   83   4  283   71   C-
83582695  39  17 20 19 20 14 20 19 13 18 17  164  34   63   7  307   77   C
86575634  30  18 17 14 19  9 13 16  9 18 17  141  78   61  10  320   80   B-
74536060  24  18 15 18 16 12 14 13 16 11 17  139  76   30   8  277   69   D+
75927432  19  17 17 16 15 18 15 12  5 17 10  137  74   72  10  312   78   C
96618874  27   6 14 14 19 15 20 20 20 14 20  156  77   98   9  367   92   A-
82679771  34  11 10 19 17 20 14 16 18  4 15  140  55   52   6  287   72   C-
47715766  37  20 19  9 19 15 16 16  7 19 18  151  62   95  10  355   89   B
84855076  38  20 15 17 16 11  6 19 20 20 15  153  60   94   8  353   88   B
91767175  34  16 11 18 14 18 19 11 16 14 20  146  69   47   5  301   75   C
77637878  30  17 16 20 11 15 18 13  8 19  8  137  66   71   7  311   78   C
54945864  16  11  9 15 11 13 17 16  8  8 19  119  61   66   9  271   68   D
62946127  22  19 14 19 20 20 19 20 20 20 14  171  63   97   8  361   90   A-
91659955  39  18  5 18 13 19 18 10 20 13 17  146  76   79  10  350   88   B
31904387  26  16 14 10 19  7 11 13 18 15 10  126  44   40   7  243   61   D-
97467762  34  13 20 16 14 18 13 19 16 11 18  147  80   81  10  352   88   B
87623194  40  15 17 16 18 19 18 19 19 17  6  158  57   71   8  334   84   B
91644504  29  13 14  9 19 15 20 20 18 12 17  148  75   72   8  332   83   B
92199190  39  20  4 10  6 14 14 17 14 19 15  129  78   31   9  286   72   C-
62413521  17  18 20 17 15  8 12 17 13 15 19  146  67   90   7  327   82   B-
81169639  35  20 17 17 15 19 17 11 13 17 16  151  66   96   7  355   89   B
77899606  10  16 11 15 12 17 16 12 11  3 10  120  78   44  10  262   66   D
29469155  36  19 17 19 14 18 16  8 16 16  5  143  68   68   1  316   79   C+
54394937  32  19 20 16 11 19 19 17 13 14 10  148  78   93   8  359   90   B+
98139730  37  19 10 11 20 17 17 20 16  9 18  148  60   79   8  332   83   B
76543210  29   3 10 10 10 15 10 15 10 15 10  105  50   71   0  255   64   D
76543211  19   0 15 10 10 15 10 15 10 15 10  110  50   70   0  249   62   D
87654321  37  20 19 20 19 20 19 20 19 20 19  176  78   98   9  3981e+002   A+
97526730  34   6 13 20 17 20 13 17 19  7  9  135  79   87   8  343   86   B
97526730  34  20 20 19 17 17 13 13  9  7  6  135  79   87   8  343   86   B
> Here's the source code and output file
¿where's the input file?
¿how do you expect us to run your program? (¿how do you expect us to analyse it without run it?)

> Ignore the outfileagain manipulator for now. This is to output the quantity of As, Bs, Cs, Ds, and Fs.
it's your job to remove all the irrelevant bits. (that may also help you to identify the relevant parts)

> do{/*...*/} while(not in.eof());
get pen and paper, follow your code and realise that that stops too late (as you may see in the output file, the last line is duplicated)
use while(infile >> stdntid){/*...*/} instead

> int asscalc(int ass[], int i){
¿what does `i' represent? ¿and why you overwrite it on line 130?

> ass[ELEMENTS], stdntid, ex, tot = 0, mi, fin, cl, pts, i
¿do they charge you by letter?

> said 3981e+002 without outputting the percentage.
read that as 398 1e+002
Last edited on
Topic archived. No new replies allowed.