getting error C4700: uninitialized local variable

My issue is i am getting error C4700: uninitialized local variable 'response' used on line 26. I know it's probably something simple I'm completely missing please help.

Here's my instructions:

The local Driver's License Office has asked you to write a program which grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. The correct answers are:

1. A 2. D 3. B 4. B 5. C 6. B 7. A 8. B 9. C 10. D 11. A 12. C 13. D 14. B 15. D 16. C 17. C 18. A 19. D 20. B

In main, declare an array and initialize it with the above correct answers. Also declare a second array for an exam taker's answers, and get a text file name from the user.

This program will have two functions:

1) have the user enter 20 answers from an exam taker and write the answers to the text file (validate that the answers are A, B, C, or D). See the 20 exam taker's answers below.

Hint: open (and close) the file in the function.

2) read the text file of the exam taker's answers and store them in the exam taker's array, which was declared in main.

Then in main, the program should display the following information:

-whether the exam taker passed or failed (15 of the 20 answers must be correct).

-total number of questions answered correctly

-total number of questions answered incorrectly

-a list of the numbers of the questions answered incorrectly (question numbers are 1-20).

Arrays must be processed using loops.

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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void studentsTest(string fileName, char student_answers[], int response, int QUESTION_AMT);
void checkAnswers(char answers[], char student_answers[], int QUESTION_AMT, int MIN_CORRECT);



int main()
{
	int response;
	const int QUESTION_AMT = 20;
	const int MIN_CORRECT = 15;
	char answers[QUESTION_AMT] = { 'A', 'D', 'B', 'B', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'C', 'D', 'B', 'D', 'C', 'C', 'A', 'D', 'B' };
	char student_answers[QUESTION_AMT];


	string fileName;
	ofstream textOut;


	studentsTest(fileName, student_answers, response, QUESTION_AMT); /*I keep getting error C4700: uninitialized local variable 'responce' used*/

    checkAnswers( answers, student_answers, QUESTION_AMT, MIN_CORRECT);


	cout << endl << endl;
	system("pause");
	return (0);
}
void studentsTest(string fileName, char student_answers[], int response, int QUESTION_AMT)
{
	cout << "\nPlease enter a name for your file>";
	getline(cin, fileName);

	ofstream textOut;
	textOut.open(fileName);

	for (int response = 0; response < QUESTION_AMT; response++)
	{
		cout << "Please enter your answers: " << (response + 1) << ": ";
		cin >> student_answers[response];

		while (student_answers[response] != 'A' && student_answers[response] != 'B' && student_answers[response] != 'C' && student_answers[response] != 'D')
		{
			cout << "\nError! Answers must be in captial letters! ex: A, B, C";

			cout << "Please enter your answers: " << (response + 1) << ": ";
			cin >> student_answers[response];
		}
	}
	ifstream textIn;
	textIn.open(fileName);
	if (textIn.fail())
	{
		cout << "\nText file not found...program will close";
		cout << endl << endl;
		system("pause");
	}
	textIn.close();
}
void checkAnswers(char answers[], char student_answers[], int QUESTION_AMT, int MIN_CORRECT)
{
	int correctAnswers = 0;

	for (int i = 0; i < QUESTION_AMT; i++)
	{
		if (answers[i] == student_answers[i])
			correctAnswers++;
	}
	cout << "\nStudent must have at least 15 correct answers to pass.";
	if (correctAnswers >= MIN_CORRECT)
	{
		cout << "\nYou passed the exam!";
	}
	else
	{
		cout << "\nYou failed the exam!";
	}

	cout << "\nThe following is the list of questions that are incorrect";
	for (int i = 0; i < QUESTION_AMT; i++)
	{
		if (answers[i] != student_answers[i])
			cout << "Question # " << i << " is incorrect! " << endl;
	}

	cout << "\nCorrect Answers = " << correctAnswers << endl;
	cout << "\nIncorrect Answers = " << QUESTION_AMT - correctAnswers << endl;
}
Last edited on
You have not initialized the response variable in main (i.e. you have not given it a value). Before you can use the variable (e.g. pass it to a function) safely you need to initialize it to some value. This is what the compiler is complaining about.

Note that inside the studentsTest function you never actually use the third parameter. It's using a variable named response inside the loop but that is a different variable that only exists within the loop. To me it looks like you don't really need the response variable in main and response parameter of the studentsTest function at all.
Last edited on
Awesome thank you for the help! :)
Topic archived. No new replies allowed.