I need help with finding another way to involve the key, the way i have it now i cant

closed account (30D23TCk)
he 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.
Your program should read the file answers.dat. This file contains the name of the student who took the quiz, and his answers for each question.

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

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

int main()
{
	string name; // student name
	double pctScore = 0; // student score
	const int NUMQS = 11; // number of questions
	vector<string> ans(11); // initializes vector of student answers
	vector<const string> KEY = { "c++", "for", "if", "variable", "function", "return", "array", "void", "reference", "main", "prototype" }; // answer key
	readFile(name, ans, NUMQS); //calls function readFile
	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, const int NUMQUESTIONS)
{
	ifstream inputFile;
	inputFile.open("answers.dat"); // Opens input file
	getline(cin,studentName); // Writes student's name on to variable

	for (int i = 0; i < NUMQUESTIONS; i++) // For loop writes student's answers on to the studentAnswers vector
		inputFile >> studentAnswers[i];
}

double calcPct(string studentName, vector<string> studentAnswers, const int NUMQUESTIONS, vector<const string> ANSKEY)
{
	int correctCounter = 0;
	double score;
	
	for (int i = 0; i < NUMQUESTIONS; i++)
	{
		if (studentAnswers[i] == ANSKEY[i])
			correctCounter += 1;
	}

	score = (correctCounter / NUMQUESTIONS) * 100;
				
	return score;
}


void printRpt(string studentName, vector<string> studentAnswers, vector<const string> ANSKEY, const int NUMQUESTIONS, double score)
{
	cout << "Report for " << studentName << endl;
	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\n";
	cout << score;
}
Last edited on
Online 48 you are losing information. Dividing an integer by an integer always results in another integer. For example, 3 / 5 = 0.
Ok, I tested your code and to get rid of the errors take away all the consts in your vectors. All of them. Then the errors disappear.

Good luck!
Last edited on
Topic archived. No new replies allowed.