Where C++ compiler is Cygwin with gcc version 8.3.0
The following code
#include <iostream>
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << "START" << endl;
int x;
string a, b;
getline(cin, a);
cin >> x; cin.ignore();
getline(cin, b);
cout << a << endl;
cout << b << endl;
cout << x << endl;
cout << "END" << endl;
from Windows 10 command prompt command :
g++ sol.cpp && a.exe < in.txt
and in.txt is a file:
Lorem Ipsum
5
Hello World
prints INCORRECT OUTPUT
START
Lorem Ipsum
5
END
instead of CORRECT OUTPUT
START
Lorem Ipsum
Hello World
5
END
while the same code in Cygwin terminal (bash) with command :
g++.exe sol.cpp && ./a.exe < in.txt
prints the correct output:
START
Lorem Ipsum
Hello World
5
END
What is possibly the problem here?
Thanks for the responses. The answer was solved. As @Johnny Mopp in the comments above pointed out, Windows uses \\r\\n
ie carriage return and line feed respectively while Unix use \\n
only, therefore in Windows one would need to write two cin.ignore()
or cin.ignore(2, '\\n')
while in Unix one cin.ignore()
will suffice.
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.