Blank output screen

I have to write a program (description in code comments). I've debugged it but the output screen is blank. Can anyone please point out what I might be doing wrong? I don't think my vector usage is correct.

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
/*This program reads the file answers.dat which contains quiz answers for a one student.  The answers are compared to the answer key.  
The incorrect answers are flagged and the quiz score is calculated.  Report is printed*/

#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()
{
	// Saves correct quiz answers into vector
	const int NUMQS = 11; // number of questions
	// answer key vector
	vector<const string> KEY = { "c++", "for", "if", "variable", "function", "return", "array", "void", "reference", "main", "prototype" };
	vector<string> ans(11); // initializes vector to hold student answers
	string name; // student name
	double pctScore = 0;

	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;
}

//**************************************************************************
// Definition of readFile.												   *
// Reads answer.dat file containing student's answer into the ans vector.  *
//**************************************************************************
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];
}

//**************************************************************************
// Definition of calcPct.												   *
// Calculates percentage scored on quiz.                                   *
//**************************************************************************
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;
}

//**************************************************************************
// Definition of printRpt.												   *
// Prints report of student name, student's answers, answer key, incorrect *
// answers and percentage scored.                                          *
//**************************************************************************
void printRpt(string studentName, vector<string> studentAnswers, vector<const string> ANSKEY, const int NUMQUESTIONS, double score)
{
	cout << "!!! Marie Dominguez' Most Excellent Quiz Reporter!!!\n\n";
	cout << "Report for " << studentName;
	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;
}


Thanks!
I've continued working on the code and made a few adjustments. Now I get some output if I hit the enter key on the output screen (but the output is supposed to come up automatically). However, the first line of the .dat file isn't being read into the variable name.

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
/*This program reads the file answers.dat which contains quiz answers for a one student.  The answers are compared to the answer key.  
The incorrect answers are flagged and the quiz score is calculated.*/

#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;
}

//**************************************************************************
// Definition of readFile.												   *
// Reads answer.dat file containing student's answer into the ans vector.  *
//**************************************************************************
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];
}

//**************************************************************************
// Definition of calcPct.												   *
// Calculates percentage scored on quiz.                                   *
//**************************************************************************
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;
}

//**************************************************************************
// Definition of printRpt.												   *
// Prints report of student name, student's answers, answer key, incorrect *
// answers and percentage scored.                                          *
//**************************************************************************
void printRpt(string studentName, vector<string> studentAnswers, vector<const string> ANSKEY, const int NUMQUESTIONS, double score)
{
	cout << "!!! Marie Dominguez' Most Excellent Quiz Reporter!!!\n\n";
	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;
}
closed account (48T7M4Gy)
Maybe you could include a sample of a couple of lines of your data file?

Also, one way of debugging is to put a line in cout << "got this far"; and move this line through the program progressively from the start. That way you get some output and an indication where the problem is.

My guess is the data file isn't being opened - around line 42. Also print out the input if you can.
Thank you.

The major issue was the getline part. I got it all working now. :)
Hi,

The names of the parameters of the readFile function aren't the same as the variable names in main, so none of that information is in main's scope (not visible there).

As kemort alluded, if you open a file - check to see that it worked. Reading from a file should be enclosed in a while loop with the name of the stream as the condition. You should also close files when you are finished with them.

With the function declarations (prototypes) give the parameters names so they are the same as the function definition.

For the calc and print functions, all the parameters should be const. STL containers such as string and vector should be passed by reference. That applies to anything that is not a basic built in type, unless you wish to change it's value in an outer scope.

Line 59 does integer division, so that will probably be zero. One of correctCounter or NUMQUESTIONS should be cast to double:

double NumQuestionsAsDbl = static_cast<double>(NUMQUESTIONS);

The usual declaration for a container you don't want to change: const vector< string> rather than vector<const string> . The latter allows extra items to be inserted into the vector, which isn't ideal.

correctCounter += 1; can be written correctCounter++;

Good Luck !!
The names of the parameters of the readFile function aren't the same as the variable names in main, so none of that information is in main's scope (not visible there).


The first two parameters are passed by reference?
Last edited on
The first two parameters are passed by reference?


Of course, my bad. I guess I am so used to naming the parameters the same as the variables in the outer scope - just to reinforce the idea that they are the same.
Topic archived. No new replies allowed.