Programming exercise with dynamic arrays.

I'm trying desperately to solve a programming exercise, but I keep getting stuck every time. Here is the problem...

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; and 0%–59.99%, F.

I could have done this on my own if it was static arrays, however, I need to do this problem with DYNAMIC ARRAYS. That's what is destroying my brain. My assignment is due tomorrow and I spent 3 days trying to find an answer. Please help!

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

char gradeLetter(double percent);

int main(){

	ifstream inFile;
	ofstream outFile;
	inFile.open("testscore.txt");
	outFile.open("testscoreoutput.txt");

	if (inFile.fail()){
		cout << "File not found." << endl;
	}
	else{
		cout << "File found! Working..." << endl;
	}

	int maxSpace = 20;
	char* testAnswers = new char[maxSpace];
	char* stuAnswers = new char[maxSpace + 1];
	string studentID;
	int i;
	double score = 0;
	char grade;

	for (i = 0; 1 < maxSpace; i++)
	{
		inFile.get(testAnswers[maxSpace]);
	}
	while (!inFile.eof())
	{
		inFile >> studentID;
		inFile.get();

		cout << studentID << endl;
		for (i = 0; i < maxSpace + 1; i++)
		{
			inFile.get(stuAnswers[i]);
		}

		for (i = 0; i < maxSpace; i++)
		{
			if (testAnswers[i] == stuAnswers[i])
			{
				score = score + 2;
			}
			if (stuAnswers[i] == ' ')
			{
				score = score;
			}
			if (testAnswers[i] != stuAnswers[i])
			{
				if (score == 0){
					//do nothing
				}
				else{
					score = score - 1;
				}
			}
		}
		outFile << "Student: " << studentID << endl;
		outFile << "Answers: ";
		for (i = 0; i < maxSpace; i++){
			outFile << stuAnswers[i];
		}
		double percent = (score / 40) * 100;
		outFile << "Test Score: " << percent << "%" << endl;
		outFile << "Grade :" << gradeLetter(percent) << endl;
	}
	delete[] stuAnswers;
	delete[] testAnswers;
	inFile.close();
	outFile.close();

	return 0;
}

char gradeLetter(double percent)
{
	if (percent >= 90)
		return 'A';
	else if (percent >= 80)
		return 'B';
	else if (percent >= 70)
		return 'C';
	else if (percent >= 60)
		return 'D';
	else
		return 'F';
}
Are you free to use vectors? If you not, then you'd have to continually call the realloc() function for every additional line (and keep could of how many students there are in a separate counter variable).
you'd have to continually call the realloc() function for every additional line
Calling realloc on memory allocated by new is illegal.

@OP what exactly is your problem? If you have solution working with static arrays, direct transition to dynamic should be elementary.
Keene: Are you free to use vectors? If you not, then you'd have to continually call the realloc() function for every additional line (and keep could of how many students there are in a separate counter variable).

Vectors aren't covered until chapter 16... I don't think my professor would like it if I did use vectors...


Also, I'm confused on to use realloc() on dynamic arrays... Can you give me an example?
As MiiNiPaa said, mixing new with realloc is illegal, so scratch that. But looking at your code now, what is the purpose for even requiring arrays?

Also, check out this link: http://www.cplusplus.com/doc/tutorial/dynamic/
I was assigned this assignment with out any background to it. Seriously, this is the start of a new semester for me and I'm using a different book and classroom to make this for. I dusted off my newbie skills and made this from scratch, but it doesn't work and leaves a an empty outFile.

I have no clue what I'm doing all I have for instructions is "Redo Programming Exercise 6 of Chapter 8 using dynamic arrays."

That's it. That's all I got. I tried to assign two new char arrays to the files and compare the test's right answers to one student's answers, but I've never done that before and I'm stuck hurting my brain because I keep hitting a brick wall. I made this code completely by scratch and it just doesn't work. When I debug it, All I get is "File Found" and then it freezes with an empty outfile. This is all that is on the file.

TFFTFFTTTTFFTFTFTFTT
ABC54301 TFTFTFTT TFTFTFFTTFT

I'm stuck with faulty code, my assignment is due tomorrow at midnight, and I'm sick with a very bad cold and an enormous headache. I'm not gonna lie, I'm in pain and frustration.

I can see why it's getting hung up.
for (i = 0; 1 < maxSpace; i++)
You used a magic number rather than your variable in your condition.
I see! That helped move it forward, I think. Now I have this error...

HEAP CORRUPTION DETECTED: after Normal block #175 (or #168) at 0x0075B1F8

CRT detected that the application wrote to memory after end of heap buffer.

I don't know what that means. I'll google it just to make sure. The good news is I choose to ignore the error, I get this on the output.

Student: ABC54301
Answers: TFTFTFTT TFTFTFFTTFT Test Score: 0%
Grade :F

We are on the right track, but the 0% concerns me. There aren't even right answers. Hmm...
HEAP CORRUPTION DETECTED
inFile.get(testAnswers[maxSpace]); Do you see problem with that line?

The good news is I choose to ignore the error
You should never ignore memory corruption, invalid access, etc. errors. They means that your program is not working and all output you get is there accidentally and you cannot be sure that it will stay that way on another PC, or even on second run of your program.
Last edited on
Oh no no, I didn't mean it like that. I was just happy to see progress is all.

And progress I got. Holy cow, I think that may have done it. The answers and score are there and everything. Thank you, friends. I can take this from here.
Topic archived. No new replies allowed.