Here is my quiz program. It is a console program, and I used the classic way of using variables. One day I may upgrade it to object oriented programming.
Here is the filetestquestions.txt
3
TF
5
There exist birds that cannot fly?
true
MC
10
Who was the President of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
TF
10
The city of Boston hosted the 2004 Summer Olympics?
false
C++ code, main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sstream>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <cctype>
#include <algorithm>
#include <locale>
using namespace std;
string StringToUpper(string strToConvert)
{
std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(), ::toupper);
return strToConvert;
}
// Determines if the string is a number
bool isNumeric(string pszInput);
int main () {
string loadanexam;
int numberOfQuestions[1];
int count=0;//set number of questions
int q=0; //question number
int TFcount=0;
string TFquestion[1024];
string filename;
int points[1024]; //use in TF and MC
string TFanswer[1024];
int MCcount=0;
string MCquestion[1024];
int howmanyanswers[1024];
string answers[50][50]; //5 answers to choose from A - F, lowered numbers because of memory error
int MCanswers=0;
string MCcorrectAnswers[1024];
int a=0; //answer array number
double userPoints=0; //use in TF and MC
double userTotalPoints=0; //use in TF and MC
double percentCorrect=0; //use in TF and MC
string useranswer;
// Each line in the .txt file
string line;
int w=0;
do{
cout << "Load an Exam file (Y / N)?" << endl;
getline(cin, loadanexam);
cout<<" "<<endl;
loadanexam = StringToUpper(loadanexam);
if (loadanexam == "Y"){
// Open the file
cout << "Type the name of the file (case sensitive)?" << endl;
getline(cin, filename);
cout<<" "<<endl;
ifstream myfile (filename);
//ifstream myfile ("testquestions.txt");
if (myfile.is_open()) {
// While the file is good
while (myfile.good() ) {
// Get the current line in the file
getline (myfile, line);
// Verify that the line is an integer
if (isNumeric(line) && count==0) {
// Convert initial 'line' to an integer and calculate
numberOfQuestions[0]==atoi(line.c_str());
count++;
}else if (isNumeric(line) && TFcount == 1) {
// Convert 'line' to an integer and calculate
points[q] = atoi(line.c_str()); //convert string to int
cout<<"Points for this True/False Question: "<< points[q] <<endl;
userTotalPoints = userTotalPoints + points[q]; //use in TF and MC
TFcount++;
}else if (TFcount == 2) {
// TF question
TFquestion[q] = line;
cout<<"TF Question: "<< TFquestion[q] <<endl;
TFcount++;
}else if (TFcount == 3) {
// TF answer
int e=0;
do{
cout<<"What is your answer (T/F)?"<<endl;
getline(cin, useranswer);
useranswer = StringToUpper(useranswer);
TFanswer[q] = line;
if (useranswer == "F" ||useranswer == "FALSE"){
useranswer = "FALSE";
TFanswer[q] = StringToUpper(TFanswer[q]);
if(TFanswer[q] == useranswer){
cout<<"Correct!"<<endl;
userPoints = userPoints + points[q];
cout<<userPoints<<endl;
e=1;
}else{
cout<<"Wrong Answer. Try again next time."<<endl;
//userPoints[q] = 0;
e=1;
}
}
if (useranswer == "T" || useranswer == "TRUE"){
useranswer = "TRUE";
TFanswer[q] = StringToUpper(TFanswer[q]);
if(TFanswer[q] == useranswer){
cout<<"Correct!"<<endl;
userPoints = userPoints + points[q];
e=1;
}else{
cout<<"Wrong Answer. Try again next time."<<endl;
e=1;
}
}
if (useranswer != "F" && useranswer != "FALSE" && useranswer != "T" && useranswer != "TRUE"){
cout<<"Try again. Only T(TRUE) or F(FALSE) allowed."<<endl;
e=0;
}
} while (e<1);
cout<<"Answer: "<< TFanswer[q] <<endl;
cout<<" "<<endl;
TFcount=0;
q++;//add 1 to question number
//multiple choice
}else if (MCcount == 1) {
cout<< "\t\t Question Number # "<<q<< endl;
// MC points
points[q] = atoi(line.c_str()); //convert string to int
cout<<"MC Question Points: "<< points[q] <<endl;
userTotalPoints = userTotalPoints + points[q]; //use in TF and MC
MCcount++;
}else if (MCcount == 2) {
// MC question
MCquestion[q] = line;
cout<<" Mutiple Choice Question: "<< MCquestion[q] <<endl;
MCcount++;
}else if (MCcount == 3) {
// MC answers to choose from
howmanyanswers[q] = atoi(line.c_str()); //convert string to int
//cout<<"How many MC answers = "<< howmanyanswers[q] <<endl;
MCanswers++;
MCcount++;
}else if (MCanswers == 1 ) {
// MC answers to choose from multidimensional array
answers[q][a] =line; //convert string to int
//cout<< answers[q][a] <<"\t\t Question Number # "<<q<< "\t MC Answer Array # "<<a <<endl;
if (a==0){
cout<< "A."<<answers[q][a] <<endl;
}
if (a==1){
cout<< "B."<<answers[q][a] <<endl;
}
if (a==2){
cout<< "C."<<answers[q][a] <<endl;
}
if (a==3){
cout<< "D."<<answers[q][a] <<endl;
}
if (a==4){
cout<< "E."<<answers[q][a] <<endl;
}
if (a==5){
cout<< "F."<<answers[q][a] <<endl;
}
a++;
if (a == (howmanyanswers[q])){
MCanswers = 2;
a=0;
}
}else if (MCanswers == 2 ) {
MCanswers = 0;
MCcorrectAnswers[q] = line;
cout<<"What is your answer (A,B,C,D,E,F)?"<<endl;
getline(cin, useranswer);
int f=0;
do {
useranswer = StringToUpper(useranswer);
if (useranswer== MCcorrectAnswers[q]){
cout<<"Correct!"<<endl;
userPoints = userPoints + points[q];
f=1;
}else if (useranswer != MCcorrectAnswers[q]){
cout<<"Wrong Answer. Try again next time."<<endl;
f=1;
}else{
cout<<"Try again next time."<<endl;
f=0;
}
}while (f<1);
MCanswers=0;
MCcount=0;
q++;//add 1 to question numbe= going to the next question
//w=1;
}else{
//is a string
//cout<<line<<" string "<<endl;
if (line == "TF"){
cout<<"True False"<<endl;
TFcount++;
}
if (line == "MC"){
cout<<"Multiple Choice"<<endl;
MCcount++;
}
}
}
myfile.close();
percentCorrect = (userPoints / userTotalPoints) * 100;
w=1;
cout << "Your Points:"<< userPoints<<", Total Points: "<< userTotalPoints << endl;
cout << "Percent Correct: "<< percentCorrect <<"%"<<endl;
cout << "Thank you for using the Professor IT exam software! \n by Sol Johnston"<<endl;
cout<<" "<<endl;
} else {
cout << "Unable to open file\n";
w=0;
}
}else if (loadanexam == "N"){
//answer n for no
cout << "Thank you for using the Professor IT exam software! \n by Sol Johnston";
cout<<" "<<endl;
w=1;
}else{
cout<<"Try again"<<endl;
}
} while (w<1);
// Exit
return 0;
}
// Determines if the string is a number
bool isNumeric(string pszInput) {
istringstream iss(pszInput);
double dTestSink;
iss >> dTestSink;
// was any input successfully consumed/converted?
if (!iss) {
return false;
}
// was all the input successfully consumed/converted?
return (iss.rdbuf()->in_avail() == 0);
}