For loop problem

I am writing a program to create an exam. I have pretty much everything working, and the code runs, but I can't make it choose between T/F and MC questions. Also, when it writes to the external file, it only writes data from one question. Any help anyone can provide would be invaluable.

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
  int addQuestion(Question *myQuestions[], int numquestions)
	{
		for (int j = 0; j < loopcounter; j++) { // ***** My Code ***********************************************
			int questionvalue;
			int count = numquestions;
			string theQuestion, line, quiztype;
			cout << "\nEnter the Question type followed by a space then the point value: \n";
			getline(cin, line);
			//get the next line with the question type and the value of the question
			int npos = line.size();
			int prev_pos = 0;
			int pos = 0;
			while (line[pos] != ' ')
				pos++;
			quiztype = line.substr(prev_pos, pos - prev_pos);
			prev_pos = ++pos;
			questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word

			//process a true/false question																	 
			if (quiztype == "TF" || "tf" || "Tf" || "tF")
			{
				myQuestions[count] = new QuestionTF;
				cout << " \nEnter a True/False Question: ";
				getline(cin, theQuestion);
				myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
			}
			//process a multiple choice question
			else if (quiztype == "MC" || "mc" || "Mc" || "mC")
			{
				myQuestions[count] = new QuestionMC;
				cout << " \nEnter a Multiple Choice Question: ";
				getline(cin, theQuestion);
				myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
			}
			else
			{
				cout << "Sorry, that is not a recognized question type, try again: ";
			}
		}
			return ++numquestions;
		}
	//} // ********** Added Code ************************************************


	// Function to print out the Quiz questions from the Array. Uses methods from the
	// classes developed to allow this printing to be done generically to screen or file.
	void printQuizQuestions(Question *myQuestions[], int numquestions)
	{
		//print out the questions that have been processed
		for (int count = 0; count<numquestions; count++)
		{
			myQuestions[count]->printOptions();
			cout << "\n";
		}
	}


	//Function to write to the file in the format needed to read later when loading if //needed.
	void writeExamQuestionFile(Question *myQuestions[], int numquestions)
	{
		int count;
		string qtype;
		cout << numquestions << "\n";
		for (count = 0; count<numquestions; count++) {
			qtype = myQuestions[count]->getQuestionType();
			cout << qtype << " " << myQuestions[count]->getValue() << "\n";
			myQuestions[count]->printOptions();
		}

	}
Line 20 and line 28 don't do what you think. Instead of:

1
2
3
4
if (quiztype == "TF" || "tf" || "Tf" || "tF") //What You Have

if (quiztype == "TF" || quiztype == "tf" || quiztype == "Tf" || quiztype == "tF")
//^ What It Should Be 
Hello Draveis,

Your dearth of code poses more questions than just what needs fixed. zapshe has covered the problem with lines 20 and 28.

Without the rest of the code the questions are:

What is "Question"? Is this a class or a struct?

When you define the parameters of your functions. What is the point of "Question *myQuestions[]"? Is this a pointer, an array or an array of pointers? As is it does produce an error at compile time.

Where does "loopcounter" come from?

Is "Question" in a header file or in the file with "main"? If it is a separate file you need to include that in your post.

Without the rest of the code I have no way to answer these questions or come up with a way to get the program to compile or even test.

In the for loop of "addQuestion" did someone teach you the most difficult way of doing this or did you try to learn this from a book?

You are using "std::string"s. Use this to your advantage do not work against it.

You could do something like:
1
2
3
4
5
std::cout << "\n Enter the Question type followed by a comma then the question type and its point value"
	<< "\n (Your question,T or F value): ";
std::getline(std::cin, theQuestion, ',');
std::cin >> quiztype >> questionvalue;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

This way you input direct to the variables without the need of using the "substr".

I would change "quizetype" to a char not a string. And when expecting input from the use you only need one letter. This will save you from having to check every possibility in the if condition.

While I am here int npos = line.size();. It does work, but not the best idea as there is std::string::npos and your redefinition, although a local variable to the for loop, is confusing.

Also line.size() it the total number of elements of the string. This could affect your calculation ending up one more than you need.

I do not know what you need for this program, but there is only so much that can be done with the code given.

Hope that helps,

Andy
Topic archived. No new replies allowed.