Reading from file to arrays

I am having issues with a homework asignment. I am supposed to design a program that pulls 20 multiple choice questions from a file into my program. I am having issues with the comparison of the questions file and the answers file. what would be the best way to go about solving this problem??

questions.txt ex:
Q1. When driving through a work zone, it is a good safety practice to:
A. Drive close to the vehicle in front of you to keep traffic flowing freely

B. Lengthen your usual following distance - by double

C. Turn on your cruise control

D. Shorten your usual following distance - by about half
$

Answers.txt Ex:
d


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
void openFile(ifstream & input, const string & message)
{
	bool done = false;
	string In_File = "";
	while(!done)
	{
		input.clear();
		cout<<"Please Input the File Location: "<<endl;
		getline(cin, In_File);
		input.open(In_File.c_str());

		if(input.fail())
		{
			cout<<"The File "<<In_File<<"does NOT Exist"<<endl;
			done = false;
		}
		else
		{
			
			input.peek();
			if(input.eof())
			{
				cout<<"The File Was EMPTY"<<endl;
				done = false;
				input.close();
			}
			else
			{
				done = true;
			}
		}
	}//end while
	cout<<"The File "<<In_File<<"opened and contains data"<<endl;
}

int main(int argc, char* argv[])
{
	cout<<"Welcome to the Driving Test Program"<<endl;
	ifstream in;
	openFile(in, "questions");
	string questions[MAX] =  {};
	int lenQ = fillquestions(questions,in);
	in.close();
	
	openFile(in, "answers");
	bool answers[MAX];
	int lenA = fillanswers(answers,in);

	if(lenQ != lenA)
	{
		cout<<"Number of questions and answers are Different. Exiting the Program"<<endl;
		exit(0);
	}
	bool response[MAX] = {};
	fillresponse(questions, response, lenQ);
	int score = 0;
	score = grade(answers, response, lenQ);
	cout<<"You Scored "<<score<<"Points, out of a Maximum of "<<lenQ<<"Points"<<endl;
	double percent = (double(score)/lenQ)*100;
Can you post the full code?

Aceix.
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAX = 12;

void openFile(ifstream & input, const string & message);
int filltestQuest(string testQuest[], ifstream & inQ);
int fillansKey(bool ansKey[], ifstream & inA);
void fillstuAns(const string testQuest[], bool stuAns[], int lenQ);
int grade(const bool ansKey[], const bool stuAns[], int len);
void printansKeyAndstuAns(const string testQuest[], const bool ansKey[], const bool stuAns[], int len);

int main(int argc, char* argv[])
{
	cout<<"Welcome to the Driving Test Program"<<endl;
	ifstream in;
	openFile(in, "testQuest");
	string testQuest[MAX] =  {};
	int lenQ = filltestQuest(testQuest,in);
	in.close();
	openFile(in, "ansKey");
	bool ansKey[MAX];
	int lenA = fillansKey(ansKey,in);

	if(lenQ != lenA)
	{
		cout<<"Number of testQuest and ansKey are Different. Exiting the Program"<<endl;
		exit(0);
	}
	bool stuAns[MAX] = {};
	fillstuAns(testQuest, stuAns, lenQ);
	int score = 0;
	score = grade(ansKey, stuAns, lenQ);
	cout<<"You Scored "<<score<<"Points, out of a Maximum of "<<lenQ<<"Points"<<endl;
	double percent = (double(score)/lenQ)*100;

	if (percent > 60)
	{
		cout<<"You Scored "<<percent<<"% in the Quiz"<<endl;
		cout<<"You Passed the Quiz"<<endl;
	}
	else
	{
		cout<<"You Scored "<<percent<<"% in the Quiz"<<endl;
		cout<<"You Failed the Quiz. Please Try Again"<<endl;
	}
	in.close();
	cout<<"--------------------------------------------------------------------------------"<<endl;
	return 0;
}

void openFile(ifstream & input, const string & message) //Does not Register Multiple Answer Test due to Array lengths not the same. Possible sentinel value needing to be inserted.
{
	bool done = false;
	string In_File = "";
	while(!done)
	{
		input.clear();
		cout<<"Please Input the File Location: "<<endl;
		getline(cin, In_File);
		input.open(In_File.c_str());

		if(input.fail())
		{
			cout<<"The File "<<In_File<<"does NOT Exist"<<endl;
			done = false;
		}
		else
		{
			
			input.peek();
			if(input.eof())
			{
				cout<<"The File Was EMPTY"<<endl;
				done = false;
				input.close();
			}
			else
			{
				done = true;
			}
		}
	}//end while
	cout<<"The File "<<In_File<<"opened and contains data"<<endl;
}

int filltestQuest(string testQuest[], ifstream & inQ)
{
	char ch = ' ';
	int i = 0;

	while((ch = inQ.peek()) != EOF && i < MAX)
	{
		getline(inQ, testQuest[i], '$');
		i++;
	}

	return i;
}

int fillansKey(bool ansKey[], ifstream & inA)
{
	char ch = ' ';
	int i = 0;

	while((ch = inA.peek()) != EOF && i < MAX)
	{
		inA>>ansKey[i];
		i++;
	}
	
	return i;
}

void fillstuAns(const string testQuest[], bool stuAns[], int lenQ)
{
	cout<<"Enter Test Questions with Cooresponding Character"<<endl;
	for(int i = 0; i <lenQ; i++)
	{
		cout<<testQuest[i];//does not allow user inputted answer after the posting of the question
		cin>>stuAns[i];
	}
}

int grade(const bool ansKey[], const bool stuAns[], int len)
{
	int score = 0;
	for(int i = 0; i<len; i++)
	{
		if(ansKey[i] == stuAns[i])
		{
			score++;
		}
	}
	
	return score;
}

void printansKeyAndstuAns(const string testQuest[], const bool ansKey[], const bool stuAns[], int len)
{
	cout<<"*******************************************************"<<endl;
	cout<<"Here are the Correct ansKey and Your Inputted ansKey"<<endl;
	cout<<"*******************************************************"<<endl;

	cout<<boolalpha;
	cout<<setw(55)<<left<<"testQuest"<<setfill(' ')<<setw(18)<<left<<"Correct ansKey"<<setfill(' ')<<setw(18)<<left<<"Your ansKey"<<setfill(' ')<<endl;
	cout<<"------------------------------------------------------------------------------------------------------------------------"<<endl;

	for(int i = 0; i<len; i++)
	{
		cout<<setw(55)<<left<<testQuest[i]<<setfill(' ')<<setw(18)<<left<<stuAns[i]<<setfill(' ')<<setw(18)<<left<<ansKey[i]<<setfill(' ')<<endl;
		cout<<"------------------------------------------------------------------------------------------------------------------------"<<endl;
	}
	cout<<"------------------------------------------------------------------------------------------------------------------------"<<endl;
}
Topic archived. No new replies allowed.