logic error

The input file is as follows:
first line: these are the test answers
every other line: ID followed by student answers
The problem is that the last set of student answers belonging to ID BAC54309 is not being displayed, neither is the first character of any other set of student answers. I'm screwing up somewhere here... Yes this is homework. Please help me.

1
2
3
4
5
6
7
8
9
10
TFFTFFTTTTFFTFTFTFTT
ABC54301 TFTFTFTT TFTFTFFTTFT
BAC54302 FFFTFTTTTTTFTFTFTFTF
BAC54303 TTFTFTTTTTTFTFTFTFTF
BAC54304 TFTTFTTTTTTFTFTFTFTF
BAC54305 TFFFFTTTTTTFTFTFTFTF
BAC54306 TFFTTTTTTTTFTFTFTFTF
BAC54307 TFFTFFTTTTTFTFTFTFTF
BAC54308 TFFTFTFTTTTFTFTFTFTF
BAC54309 TFFTFTTFTTTFTFTFTFTF


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
 	/*  1. OPEN FILE
	    2. COUNT STUDENTS
    	3. ALLOCATE DYNAMIC ARRAYS
    	4. RETURN FSTREAM TO BEGINNING OF FILE
    	5. STORE ALL IDs, ANSWERS IN PARRALLEL
    	6. CLOSE FILE
    	7. CALCULATE POINTS AND GRADE
    	8. PRINT ID, ANSWERS, POINTS (SCORE), AND
    	GRADE FOR EACH STUDENT
    	9. DEALLOCATE DYNAMIC ARRAYS
	*/

/******************************************************/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

// GLOBAL VARS
ifstream inFile;
char *stdtID, **stdtAN, tstAN[20];
int *stdtPTS, stdtCT = 0, tmpPTS = 0;
double *stdtGRD;
bool first_iteration = true;
string strTmp;
char chTmp;

// OPEN FILE
inFile.open("tst_grades");

// COUNT STUDENTS
while ( getline(inFile, strTmp)) {
    if (first_iteration == false) ++stdtCT;    
    else if (first_iteration == true) first_iteration = false;
}

// ALLOCATE DYNAMIC ARRAYS
stdtID = new char[stdtCT];

stdtAN = new char* [stdtCT];
for (int j = 0; j < stdtCT; j++) {
	stdtAN[j] = new char[20];
}

stdtPTS = new int[stdtCT];
stdtGRD = new double[stdtCT];

// RETURN FSTREAM TO BEGINNING OF FILE
inFile.clear();
inFile.seekg(0, ios::beg);

// STORE ALL IDs, ANSWERS IN PARALLEL
first_iteration = true;
for (int i=0; i<stdtCT; i++) {
    
    if (first_iteration == false) {
    	for (int s=0;s<8;s++) {
	    	stdtID[s] = inFile.get();

	    }

	    inFile.get(chTmp); // chomp space between ID and answers
	    for (int s=0;s<20;s++) {
	    	stdtAN[i][s] = inFile.get();
			cout << stdtAN[i][s]; // <-- printing string from here
	    }

    } 
    else if (first_iteration == true) {
    	getline(inFile, strTmp); // chomp first line
    	first_iteration = false;
    }
	inFile.get(chTmp);
		   
}
// CLOSE FILE
inFile.close();


// CALCULATE POINTS AND GRADE
for (int i=0; i<stdtCT; i++) {
	for (int s=0; s<20; s++) {
		if (stdtAN[i][s] == tstAN[s]) tmpPTS += 2;
		if ((stdtAN[i][s] != tstAN[s]) && (stdtAN[i][s] != ' ')) tmpPTS += 1;
	}
	stdtPTS[i] = tmpPTS;

	stdtGRD[i] = ((tmpPTS / 40) * 100);

	tmpPTS = 0;
}

// PRINT ID, ANSWERS, POINTS (SCORE), AND GRADE FOR EACH STUDENT


// DEALLOCATE DYNAMIC ARRAYS
for (int i=0; i < stdtCT; i++) {
	delete [] stdtAN[i];
}
delete [] stdtID;
delete [] stdtAN;
delete [] stdtPTS;
delete [] stdtGRD;

return 0;
}




FTFTFTT TFTFTFFTTFT
FFTFTTTTTTFTFTFTFTF
TFTFTTTTTTFTFTFTFTF
FTTFTTTTTTFTFTFTFTF
FFFFTTTTTTFTFTFTFTF
FFTTTTTTTTFTFTFTFTF
FFTFFTTTTTFTFTFTFTF
FFTFTFTTTTFTFTFTFTF
Where is the print code?
A minor point, you test this
1
2
3
4
        if (first_iteration == false) {
        }
        else if (first_iteration == true) {
        }

The second "if" is redundant.
1
2
3
4
        if (first_iteration == false) {
        }
        else {
        }


But onto the other points. Lines 72 to 76
1
2
3
4
5
    else if (first_iteration == true) {
    	getline(inFile, strTmp); // chomp first line
    	first_iteration = false;
    }
	inFile.get(chTmp);

Two problems here. One, the getline will consume the 'newline' character, so the get() which follows takes the first character of the data, which mis-alighns all of the following reads.
Secondly, the getline() discards the first line of the file. While it does that, the loop counter i on line 57 is incremented by 1. That results in the first row of the data array being left empty, and the last data row is never read from the file.

Other points. stdtID[] is allocated with a length equal to the number of data rows in the file. Luckily in this case that is enough space to hold the 8-character id of a single student at a time. But it gets overwritten each time with the ID of the next student.

Suggestions. Move the code to discard the first line outside the loop.
Allocate space to store all the student IDs. I'd recommend allowing space for a null terminator, so that the ID can be printed an handled as a normal C-string. You may want to similarly make the storage for each stdtAN 21 chars long, to allow for a null terminator.

One more suggestion. Run your code with a debugger to trace the data values as the program executes.
Last edited on
Thank you, Chervil for your prompt and extremely helpful response!

- Rusty
Topic archived. No new replies allowed.