#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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 (unsignedint 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."
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.