Quiz Program
Oct 23, 2016 at 8:35pm UTC
Hey All,
I created my first program to ask a user to write questions and answers and output it to a text file to give as a test to students. I have everything working but my instructor made a suggestion to use a switch instead of an If statement. I was wondering if you all would mind taking a look at my code, everything works great but I think it could use some streamlining. Thanks all!
Lou
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("TestInput.txt" );
int numberQuestions, numberPoints, numberChoices;
string questionType, question, answerChoice, correctAnswer;
cout << "How many questions are on the test? " ;
cin >> numberQuestions;
outfile << numberQuestions << endl;
for (int i = 1; i <= numberQuestions; i++) {
cout << "What type of question is this? (TF or MC) " ;
cin >> questionType;
cin.ignore();
outfile << questionType << endl;
if (questionType == "TF" ) {
cout << "What is the point value for the question? " ;
cin >> numberPoints;
cin.ignore();
outfile << numberPoints << endl;
cout << "Enter your question here: " ;
getline (cin, question);
cout << "What is the corret answer? (T or F) " ;
cin >> correctAnswer;
outfile << correctAnswer << endl;
}
else
if (questionType == "MC" ) {
cout << "What is the point value for the question? " ;
cin >> numberPoints;
cin.ignore();
outfile << numberPoints << endl;
cout << "Enter your question here: " ;
getline (cin, question);
cout << "Press ENTER to continue...." ;
cin.ignore();
outfile << question << endl;
cout << "How many choices will the question have? " ;
cin >> numberChoices;
cout << "Press ENTER to continue...." ;
cin.ignore();
outfile << numberChoices << endl;
cin.ignore();
for (int i=0; i < numberChoices; i++) {
cout << "Please enter an answer choice: " ;
cin >> answerChoice;
cin.ignore();
outfile << answerChoice << endl;
}
cout << "What is the correct answer? (Letter) " ;
cin >> correctAnswer;
outfile << correctAnswer << endl;
}
}
return 0;
}
Topic archived. No new replies allowed.