Issues with loop

Ok Im trying to have the program end when the person enters "done" on either input1 or input2 and its still requiring the person to enter "done" and "done" on both input1 and input2 in order for the program to end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void write_questions()
{
	string input1, input2;
	ofstream biology_flash;
	biology_flash.open("C:\\Users\\Austin\\Desktop\\biology_flash_cards_questions.txt");	//sort folder is created and opened.
	cin.ignore();
	biology_flash << "___________________________________________" << endl;
	while ((input1 != "done") || (input2 != "done"))
	{
		biology_flash << endl;
		getline(cin, input1);
		getline(cin, input2);
		biology_flash << "|       " << input1 << "        |      " << input2 << "       |" << endl;
		biology_flash << "___________________________________________" << endl;
	}
	biology_flash.close();
}
Well that is the way you programmed it!

To pseudo it up:

1
2
3
4
5
6
7
8
9
10
run the next sequence of code if input1 is not equal to "done" and input2 is not equal to "done" 

append biology_flash with a new line

request a line of input from the user, and place it into input1
request a line of input from the user, and place it into input2

append biology_flash with a pretty formatted input1 and input2

return to beginning of this loop and recheck the paramaters


You'll want a conditional statement that checks the result of the user's input right after you receive it. If the input is something that should kill your loop, use the break(); statement to break out of the loop.
On line 8 you're using the disjunctive OR so either input1 does not equal done OR input2 does not equal done.
Is that what you wanted or do you want to make sure that both of them do not equal "done"?
If it's the latter case you need to use the conjunction AND operator.
When the user types "done" in either input1 or input2 then the loop should end. Thus the reason for the OR operator.

While input1 or input2 doesn't equal zero then continue the loop.

This works but is it the most efficient?


void write_questions()
{
	string input1, input2;
	ofstream biology_flash;
	biology_flash.open("C:\\Users\\Austin\\Desktop\\biology_flash_cards_questions.txt");	//sort folder is created and opened.
	cin.ignore();
	biology_flash << "___________________________________________" << endl;
	while ((input1 != "done") || (input2 != "done"))
	{
		biology_flash << endl;
		getline(cin, input1);
		if (input1 == "done")
			break;
		getline(cin, input2);
		biology_flash << "|       " << input1 << "        |      " << input2 << "      " << endl;
		biology_flash << "___________________________________________\n";
		if (input2 == "done")
			break;
	}
	biology_flash.close();
}
Topic archived. No new replies allowed.