Zybooks won't run this?

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {

	string userStr;	//User input
	bool inputDone = false;	//if user is done inputting
	while(!inputDone)
	{
		bool commaCheck = false;	//Chceck if there is comma
		do
		{	
			//Prompt user to enter a string w/comma
			cout << "Enter input string:" << endl;
			getline(cin, userStr);
			if (userStr == "q")		//If user is done
			{
				inputDone = true;
				break;
			}
			else{
				//Loops through string to see if there is comma
			for (unsigned int i = 0; i < userStr.length(); i++)
			{
				if (userStr[i] == ',')	//If there is a comma
					commaCheck = true;
			}
			if (!commaCheck)	//Print error message if no comma
			{
				cout << "Error: No comma in string." << endl << endl;
			}
		}
		} while (!commaCheck);	//Loop if no comma
	if(!inputDone)	//If user is not done,
	{
		string first, second;
		istringstream stream(userStr);
		getline(stream, first, ',');
		stream >> second;

		cout << "First word: " << first << endl;
		cout << "Second word: " << second << endl << endl;
	}
	}
	return 0;
}


Hello, this is my code, and this code runs on visual studio and dev c++, but for some reason, it will not work on zybooks. It's weird, it gives an error when I type in: Allen, Jill
"Program generated too much output.
Output restricted to 50000 characters.

Check program for any unterminated loops generating output."

Do you guys know what's up with it?

Last edited on
it gives an error when I type in: Allen, Jill


Did you also type in "q" in order to exit from the loop?
Is it possible that cin has a huge buffer that's not being flushed (like you are redirecting a file to the standard input of this program)? Then the program would loop until it finds "\nq\n" in the input file, and enter a potentially infinite loop.


Or cin has a small buffer which is rapidly emptied. After that the cin.fail() flag is set and no further input will be read. But the loop doesn't test for that condition, it only tests for (userStr == "q") which if it wasn't true on the final input before the end of input was reached, will never be true.

It can be simulated using a stringstream in place of cin.

If the stringstream contains "Allen, Jill\nq\n" the program works. But if it contains only "Allen, Jill" it will enter an infinite loop.
Hello! Thank you for the assistance, it is exactly because I didn't put in a q so it didn't exit. Thank you for the help! I feel so dumb about this lol.
Topic archived. No new replies allowed.