File and char help

I need some help with an assignment. The question wants me to check a True/False test. The correct answers are at the top of a text file, followed by a student ID and the students answers. How can I get the first line, the correct answers, into an array. Then take the students answers and load them into a different array?
Here is what I have so far.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std; 


int main()
{
	char studentAnswer[21];
	char answerArray[21];
	string studentID;
	int totalPoints = 0;
	double finalScore = 0;

	ifstream txtFile("txtFile.txt");

		for (int i = 0; i < 21; i++)
		{
			if(studentAnswer[i] == answerArray[i])
			{
				totalPoints =+ 2;
			}
			else if(studentAnswer[i] != answerArray[i])
			{
				totalPoints =- 1;
			}
			else if (studentAnswer[i] == ' ')
			{
				totalPoints =- 2; 
			}
		}

	getchar();
	return 0;
}
You'll need run through each file(considering the answer key and student files are on separate files) and assign the right character into the array. An example would be:


Open file
read a life of your file into your string(this grabs white spaces)
Hint: get the entire line

Repeat for answer file
...


relatively simple.
Last edited on
The problem with that is all the information is on the SAME file. Also, I need to give points based on correct answers and remove points for wrong answers and no answers. If I compare strings, how do I assign points? Also, I NEED to use dynamic arrays. This is what I have so far.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std; 


void studentGrade(int totalPoints, double finalScore);
void getScore(char studentAnswer[arraySize], char answerArray[arraySize], int totalPoints);

const int arraySize = 20;

int main()
{
	char *studentAnswer;
	char *answerArray;
	studentAnswer = new char[arraySize];
	answerArray = new char[arraySize];


	string studentID;
	int totalPoints = 0, i = 0;
	double finalScore = 0;

	ifstream txtFile;
	txtFile.open("txtFile.txt", ifstream::in);

	char ch = txtFile.get();

		while (txtFile.good())
		{
				studentAnswer[i] = (char)ch;
				ch = txtFile.get();
				i++;
		}

		getScore(studentAnswer[arraySize], answerArray[arraySize], totalPoints);

		for (int i = 0; i < 20; i++)
		{
			cout << i << studentAnswer[i] << totalPoints << endl;
		}


	studentGrade(totalPoints, finalScore);
		
	getchar();
	return 0;
}

void studentGrade(int totalPoints, int finalScore)
{
	finalScore = totalPoints/40;
		if(finalScore >= 90 && finalScore <= 100)
		{
			cout << "A" << endl;
		}
		else if (finalScore >= 80 && finalScore <= 89.99)
		{
			cout << "B" << endl;
		}
		else if (finalScore >= 70 && finalScore <= 79.99)
		{
			cout << "C" << endl;
		}
		else if (finalScore >= 60 && finalScore <= 69.99)
		{
			cout << "D" << endl;
		}
		else if (finalScore >= 0 && finalScore <= 59.99)
		{
			cout << "F" << endl;
		}
	
}

void getScore(char studentAnswer[], char answerArray[], int totalPoints)
{
	for (int i = 0; i < 20; i++)
		{
			if(studentAnswer[i] == answerArray[i])
			{
				totalPoints += 2;
			}
			else if(studentAnswer[i] != answerArray[i])
			{
				totalPoints -= 1;
			}
			else if (studentAnswer[i] == ' ')
			{
				totalPoints -= 2; 
			}
		}
}
Topic archived. No new replies allowed.