Help with inFiles & outFiles

I'm so close to finishing this program. Please offer your assistance, if you are skilled. ^_^


Here is the Question/Problem I am attempting to solve.

"The history teacher at your school needs help in grading a True/False test. The students' IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFTFTT

Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry...

ABC54301 TFTFTFTT TFTFTFFTTFT

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%-100%, A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; 0%-59.99%, F."


This is what I've come up with...
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main() 
{
    string id;
    char answers [20]; 
    char response; 
    char fileName [25];
    int testScore;
    
    cout << "\nThis program will take the teacher's correct test answers";
    cout << "\nAs well as the 150 students answers, and grade the tests.\n" << endl;
    
    cout << "Enter the input file name: ";
    cin >> fileName;

    ifstream inFile; 
    inFile.open (fileName);
    inFile.close();
    
    for (int i=0; i<20; i++)
        inFile >> answers [i];
    
    while ((inFile >> id))
    {
        cout << "\n" << id << " ";
        inFile.get(response);
        testScore = 0;
        
        for (int i = 0; i < 20; i++)
        {
            inFile.get(response);
            cout << " " << response;
            
            if (response == ' ')
                testScore += 0;
            else
                if (response == answers [i])
                    testScore += 2;
                else
                    testScore += -1;
        }
        
        cout << "   " << testScore << "  ";
        
        double p = testScore * 2.5;
        
        if (p >= 90)
            cout << 'A';
        else
            if (p >=80)
                cout << 'B';
            else
                if (p >=70)
                    cout << 'C';
                else
                    if (p >=60)
                        cout << 'D';
                    else
                        cout << 'F';
    }
    
    cout << "\nStudent IDs     |     Answers |  % |  Grades";
    cout << "\n--------------------------------------------" << endl;
    
    return 0;
}


When I run the program, the output is...
This program will take the teacher's correct test answers
As well as the 150 students answers, and grade the tests.

Enter the input file name: grades.txt

Student IDs | Answers | % | Grades
--------------------------------------------

Yet there should be 150 Student IDs, their Answers, % correct, and their Grade.

What am I doing wrong?
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
TFFTFFTTTTFFTFTFTFTT

