Need help with my code please.

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.
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	for (int i = 0; i < numberOfQuestions; i++) {

		std::cout << "Enter the question type:";
		while (true) {
			std::cin >> questionType;
			if (questionType == "TF" || questionType == "MC") {
				break;
			}
			std::cout << "Oops! Please enter \'TF\' or \'MC\': ";
		}

		if (questionType == "TF") {
			//...
		}

		if (questionType == "MC") {
			//...
		}

	}
Last edited on
Ahhh Thank you. I tried using while but was forgetting (true). Thanks for the help.
Topic archived. No new replies allowed.