Must be done in C++ in this fashion #include <iostream> #include <string> #inclu
ID: 655408 • Letter: M
Question
Must be done in C++ in this fashion
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/*
This program qives a student a true/false quiz. Student enters 1 for true answer and 0 (zero) for the
false answer. Program stores all three: questions, answers, and student responses in three different arrays.
The questions are stored in a string array and read from a file. [One question per line].
The answers are stored in an int array and read from a file. In answers file 1 represent true and 0 false.
Both array have same length.
Student responses are recorded in another (third) int array.
The answer and student response arrays are compared. Student score is computed.
If student scores 60% or more the message is given that student passed. Otherwise
message is given that student failed.
*/
const int MAX = 10;
void openFile(ifstream & input, const string & message);
int fillQuestionsArray(string questions[], ifstream & inq);
int fillAnswersArray(char answers[], ifstream & inq);
void fillResponseArray(const string questions[], int response[], int lenQ);
int GradeResponses(const char answers[], const int response[], int len);
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len);
int main(){
cout << "Welcome to the drivers license quiz." << endl;
ifstream in;
openFile(in, "questions");
string questions[MAX] = {};
int lenQ = fillQuestionsArray(questions, in);
{
for (int i = 0; i < MAX; i++);
}
in.close();
openFile(in, "answers");
char answers[MAX];
int lenA = fillAnswersArray(answers, in);
if (lenQ != lenA){
cout << "Number of questions and answers are different. Exiting the program." << endl;
exit(0);
}
int response[MAX] = {};
fillResponseArray(questions, response, lenQ);
int score = 0;
score = GradeResponses(answers, response, lenQ);
printAnswersAndResponses(questions, answers, response, lenQ);
cout << "You scored " << score << " points, out of maximum " << lenQ << " points." << endl;
double percent = (double(score) / lenQ) * 100;
if (percent >= 60.0){
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "You passed the quiz." << endl;
}
else{
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "Sorry you failed the quiz." << endl;
}
in.close();
cout << "--------------------------------------------------------------------------------------------------------" << endl;
return 0;
}
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len){
cout << "*****************************************************" << endl;
cout << "Here are correct answers and your responses." << endl;
cout << "*****************************************************" << endl;
cout << setw(55) << left << "Question" << setfill(' ') << setw(18) << left << "Correct Answer" << setfill(' ') << left << setw(15) << "Your Response" << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i<len; i++){
cout << setw(55) << left << questions[i] << setfill(' ') << setw(18) << left << answers[i] << setfill(' ') << setw(15) << left << response[i] << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
int GradeResponses(const char answers[], const int response[], int len){
int score = 0;
for (int i = 0; i<len; i++){
if (answers[i] == response[i]){
score++;
}
}
return score;
}
void fillResponseArray(const string questions[], int response[], int lenQ){
cout << "Please answer the questions as asked. Questions are true or false. "
<< "Enter 1 for a true answer and 0 (zero) for a false answer." << endl;
for (int i = 0; i<lenQ; ++i){
cout << questions[i] << " : ";
cin >> response[i];
}
}
int fillAnswersArray(char answers[], ifstream & inA){
char ch = ' ';
int i = 0;
while ((ch = inA.peek()) != EOF && i<MAX){
inA >> answers[i];
i++;
}
return i;
}
int fillQuestionsArray(string questions[], ifstream & inq){
char ch = ' ';
int i = 0;
while ((ch = inq.peek()) != EOF && i<MAX){
getline(inq, questions[i]);
i++;
}
return i;
}
void openFile(ifstream & input, const string & message){
bool done = false;
string In_File = "";
//Loop to get the name of a file that truly exists
// and check that file is not empty.
while (!done){
input.clear();
cout << "Please input the name of the data file with " << message << " to be read : ";
getline(cin, In_File);
cout << "The file name entered is : " << In_File << endl;
input.open(In_File.c_str());
if (input.fail()){
cout << "The file " << In_File << " does not exist. ";
done = false;
}
else{
input.peek();//peek at the input file
if (input.eof()){//If file is empty then the function eof() returns true
cout << "The file has no data in it ";
done = false;
input.close();
}
else{
done = true;
}
}
}//end of while loop
cout << "File " << In_File << " opened and has data in it." << endl;
}
------------------------------------------------------------------------------------
Motor Vehicle Department Driver License Written Test
In this assignment we use C++ arrays to accomplish followings:
Deliver a driver license written test that contains multiple choice questions.
Collect user responses to driving test questions. User responses will be one of the letters: A, B, C, or D.
Grade the test.
Display user score.
Display all the questions, correct answer, and user answer to each.
Please understand that this document, test runs, demo I make in the class, verbal description of this assignment; all contain project specifications for you to meet. Please feel free to ask questions about any specifications that you are not clear about.
Data Files:
You are given two data files that you can download from D2L. File Questions.txt contains written test questions. The format of this file is given below in the figure 1.
Figure 1.
Notice the structure of file. For each question, first line gives the question number and text. Then there are four possible answers (A, B, C, and D). Each question and its answers are separated from the next question and answer by $ character. You will need to use this character when storing questions and possible answers in a questions array.
The answer keys are stored in the file AnswerKey.txt. It simply has letters A, B, C, or D as correct answers to the questions in question file. Figure 2 shows the picture of this file.
Figure 2.
For example the answer to question 1 in Figure 1 is the first letter (B) in Figure 2. Similarly, the answer to question 2 in Figure 1 is second letter (D) in Figure 2 and so on.
There are 20 questions in the questions file as well as 20 answers in the same order in answers key file. You will need to create an array of size 20 or more.
What Should Your Program Accomplish?
Your program will use user defined functions whose description will follow soon. Basically your program will open and read questions file and store questions in a string array, so that there is one question per array. The picture of this array in RAM will look like Figure 3 below. (This just a schematic picture).
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
Q2. When driving on a freeway entrance ramp, you should look for a gap in freeway traffic by:
A. Looking in the inside rearview mirror only
B. Looking in the sideview mirror only
C. Looking in both rearview and sideview mirrors
D. Looking in your mirrors and turning your head to look over your shoulder
questions[0]
questions[1]
Rest of array elements ->
Figure 3.
Then your program will open and read answers file and populate the answer key array which is a character (char) array. The picture of this array in RAM will look like Figure 4 below. (This is just schematic picture).
B
D
A
A
C
A
Ans[0]
Ans[1]
Ans[19]
Figure 4.
Then your program will present one question at a time (using a loop) to the user and collect user responses in a third array, which will also be a character (char) array. The schematic picture of response array will look just like figure 4 if user answered all questions correctly. Once all user responses are recorded in the response array, the program will compare the answer key array and response array and if answer in certain array location matches the same location in response array, the user gets one point. Otherwise user gets no point.
Program then prints the number of points user scored, and number of questions they missed. In the end the program displays one question at a time (with possible answers), the answer key, and the user response. If user had correct answer to that question then program prints that
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
Q2. When driving on a freeway entrance ramp, you should look for a gap in freeway traffic by:
A. Looking in the inside rearview mirror only
B. Looking in the sideview mirror only
C. Looking in both rearview and sideview mirrors
D. Looking in your mirrors and turning your head to look over your shoulder
Explanation / Answer
#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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.