Question

Write a C++ program that will deliver a multiple choice quiz. The program should Read questions...

Write a C++ program that will deliver a multiple choice quiz.

The program should

  • Read questions from the file questions.txt
  • Read the answer key for each test question from the file answers.txt
  • When the program runs it will present each question and obtain a response
  • After all questions have been answered, the program should output a report with the question number, the response given and either “correct” or “incorrect”. Show the output on the console and store the report in a file named studentResults.txt
  • Re-prompt for questions that are not answered A, B, C or D

questions.txt content:

1 The chemical reference for water is: A) H0 B) H2O C) CO2 D) W2C
2 Which ocean boarders New York: A) Pacific B) Baltic C) Atlantic D) Mediterranean
3 Which bridge is not in California: A) Golden Gate B) Dumbarton C) Brooklyn D) Carquinez
4 The highest hand in poker is A) Straight Flush B) Four of a kind C) Full House D) Royal Flush
5 The game of Baseball has A) 9 innings B) 4 quarters C) 6 sets D) 2 periods
6 The most populated city in the USA is A) Los Angeles B) San Francisco C) New York City D) Chicago
7 The world city with the greatest population is A) Tokyo B) Delhi C) Guangzhou D) Karachi
8 The longest river in the world is A) Mississippi B) Nile C) Lena D) Yangtze

answerKey.txt contains:

1 B
2 A
3 C
4 D
5 A
6 C
7 A
8 B

Example of report when the following responses B,A,C,D,B,C,A,C are entered
1 answer B correct
2 answer A correct
3 answer C correct
4 answer D correct
5 answer B incorrect
6 answer C correct
7 answer A correct
8 answer C incorrect
Your missed 2 questions

Test your program using the following responses to questions 1-8
A
B
C
D
X
A
B
C
D

Submit files: screenCapture.png, studentResults.txt, and quizProgram.cpp to Canvas

Expectations:

Your program will use at least three or more functions
Your program will have a prototype statement for each function

Suggestions:

Use functions names like getQuestion getAnswer writeResults getUserInput, openFiles and closeFiles.

You can use a string data type for questions (don't forget the header file
needed for using strings). It might also be useful for you to do a stub program to arrange your functions before you begin coding them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Ouck Launch ICtr+) CuuTestUsingFidnCpp- Microsoft Visual Studio View Pmjert ld Dehun Team Tocls deepthiongarattu D File Feit--------------------------------------------------

Program

//Header filesforI/O ,file operations,string read and storage
#include <iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
//Function prototypes
void readQuestionsAndanswers(ifstream&, ifstream&,vector<string>&,vector<char>&);
void writeReport(ofstream&,vector<char>,vector<char>);
void askQuestions(vector<string>, vector<char>, vector<char>&);
int main()
{
   //Store file reads and user responses
   vector<string> questions;
   vector<char> answers;
   vector<char> responses;
   //File for questions
   ifstream inQuestion("questions.txt");
   //File for answers
   ifstream inAnswer("answerKey.txt");
   //Call function to read input files
   readQuestionsAndanswers(inQuestion, inAnswer, questions, answers);
   //Call function to ask questions to user
   askQuestions(questions, answers, responses);
   //Output result storage file name
   ofstream out("studentsResult.txt");
   //Call function for report
   writeReport(out, answers, responses);
}
//Function to get questions and its answers from the files
void readQuestionsAndanswers(ifstream& inQuestion,ifstream& inAnswer,vector<string>& questions, vector<char>& answers) {
   //variable for each line read
   string line = "";
   char resp;
   int qNum;
   //Error check in file open
   if (!inQuestion || !inAnswer) {
       cout << "File not open!!!" << endl;
       exit(0);
   }
   //Loop until end of the file ad store into vector
   while (getline(inQuestion, line)) {
       questions.push_back(line);
       inAnswer >> qNum >> resp;
       answers.push_back(resp);
   }
   //Close input files
   inQuestion.close();
   inAnswer.close();
}
//Result Function to display and write into file
void writeReport(ofstream& out, vector<char> answers, vector<char> responses) {
   //variable for incorrect
   int inCorrect = 0;
   //Compare ansers and display result
   for (int i = 0; i < responses.size(); i++) {
       if (responses.at(i) == answers.at(i)) {
           cout << (i + 1) << " answer " << responses.at(i) << " correct" << endl;
           out << (i + 1) << " answer " << responses.at(i) << " correct" << endl;
       }
       else {
           cout << (i + 1) << " answer " << responses.at(i) << " incorrect" << endl;
           out << (i + 1) << " answer " << responses.at(i) << " incorrect" << endl;
           inCorrect++;
       }
   }
   cout << "Your missed " << inCorrect << " questions" << endl;
   out << "Your missed " << inCorrect << " questions" << endl;
   //close output file
   out.close();
}
//Function to ask questions and get answers from user
void askQuestions(vector<string> questions, vector<char> answers, vector<char>& responses) {
   //Variable for answer
   char ch;
   //Ask Questions
   for (int i = 0; i < questions.size(); i++) {
       cout << questions.at(i) << endl;
       cout << "Enter your answer: ";
       cin >> ch;
       ch = toupper(ch);
       while (ch != 'A' && ch != 'B' && ch != 'C' && ch != 'D') {
           cout << "Error!!!Response shoulb A,B,C or D.Re-enter!!!" << endl;
           cout << questions.at(i) << endl;
           cout << "Enter your answer: ";
           cin >> ch;
           ch = toupper(ch);
       }
       responses.push_back(ch);
   }
   cout << endl;
}

