I'm trying to get these outputs separated, but the second output's first character is being ignored.
Input:
1
1 2 3
Output:
(space)2 3
Expected output...
1 2 3
#include <iostream>
using namespace std;
int main()
{
string a; cin >> a;
string s; cin >> s;
getline(cin, s);
cout << s;
}
This program produces the required output
#include <iostream>
using namespace std;
int main()
{
string a; cin >> a;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
string s;
getline(cin, s);
cout << s;
}
The call to cin.ignore()
moves to the next line, as you are still on the previous line after cin >> a;
. You could do something like string dummy; getline(cin, dummy);
string dummy; getline(cin, dummy);
but ignore
avoids using an unnecessary variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.