In my code i'm have some trouble trying to implement error checking based on what the user enters. For example I have this in my code:
1 2
if (questionType != "MC" && questionType != "TF")
std::cout << "Oops there has been an error, please be sure to enter MC or TF as these are the only two options. Try again. " << std::endl;
This somewhat works but not how I need it to. I need it to start back at the beginning of the for loop and I can't seem to figure out how. Is there anyway I can make this work. If not is there a different way of achieving what I am trying to do? I will post my whole code below.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//void createFile();
//void openExistingFile();
void createFile()
{
int numberOfQuestions;
int numberOfAnswers;
std::string questionType;
int questionValue;
std::string question;
std::string mcAnswers;
std::string Answer;
std::ofstream file;
std::string fileName;
std::cout << "Please enter the name of the file you wish to create." << std::endl;
std::cin >> fileName;
fileName += ".txt";
file.open(fileName.c_str());
std::cout << "please enter and integer number for the amout of questions you would like in your test. " << std::endl;
std::cin >> numberOfQuestions;
file << numberOfQuestions << std::endl;
for (int i = 0; i < numberOfQuestions; i++)
{
std::cout << "Please enter the question type in the format of MC for multiple choice or TF for true or false." << std::endl;
std::cin >> questionType;
file << questionType;
if (questionType == "TF") {
std::cout << "Please enter the integer point value of the question." << std::endl;
file << " ";
std::cin >> questionValue;
std::cin.ignore();
file << questionValue << std::endl;
std::cout << "Please enter the question." << std::endl;
std::getline(std::cin, question);
file << question << std::endl;
std::cout << "Please enter the answer to the question." << std::endl;
std::getline(std::cin, Answer);
file << Answer << std::endl;
}
if (questionType == "MC")
{
std::cout << "Please enter the integer point value of the question." << std::endl;
file << " ";
std::cin >> questionValue;
file << questionValue << std::endl;
std::cin.ignore();
std::cout << "Please enter the question." << std::endl;
std::getline(std::cin, question);
file << question << std::endl;
std::cout << "Please enter the number of answers to the question. The max amount of answers is six." << std::endl;
std::cin >> numberOfAnswers;
std::cin.ignore();
file << numberOfAnswers << std::endl;
for (int k = 0; k < numberOfAnswers; k++)
{
std::cout << "Please enter the " << k + 1 << " answer:" << std::endl;
std::cin >> mcAnswers;
file << mcAnswers << std::endl;
}
std::cout << "Please enter the correct answer of the " << numberOfAnswers << " answers." << std::endl;
std::cin >> Answer;
file << Answer << std::endl;
}
if (questionType != "MC" && questionType != "TF")
std::cout << "Oops there has been an error, please be sure to enter MC or TF as these are the only two options. Try again. " << std::endl;
}
}
/*void openExistingFile()
{
}
*/
void menu()
{
char selection = 'a';
do
{
std::cout << "Please select an option from the list below." << std::endl;
//display the menu
std::cout << "Menu";
std::cout << "======" << std::endl;
std::cout << "1 - Create new file" << std::endl;
std::cout << "2 - Open existing file" << std::endl;
std::cout << "E - Press E to exit" << std::endl << std::endl;
std::cout << "Enter selection: ";
// read user selection
std::cin >> selection;
switch (selection)
{
case'1':
std::cout << "You selected - Create new file" << std::endl;
createFile();
break;
case'2':
std::cout << "You selected - Open existing file" << std::endl;
//openFile();
break;
case'E':
case'e':
std::cout << "Thank you." << std::endl;
break;
//if 1, 2, 3 or E is not selected then go to the default case
default: std::cout << "Invalid selection. Please try again";
// no break in the default case
}
std::cout << std::endl << std::endl;
}
while (selection != 'E' && selection != 'e');
std::cout << "Thank you" << std::endl;
}
int main(int argc, char* argv[])
{
menu();//calls menu function
std::cin.ignore();
std::cin.get();
return 0;
}