What am i doing wrong? It will Display the correct answers but does nOt give the students?

closed account (30D23TCk)
I am supposed to Write a program helps your instructor grade quizzes. The program will read a file containing a single student’s answers to a recently given quiz. It will compute the student’s grade on the test as a percentage and then print a report. The program should read the file answers.dat



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

//Formula prototypes
void readFile(string &, vector<string> &, int);
void readFileS(vector <string> , int);
double calcPct(string, vector<string>, int, vector<string>);
void printRpt(string, vector<string>, vector<string>, int, double);

int main()
{
	string name; // student name
	double pctScore = 0; // student score
	int NUMQS = 11; // number of questions
	vector<string> ans(11); // initializes vector of student answers
	vector<string> KEY = { "c++", "for", "if", "variable", "function", "return", "array", "void", "reference", "main", "prototype" }; // answer key
	readFile(name, ans, NUMQS); //calls function readFile
	readFileS(KEY, NUMQS);
	pctScore = calcPct(name, ans, NUMQS, KEY); //calls calcPct function
	printRpt(name, ans, KEY, NUMQS, pctScore); //calls printRpt function
	system("pause");
	return 0;
}


void readFile(string &studentName, vector<string> &studentAnswers, int NUMQUESTIONS)
{
	ifstream inputFile;

	inputFile.open("Answers.dat");
	for (int i = 0; i < NUMQUESTIONS; i++) // For loop writes student's answers on to the studentAnswers vector
		inputFile >> studentAnswers[i];
	inputFile.close();
}
	void readFileS(vector<string> ANSKEY, int NUMQUESTIONS)
{
		ifstream inputFileS;

		inputFileS.open("Answers.txt");
	for (int i = 0; i < NUMQUESTIONS; i++) // For loop writes student's answers on to the studentAnswers vector
		inputFileS >> ANSKEY[i];
}


double calcPct(string studentName, vector<string> studentAnswers, int NUMQUESTIONS, vector<string> ANSKEY)
{
	int correctCounter = 11;
	double score;

	for (int i = 0; i < NUMQUESTIONS; i++)
	{
		if (studentAnswers[i] == ANSKEY[i])
			correctCounter ++;
	}

	score = (correctCounter / NUMQUESTIONS) * 100;

	return score;
}

void printRpt(string studentName, vector<string> studentAnswers, vector<string> ANSKEY, int NUMQUESTIONS, double score)
{
	cout << setw(20) << "CORRECT ANS" << setw(20) << "STUDENT ANS\n";

	for (int i = 0; i < NUMQUESTIONS; i++)
	{
		cout << setw(20) << ANSKEY[i] << setw(20) << studentAnswers[i];
		if (ANSKEY[i] != studentAnswers[i])
			cout << setw(15) << "INCORRECT";
		cout << endl;
	}
	cout << "QUIZ SCORE" << endl;
	cout << score; 
}
Last edited on
Topic archived. No new replies allowed.