Question: C++ I need help with my multiple choice loop. My program loops through like it should asking the choices but when it stores it in
C++ I need help with my multiple choice loop. My program loops through like it should asking the choices but when it stores it in the file instead of storing all the choices in the order it only stores the last one. If I chose multiple choice then chose 3 options it should store all of them in the file in the order it was enter not just one. Please help me.
My code:
#include "stdafx.h"
#include
#include
#include
using namespace std;
class TestBank
{
public:
void TrueFalse(ofstream&);
void MultipleChoice(ofstream&);
};
int main()
{
TestBank bankObj;
ofstream test;
int testNumber, typeOfQuestion;
char yesOrNo;
// Create test bank file.
test.open("C:\\temp\\testBankFile.txt");
if (test.is_open())
{
cout << "How many questions would you like to create? ";
cin >> testNumber;
test << testNumber << endl;
int nextNum = 1;
//create the test bank
for (int i = testNumber; i > 0; --i)
{
cout << endl << "What type of question will #" << nextNum++
<< " be? 1=T/F or 2=multiple choice: ";
cin >> typeOfQuestion;
if (typeOfQuestion == 1)
test << "TF ";
else if (typeOfQuestion == 2)
test << "MC ";
else
cout << "Invalid option" << endl;
switch (typeOfQuestion)
{
case 1: bankObj.TrueFalse(test);
break;
case 2: bankObj.MultipleChoice(test);
}
}
test.close();
}
else
{
cout << "Unable to create test bank";
}
system("pause");
return 0;
}
// function to create true/false question
void TestBank::TrueFalse(ofstream& thefile)
{
int points;
string question;
string answer;
char yn;
do
{
cout << " How many points does this question worth? Enter: ";
cin >> points;
cin.ignore();
cout << " Please enter the question: ";
getline(cin, question);
cout << " Please enter the answer True or False ? ";
cin >> answer;
cin.ignore();
cout << "Would like to make any correction
cin >> yn;
} while (yn == 'y');
//Write all information into the file
thefile << points << endl;
thefile << question << endl;
thefile << answer << endl;
}
//Now you have to complete function MultipleChoice()
// function to create multiple choice question
void TestBank::MultipleChoice(ofstream& theFile)
{
int points;
string question;
string answer;
string choice;
int OP;
char yn;
do
{
cout << " How many points does this question worth? Enter: ";
cin >> points;
cin.ignore();
cout << " Please enter the question: ";
getline(cin, question);
cout << " How many options would you like? ";
cin >> OP;
cin.ignore();
for (int i = OP; i > 0; --i)
{
cout << " Please enter the choice: ";
getline(cin, choice);
}
cout << " Please enter the answer (A-E): ";
cin >> answer;
cin.ignore();
cout << "Would like to make any correction
cin >> yn;
} while (yn == 'y');
//Write all information into the file
theFile << points << endl;
theFile << question << endl;
theFile << OP << endl;
theFile << choice << endl;
theFile << answer << endl;
}
All the inputs are sent to the file after the program asks the user if they want to make corrections. If no then it sends off the data to the file. I need all the choices sent not just the last one entered. Please help!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
