简体   繁体   English

Windows 10 命令提示符的输出不正确

[英]Incorrect output from windows 10 command prompt

Where C++ compiler is Cygwin with gcc version 8.3.0其中 C++ 编译器是Cygwingcc 版本 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 :从 Windows 10命令提示符命令

g++ sol.cpp && a.exe < in.txt

and in.txt is a file: in.txt是一个文件:

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 :Cygwin 终端(bash)中的相同代码与命令

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.正如@Johnny Mopp 在上面的评论中指出的那样,Windows 分别使用\\r\\n回车换行,而 Unix 仅使用\\n ,因此在 Windows 中需要编写两个cin.ignore()cin.ignore(2, '\\n')而在 Unix 中一个cin.ignore()就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM