繁体   English   中英

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

[英]Incorrect output from windows 10 command prompt

其中 C++ 编译器是Cygwingcc 版本 8.3.0

以下代码

#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;

从 Windows 10命令提示符命令

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

in.txt是一个文件:

Lorem Ipsum
5
Hello World

打印错误的输出

START
Lorem Ipsum

5
END

而不是正确的输出

START
Lorem Ipsum
Hello World
5
END

Cygwin 终端(bash)中的相同代码与命令

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

打印正确的输出:

START
Lorem Ipsum
Hello World
5
END

这里可能有什么问题?

感谢您的回复。 答案已解决。 正如@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