ABC54301 TFTFTFTT TFTFTFFTTFT
ABC59310 FTTFTFTTFTTFTTTFTFTT
ABC39583 FTTFTFTTFTFFFTFTTFTF
ABC38493 FTFTTFTFTFTTF TFFTTT
ABC10394 FTTFTFTFFTFTFTTFTTFT
ABC10006 FTFTFFFTFFTFTFTFFTFT
ABC10007 FTFTFFTFTFTTTTFFTTFT
ABC38558 FTFTTFTFFTFFFTFTFTFF
ABC10439 FTTTFTFTFTFTTFTFTFFT
ABC10010 FTTFTFFFFTFTTTFTFTFT
ABC10011 FTFFTFTTFTFTFFFTFTFT
ABC10512 FTFFTTTFTFTTTFTFTFTF
ABC10013 FFTTFTFTFFFTFTFFTFTT
ABC10014 FTTFTTFTFTTTFTFTTFTT
ABC10415 FTTFTTFTTFTTTTTFTFTF
ABC10016 FTTTFTFTTFFTFTTFTFTT
ABC10017 FTTFTTFTTFTTFTFTTFTT
ABC10018 FTFTTFTTFTFTTFTFTTTF
ABC10019 FTFTTFTTTFTFTTFFTTTF
ABC10020 FTTFTFTTFTFTTFFTFTFT
ABC10021 FTFFTTTFTTFTFFTTTTFT
ABC10022 FTTFTFTFTTFTFTFTTTFT
ABC10023 FTFTFTFTFTTFTFTFTFTT
ABC10024 FTTTFTTFTFTFTFTTFTFT
ABC11025 FTTFTTFTFFFTFTTFTFTF
ABC10026 FTTFTFTFTTTFTFTFTFTT
ABC10027 FTFTTFTFTTFTFTFTTTFT
ABC10028 FTTFTTFTTFTFTTTFTFTT
ABC10029 TFTTFTTFTTFTTFTFTTTF
ABC10030 TFTTFTTFTFTTFTFTTFTF
ABC10031 FTTFTFTFTFTFTFFTTFTF
ABC10032 FTTFFTTFTFTTFTFTFTFT
ABC10033 FTTFTFTTFTFTTFTFTTFT
ABC10034 TFTFTFFFTFTFTFTTFTFT
ABC10035 FTTFTTFTTFTFTFTTFTTT
ABC10036 TFTFTFTTTFTTFTTFTFTT
ABC10037 TFTFFTTFTFTTFTFFTTFT
ABC10038 FTTFTTFTTFTFTFTTFTFT
ABC10039 TTFTFTTFTTFTFFTTFTFT
ABC10040 FTFTTFTTFTFTFFTFFTTF
ABC10041 FTFFTFTTFTFTFFFTFTFT
ABC10542 FTFFTTTFTFTTTFTFTFTF
ABC10143 FFTTFTFTFFFTFTFFTFTT
ABC10044 FTTFTTFTFTTTFTFTTFTT
ABC10445 FTTFTTFTTFTTTTTFTFTF
ABC10046 FTTTFTFTTFFTFTTFTFTT
ABC10047 FTTFTTFTTFTTFTFTTFTT
ABC10048 FTFTTFTTFTFTTFTFTTTF
ABC10049 FTFTTFTTTFTFTTFFTTTF
ABC10050 TFTTFTTFTFTTFTFTFTFT
ABC10051 TFTTFTFTFTFTFTFTFTFT
ABC10552 FTFFTTTFTFTTTFTFTFTF
ABC10053 FFTTFTFTFFFTFTFFTFTT
ABC10054 FTTFTTFTFTTTFTFTTFTT
ABC10455 FTTFTTFTTFTTTTTFTFTF
ABC10056 FTTTFTFTTFFTFTTFTFTT
ABC10057 FTTFTTFTTFTTFTFTTFTT
ABC10058 FTFTTFTTFTFTTFTFTTTF
ABC10059 FTFTTFTTTFTFTTFFTTTF
ABC10060 FTTFTFTTFTFTTFFTFTFT
ABC10061 FTFFTTTFTTFTFFTTTTFT
ABC10062 FTTFTFTFTTFTFTFTTTFT
ABC10063 FTFTFTFTFTTFTFTFTFTT
ABC10064 FTTTFTTFTFTFTFTTFTFT
ABC11065 FTTFTTFTFFFTFTTFTFTF
ABC10066 FTTFTFTFTTTFTFTFTFTT
ABC10067 FTFTTFTFTTFTFTFTTTFT
ABC10068 FTTFTTFTTFTFTTTFTFTT
ABC10069 TFTTFTTFTTFTTFTFTTTF
ABC10070 FTFTTFTTFTTFTFTFTFTF
ABC10071 FTFTFFFTFTTFTFTTTTFT
ABC10072 FFTTTFTTFTFTFTFTFTFF
ABC10073 FTTFTTFTFTFTFTTFTFTT
ABC10074 FTTFTFTTFFTTTFTFFTFT
ABC10075 FTFTTTFTFTFTFTFTFTFT
ABC54376 TFTFTFTT TFTFTFFTTFT
ABC59377 FTTFTFTTFTTFTTTFTFTT
ABC39578 FTTFTFTTFTFFFTFTTFTF
ABC38479 FTFTTFTFTFTTF TFFTTT
ABC10380 FTTFTFTFFTFTFTTFTTFT
ABC10081 FTFTFFFTFFTFTFTFFTFT
ABC10082 FTFTFFTFTFTTTTFFTTFT
ABC38583 FTFTTFTFFTFFFTFTFTFF
ABC10484 FTTTFTFTFTFTTFTFTFFT
ABC10085 FTTFTFFFFTFTTTFTFTFT
ABC10086 FTFFTFTTFTFTFFFTFTFT
ABC10587 FTFFTTTFTFTTTFTFTFTF
ABC10088 FFTTFTFTFFFTFTFFTFTT
ABC10089 FTTFTTFTFTTTFTFTTFTT
ABC10490 FTTFTTFTTFTTTTTFTFTF
ABC10091 FTTTFTFTTFFTFTTFTFTT
ABC10092 FTTFTTFTTFTTFTFTTFTT
ABC10093 FTFTTFTTFTFTTFTFTTTF
ABC10094 FTFTTFTTTFTFTTFFTTTF
ABC10095 FTTFTFTTFTFTTFFTFTFT
ABC10096 FTFFTTTFTTFTFFTTTTFT
ABC10097 FTTFTFTFTTFTFTFTTTFT
ABC10098 FTFTFTFTFTTFTFTFTFTT
ABC10099 FTTTFTTFTFTFTFTTFTFT
ABC11100 FTTFTTFTFFFTFTTFTFTF
ABC10101 FTTFTFTFTTTFTFTFTFTT
ABC10102 FTFTTFTFTTFTFTFTTTFT
ABC10103 FTTFTTFTTFTFTTTFTFTT
ABC10104 TFTTFTTFTTFTTFTFTTTF
ABC10105 TFTTFTTFTFTTFTFTTFTF
ABC10106 FTTFTFTFTFTFTFFTTFTF
ABC10107 FTTFFTTFTFTTFTFTFTFT
ABC10108 FTTFTFTTFTFTTFTFTTFT
ABC10109 TFTFTFFFTFTFTFTTFTFT
ABC10110 FTTFTTFTTFTFTFTTFTTT
ABC10111 TFTFTFTTTFTTFTTFTFTT
ABC10112 TFTFFTTFTFTTFTFFTTFT
ABC10113 FTTFTTFTTFTFTFTTFTFT
ABC10114 TTFTFTTFTTFTFFTTFTFT
ABC10115 FTFTTFTTFTFTFFTFFTTF
ABC10116 FTFFTFTTFTFTFFFTFTFT
ABC10117 FTFFTTTFTFTTTFTFTFTF
ABC10118 FFTTFTFTFFFTFTFFTFTT
ABC10119 FTTFTTFTFTTTFTFTTFTT
ABC10120 FTTFTTFTTFTTTTTFTFTF
ABC10121 FTTTFTFTTFFTFTTFTFTT
ABC10122 FTTFTTFTTFTTFTFTTFTT
ABC10123 FTFTTFTTFTFTTFTFTTTF
ABC10124 FTFTTFTTTFTFTTFFTTTF
ABC10125 TFTTFTTFTFTTFTFTFTFT
ABC10126 TFTTFTFTFTFTFTFTFTFT
ABC10127 FTFFTTTFTFTTTFTFTFTF
ABC10128 FFTTFTFTFFFTFTFFTFTT
ABC10129 FTTFTTFTFTTTFTFTTFTT
ABC10130 FTTFTTFTTFTTTTTFTFTF
ABC10131 FTTTFTFTTFFTFTTFTFTT
ABC10132 FTTFTTFTTFTTFTFTTFTT
ABC10133 FTFTTFTTFTFTTFTFTTTF
ABC10134 FTFTTFTTTFTFTTFFTTTF
ABC10135 FTTFTFTTFTFTTFFTFTFT
ABC10136 FTFFTTTFTTFTFFTTTTFT
ABC10137 FTTFTFTFTTFTFTFTTTFT
ABC10138 FTFTFTFTFTTFTFTFTFTT
ABC10139 FTTTFTTFTFTFTFTTFTFT
ABC11140 FTTFTTFTFFFTFTTFTFTF
ABC10141 FTTFTFTFTTTFTFTFTFTT
ABC10142 FTFTTFTFTTFTFTFTTTFT
ABC10143 FTTFTTFTTFTFTTTFTFTT
ABC10144 TFTTFTTFTTFTTFTFTTTF
ABC10145 FTFTTFTTFTTFTFTFTFTF
ABC10146 FTFTFFFTFTTFTFTTTTFT
ABC10147 FFTTTFTTFTFTFTFTFTFF
ABC10148 FTTFTTFTFTFTFTTFTFTT
ABC10149 FTTFTFTTFFTTTFTFFTFT
ABC10150 FTFTTTFTFTFTFTFTFTFT


