C++ I need help with my multiple choice loop. My program loops through like it s
ID: 3901959 • Letter: C
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 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 <iostream>
#include <fstream>
#include <string>
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 <y/n>? ";
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 <y/n>? ";
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!!!
Explanation / Answer
Using arrays, we can store all the options entered by user and only after confirmation, write it to file.
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class TestBank
{
public:
void TrueFalse(ofstream&);
void MultipleChoice(ofstream&);
void PrintTest(ifstream& );
};
int main()
{
TestBank bankObj;
ofstream test;
int testNumber, typeOfQuestion;
char yesOrNo;
// Create test bank file.
string filename = "C:\temp\testBankFile.txt";
test.open(filename.c_str());
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();
cout << endl << endl << "==================== Test Questions ====================" << endl;
ifstream in(filename.c_str());
bankObj.PrintTest(in);
in.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 <y/n>? ";
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 options[10];
int numOptions;
char op = 'A', correct;
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? ";
cin >> numOptions;
cin.ignore();
op = 'A';
for(int i = 1; i <= numOptions; i++, op++)
{
cout << "Enter option " << op << ": ";
getline(cin, options[i-1]);
}
cout << "Enter the correct option: ";
cin >> correct;
cout << "Would like to make any correction <y/n>? ";
cin >> yn;
} while (yn == 'y');
theFile << points << endl;
theFile << question << endl << numOptions << endl;
for(int i = 0; i < numOptions; i++)
theFile << options[i] << endl;
theFile << correct << endl;
}
void TestBank::PrintTest(ifstream& in)
{
int numQs;
int numOptions;
int points;
string question, option, ans;
string type;
in >> numQs;
for(int i = 1; i <= numQs; i++)
{
in >> type >> points;
in.ignore();
getline(in, question);
cout << question << endl;
if(type == "TF")
{
getline(in, ans);
cout << ans << endl;
}
else
{
in >> numOptions;
in.ignore();
char op = 'A';
for(int j = 1; j <= numOptions; j++, op++)
{
getline(in, option);
cout << op << ". " << option << endl;
}
getline(in, ans);
cout << "Answer: " << ans << endl;
}
cout << endl;
}
}
output
=====
How many questions would you like to create? 3
What type of question will #1 be? 1=T/F or 2=multiple choice: 1
How many points does this question worth? Enter: 5
Please enter the question: There exists birds that cannot fly?
Please enter the answer True or False ? true
Would like to make any correction <y/n>? n
What type of question will #2 be? 1=T/F or 2=multiple choice: 2
How many points does this question worth? Enter: 10
Please enter the question: Who was the President of the USA in 1991?
How many options? 6
Enter option A: Richard Nixon
Enter option B: Gerald Ford
Enter option C: Jimmy Carter
Enter option D: Ronald Reagan
Enter option E: George Bush Sr.
Enter option F: Bill Clinton
Enter the correct option: E
What type of question will #3 be? 1=T/F or 2=multiple choice: 1
How many points does this question worth? Enter: 10
Please enter the question: The city of Boston hosted the 2004 Summer Olympics?
Please enter the answer True or False ? false
Would like to make any correction <y/n>? n
==================== Test Questions ====================
There exists birds that cannot fly?
true
Who was the President of the USA in 1991?
A. Richard Nixon
B. Gerald Ford
C. Jimmy Carter
D. Ronald Reagan
E. George Bush Sr.
F. Bill Clinton
Answer: E
The city of Boston hosted the 2004 Summer Olympics?
false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.