--------------------------------------------------

output

1 The chemical reference for water is: A) H0 B) H2O C) CO2 D) W2C
Enter your answer: A
2 Which ocean boarders New York: A) Pacific B) Baltic C) Atlantic D) Mediterranean
Enter your answer: B
3 Which bridge is not in California: A) Golden Gate B) Dumbarton C) Brooklyn D) Carquinez
Enter your answer: C
4 The highest hand in poker is A) Straight Flush B) Four of a kind C) Full House D) Royal Flush
Enter your answer: D
5 The game of Baseball has A) 9 innings B) 4 quarters C) 6 sets D) 2 periods
Enter your answer: X
Error!!!Response shoulb A,B,C or D.Re-enter!!!
5 The game of Baseball has A) 9 innings B) 4 quarters C) 6 sets D) 2 periods
Enter your answer: A
6 The most populated city in the USA is A) Los Angeles B) San Francisco C) New York City D) Chicago
Enter your answer: B
7 The world city with the greatest population is A) Tokyo B) Delhi C) Guangzhou D) Karachi
Enter your answer: C
8 The longest river in the world is A) Mississippi B) Nile C) Lena D) Yangtze
Enter your answer: D

1 answer A incorrect
2 answer B incorrect
3 answer C correct
4 answer D correct
5 answer A correct
6 answer B incorrect
7 answer C incorrect
8 answer D incorrect
Your missed 5 questions

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will deliver a multiple choice quiz. The program should Read questions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is subject of C++ Your program should read the file answers.dat. This file contains the...

    This is subject of C++ 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. The answers file has the following format: The student name is always the first line. A student name may or may not have last names. For example, both Elvis Presley and Cher took this class. A quiz always has 11 questions. There is one answer per line. Each answer is...

  • Using C++ programming Write a program that compares a student’s answers in a 5-question quiz to...

    Using C++ programming Write a program that compares a student’s answers in a 5-question quiz to the teacher’s answer key. The answer key is: {'C', 'D', 'B', 'B', 'A'} You MUST use TWO functions: One for input and the other for comparing/output. The output is simply the # of questions answered correctly. This program will test your ability to handle parallel arrays. Output: Look at the questions and choices on page 999 of your textbook. Enter the UPPERCASE letter of...

  • Math Problem Generator Your job will be to write a C++ program to teach math to...

    Math Problem Generator Your job will be to write a C++ program to teach math to school children. Your program will accomplish this by showing sets of math problems to the user and allowing them to enter an answer. The program should tell the user if they are correct and should continue providing additional questions. The program will keep track of correct and incorrect answers. The questions should use random numbers and should be simple enough math problems that someone...

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

  • Write a MIPS math quiz program in MARS. The program should start with a friendly user...

    Write a MIPS math quiz program in MARS. The program should start with a friendly user greeting. From there, it should generate a random arithmetic problem. Your program will need to generate three random things: the first and second operand and the operator to use. Your program should generate random positive integers no greater than 20 for the operands. The possible operators are +, -, * and / (division). The user should be prompted for an answer to the problem....

  • This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...

    This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program The first line is the number of questions (just throw this line away this week)...

  • Programming in C: Write a program that will help an elementary school student learn multiplication. Use...

    Programming in C: Write a program that will help an elementary school student learn multiplication. Use the random number generator to produce two positive integers between 1 and 12 inclusive. It should then type a question such as:             How much is 6 times 7? The student then types the answer. Your program checks the students’ answer. If it is correct, print a message such as Very good!   If the answer is wrong, print a message such as No. Please...

  • see below: You will be creating a personal quiz that will provide a grade depending on the responses given. The quiz should contain at least three questions of the true/false or multiple-choice natu...

    see below: You will be creating a personal quiz that will provide a grade depending on the responses given. The quiz should contain at least three questions of the true/false or multiple-choice nature. You should select only one type of question to use. These questions should originate from Comprehension Check in your textbook (noting the chapter and page number) These questions and the correct answer identifier should be stored in a text file. A second table should contain the possible...

  • Java: The local Driver's License Office has asked you to write a program that grades the...

    Java: The local Driver's License Office has asked you to write a program that grades the written portion of the driver's   license exam. The exam has 20 multiple choice questions.   Here are the correct answers:    1. B  6. A  11.B  16. C    2. D  7. B  12.C  17. C    3. A   8. A  13.D  18. B    4. A  9. C  14.A  19. D    5. C  10. D  15.D  20. A    Your program should store the correct answers in an array. (Store each question's answer in an element of a String array.) The program...

  • I need to rewrite this program using only RIO I/O functions on c 6 Write a...

    I need to rewrite this program using only RIO I/O functions on c 6 Write a C program that asks the user to enter the fil he all its permissions in a readable format.) Also, the program prints the file contents to the standard output if the file is of a regular type. Use only Unix V/O functions. Do not use the O package nor C standard I/O functions. Save your source file as "lab07a.c" and include it in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT