I'm currently learning c++ from programming books. Codeblocks is the IDE I am using. One of the exercises asks me to compare two string sizes inputted by the user and display the result all using a do while loop. The problem I have seems to be cropping up in some programs. When I input the two strings, I intend to press the enter button to mark the end of creating the first string and to then start the input process on the second string, yet the second string is interpreted literally as the second word. Whatever I put after the first space will be used as the second string. While I know this has useful implications in many programs, I'd like to know whether there is a way to force a change in this behavior so that the program starts reading input for the second string after I press enter.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Enter two strings to compare length\nNo pun intended\n\n";
string tst;
do {
string v1, v2;
cin >> v1;
cin >> v2;
if (v1.size() > v2.size()) {
int size1 = v1.size() - v2.size();
cout << "input 1 is bigger than input 2 by " << size1;
}
if (v2.size() > v1.size()) {
int size2 = v2.size() - v1.size();
cout << "input 2 is bigger than input 1 by " << size2;
}
if (v2.size() == v1.size()) {
int size3 = v1.size();
cout << "both inputs are the same: " << size3;
}
cout << "\n\nWould you like to make another comparison?\nPlease enter Yes or No\n\n";
cin >> tst;
} while (!tst.empty() && tst[0] != 'n' && tst[0] == 'y');
}