Help with Arrays - Need Help with inFiles & outFiles

im having trouble running the program, every time i opens it just closes can some please help me!!

Question

the Computer Science department 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 the answers to the test in the form:

TFFTFFTTTTTTFFTFTFTT


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 -1 point and no answer gets 0 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.

Test your program with the following input file:(what is inside the file)

TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
LKJ81672 TTTFTFFTFFTTFTFTF
STD24151 TFTFTTTFTFTFFTTF





#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include <cmath>

using namespace std;

void readDataFromFile(ifstream &, char[], string[], string[]);
void calculateScore(char[], string[], double[]);
void calculateGrades(double[], char[]);

const int MAX_ANSWERS = 20;
const int MAX_STUDENTS = 150;
const double MAX_MARKS = 40.0;
int studentsCounter = 0;


int main()
{
ifstream inFile;
string fileName;
char testAnswers[MAX_ANSWERS];
string studentID[MAX_STUDENTS];
string studentAnswers[MAX_STUDENTS];
double studentScores[MAX_STUDENTS];
char studentGrades[MAX_STUDENTS];

cout << "Enter the file name:";
cin >> fileName;

inFile.open("studentdata.txt");
if (!inFile)
{
cout << "The file" << fileName
<< "is not found." << endl;
system("pause");
return 1;

}

readDataFromFile(inFile, testAnswers, studentID, studentAnswers);
calculateScore(testAnswers, studentAnswers, studentScores);
calculateGrades(studentScores, studentGrades);

cout << "Results of the students." << endl;

cout << "Student_ID\tStudent_Answers\t\tScore\tGrade" << endl;

cout << "----------------------" << endl;
for (int student = 0; student < studentsCounter, student++)
{
cout << studentID[student] << "\t" << studentAnswers[student] << "\t" << studentScores[student] << "\t" << studentGrades[student] << endl;
}
inFile.close();
system("pause");
return 0;
}

void readDataFromFile(ifstream &inFile, char testAnswers[], string studentID[], string studentAnswers[])
{
char ch;
int index;
for (int i = 0; i < MAX_ANSWERS; i++)
inFile >> testAnswers[i];
index = 0;
inFile >> studentID[index];
while (inFile)
{
studentsCounter++;
inFile.get(ch);
getline(inFile, studentAnswers[index]);
index++;
inFile >> studentID[index];
}
}

void calculateScores(char testAnswers[], string studentAnswers[], double studentScores[])
{
string answers;
double sum;
for (int i = 0; i < studentsCounter, i++)
{
answers = studentAnswers[i];
sum = 0;
for (int j = 0; j < answers.length(); j++)
{
if (answers[j] == ' ')
sum = sum + 0;
else if (answers[j] == testAnswers[j])
sum = sum + 2;
else
sum = sum - 1;
}
studentScores[i] = sum;
}
}

void calculateGrades(double studentScores[], char studentGrades[])
{
double percentage;
for (int i = 0; i < studentsCounter; i++)
{
percentage = (studentScores[i] / MAX_MARKS) * 100;
if (percentage >= 90)
studentGrades[i] = 'A';
else if (percentage >= 80)
studentGrades[i] = 'B';
else if (percentage >= 70)
studentGrades[i] = 'C';
else if (percentage >= 60)
studentGrades[i] = 'D';
else
studentGrades[i] = 'F';
}
}
The problem is that you ask for a filename but try to open a different file.
1
2
3
cout << "Enter the file name:";
cin >> fileName;
inFile.open("studentdata.txt");

You also have two synthax errors:
1
2
for (int student = 0; student < studentsCounter, student++)
for (int i = 0; i < studentsCounter, i++)


After I fixed them I got the following output:
1
2
3
4
5
6
7
8
9
10
11
Enter the file name: Data.txt
Results of the students.
Student_ID      Student_Answers         Score   Grade

ABC54102        T FTFTFTTTFTTFTTF TF    27      D
DEF56278        TTFTFTTTFTFTFFTTFTTF    40      A
ABC42366        TTFTFTTTFTFTFFTTF       34      B
ABC42586        TTTTFTTT TFTFFFTF       26      D
LKJ81672        TTTFTFFTFFTTFTFTF       7       F
STD24151        TFTFTTTFTFTFFTTF        -1      F
Press any key to continue . . .
where for (int student = 0; student < studentsCounter, student++)
you need to put " : " after "studentsCounter"
so thomas1965 what would i change im not understanding what to change to open the file, and the synthax errors whats wrong with them??
Last edited on
i figured out the synthax error but im still having trouble opening the .txt file, and for line

cout << studentID[student] << "\t" << studentAnswers[student] << "\t" << studentScores[student] << "\t" << studentGrades[student] << endl;

for student it tells me its unidentified
what would i change im not understanding what to change to open the file
1
2
3
4
cout << "Enter the file name:";
cin >> fileName;

inFile.open(fileName);


The synthax errors fixed:
1
2
for (int student = 0; student < studentsCounter; student++)
for (int i = 0; i < studentsCounter; i++)
okay so how would i change this not understanding what to really change, i really dont understand whats wrong


1
2
3
4
5
	cout << "Enter the file name:";
	cin >> fileName;

	inFile.open("studentdata.txt");
	if (!inFile)
Last edited on
okay so how would i change this not understanding what to really change, i really dont understand whats wrong

You really should look at the code.

1
2
3
4
5
	cout << "Enter the file name:";
	cin >> fileName;

	inFile.open("studentdata.txt"  fileName);
	if (!inFile)
just to delete second line solve this
Topic archived. No new replies allowed.