I am having problems with my code for this homework assignment. Everything is outputting correctly except I am getting an extra space after my "first" output. My code is shown below please help me to fix the extra space.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string userStr;
bool inputDone = false;
while(!inputDone)
{
bool commaCheck = false;
do
{
cout << "Enter input string:" << endl;
getline(cin, userStr);
if (userStr == "q")
{
inputDone = true;
break;
}
else{
for (unsigned int i = 0; i < userStr.length(); i++)
{
if (userStr[i] == ',')
commaCheck = true;
}
if (!commaCheck)
{
cout << "Error: No comma in string." << endl << endl;
}
}
} while (!commaCheck);
if(!inputDone)
{
string first, second;
istringstream stream(userStr);
getline(stream, first, ',');
stream >> second;
cout << "First word: " << first << endl;
cout << "Second word: " << second << endl;
cout << endl;
}
}
return 0;
}