Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have to add invalid answer try again to my C++ code. Can someone help me out? Here is my code so far. I need

I have to add "invalid answer try again" to my C++ code. Can someone help me out? Here is my code so far. I need to be able to allow the user to correct their answer if they choose wrong in multiple choice and for true/false. I have also included the assignment details to help clarify anything I missed.

Write a C++ program to allow the user to create a test bank of questions. The program should first ask the user how many questions he or she wishes to create. This quantity will be the first line in the test bank. The user should now be prompted for all information for each question, and then that question is written out to the test bank in the exact format specified in the Phase 2 Individual Project.

For each question, the following information should be gathered:

Question type: Multiple choice (MC) or True/False (TF)

Question value

Actual question

If the question is TF, then get answer

If the question is MC, then get the number of options followed by each option and finally the answer

***********This program should be robust and allow users who make mistakes the ability to correct them (exception handling).*************

This is where I'm stuck. The sentence in BOLD.

#include #include #include #include #include #include #include #include #include #include #include #include

using namespace std;

int main() { ofstream outFile; outFile.open ("testbank.txt"); // A text file 'testbank.txt' will be created or opened in the same directory of the .cpp file

int totalQuestion; string option, type, question, answer; int totalOptions;

try{

cout << "Welcome to Michael's Quiz. Enter total number of questions: "; cin >> totalQuestion;

outFile << totalQuestion << endl;

for (int i = 0; i < totalQuestion; ++i) { cout << " Question " << (i+1) <> type;

if(type == "TF") { cin.ignore();

cout << "Enter the question : "; getline(cin,question);

cout << "Enter the answer(True/False): "; cin >> answer;

outFile << type << endl; outFile << question << endl << answer << endl; }

else if(type == "MC") { cin.ignore();

cout << "Enter the question : "; getline(cin,question);

cout << "Enter the number of options for the question : "; cin >> totalOptions;

outFile << type << endl; outFile << question << endl;

cin.ignore(); /* This line should be out of the loop so that we don't have to press Enter twice */

for (int j = 0; j < totalOptions; ++j) { cout << "Enter option number " << (j+1) <<" : "; getline(cin , option); outFile << option << endl; }

cout << "Enter the correct answer : "; cin >> answer; outFile << answer << endl; }

else { throw "Wrong type of Question"; // Exception Handling }

} }

catch(const char* msg) // Catch Block to handle the exception { cerr << msg << endl; }

outFile.close(); return 0;

} /* output:

Enter total number of question: 4

Question 1 Enter question type(TF/MC): TF Enter question: There are 50 states in America? Enter number of options: 2 Enter option 1: True Enter option 2: False Enter answer(True/False): True

Question 2 Enter question type(TF/MC): MC Enter question: Donald Trump is the President of the United States? Enter number of options: 4 Enter option 1: A 1st Enter option 2: B 30th Enter option 3: C 40th Enter option 4: D 45th Enter answer(correct option): D

Question 3 Enter question type(TF/MC): TF Enter question: 1+2=3? Enter number of options: 2 Enter option 1: True Enter option 2: False Enter answer(True/False): True

Question 4 Enter question type(TF/MC): TF Enter question: The ocean is orange? Enter number of options: 2 Enter option 1: True Enter option 2: False Enter answer(True?False): False output.txt 4 TF There are 50 states in America? True MC Donald Trump is the ____ President of the United States of America? A 1st B 30th C 40th D 45th D TF 1+2=3? True TF The ocean is orange? False */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

4. Help trainees set challenging mastery or learning goals.

Answered: 1 week ago

Question

2. Enrolling employees in courses and programs.

Answered: 1 week ago