C-Type String Losing Data

Hey. I have this program that reads an input file. First it reads the answer key, and it should stay constant afterwards, but I lose data somehow. I can't figure out why. Help is appreciated!

Data File
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF


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
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

void getgrade(char answersheet[], char response[],	float &percent, float &score, char &grade);

int main()
{
    ifstream infile("Ch8_Ex6Data.txt");

    char studentid[9];
    char response[21];
    char answersheet[21];
	float score = 0;
	float percent = 0;
	char grade;

    infile.getline(answersheet,21) ;
    cout << answersheet << endl << endl;
	cout << "Student ID --- Response --- Score --- Grade" << endl;

    while ( infile.getline(studentid,9, ' ') && infile.getline(response, 21) )
	{

        cout << studentid << " '" << response << "' ";

		getgrade(answersheet,response,percent, score, grade);

		cout << score << " " << grade << endl;

		score = 0;
		percent = 0;
	}
	cout << answersheet << endl;

	system("pause");
	return 0;
}

void getgrade(char answersheet[], char response[],	float &percent, float &score, char &grade)
{
	for (int i = 0; i < 20; i++)
	{
		if (answersheet[i] == response[i])
			score = score + 2;
		else if (answersheet[i] = ' ')
			score = score;
		else if (answersheet[i] != response[i])
			score = score - 1;
	}


	percent = ((score/40) * 100);
				
	if (percent >= 90)
		grade = 'A';
	if (percent >= 80 && percent < 90)
		grade = 'B';
	if (percent >= 70 && percent < 80)
		grade = 'C';
	if (percent >= 60 && percent < 70)
		grade = 'D';
	if (percent < 60)
		grade = 'F';

}



Output

TTFTFTTTFTFTFFTTFTTF

Student ID --- Response --- Score --- Grade
ABC54102 'T FTFTFTTTFTTFTTF TF' 30 C
DEF56278 'TTFTFTTTFTFTFFTTFTTF' 30 C
ABC42366 'TTFTFTTTFTFTFFTTF' 30 C
ABC42586 'TTTTFTTT TFTFFFTF' 28 C
T  TFT T TFT F TF TF
Press any key to continue . . .
On line 48, there is an assignment (=) rather than comparison (==). (It's corrupting your answer sheet.)
Last edited on
I must have checked for that a thousand times...no sleep makes me fail. Thank you!
Topic archived. No new replies allowed.