Here are the contents of my Text document. "grades.txt"
I can not figure out how to make these applicable to my program. :(

HEEEEEEELLLPPPP!!!! Please. ^_^
You are closing the file before reading it. Doesn't make much sense.
I have completely removed the... inFile.close()... yet, I still receive the same result. :(
Any clues?
Don't completely remove inFile.close() move it after the while loop.

Code works for me. (Get some negative %s though.)

The problem is maybe that the program doesn't find grades.txt.

Is grades.txt in the same folder as the program?

If not, enter the full path for the file at the prompt, e.g. C:\grades.txt

You should output an error message if inFile is not opened.
Last edited on
You read the file one character at a time. You never account for newlines. You never attempt to read in a student id, treating all characters you encounter after the first set of answers as quiz answers.

Feel free to completely remove inFile.close(), since the file will be automatically closed by the destructor. It definitely would seem your program is not finding the file. Make sure you've got it in the right place.

Feel free to completely remove inFile.close(), since the file will be automatically closed by the destructor. It definitely would seem your program is not finding the file. Make sure you've got it in the right place.


That's good to know. Tutorials I have done in other languages always emphasised closing the file so that's why I made the comment.


You never attempt to read in a student id


I think it's done here.

while ((inFile >> id))




So it is. That's what I get for posting when I'm half-asleep.
nothing like late night coding
Put a break point @ while ((inFile >> id)) verify you are reading the id on the first and second pass... Check a few responses as well. Report back your findings.
Topic archived. No new replies allowed.