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

问题描述 投票:0回答:1

其中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)中的相同代码与command

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

打印正确的输出:

START
Lorem Ipsum
Hello World
5
END

这里可能是什么问题?

c++ file input cygwin command-prompt
1个回答
0
投票

感谢您的回复。答案已解决。正如@Johnny Mopp在上面的评论中指出的那样,Windows分别使用\r\n回车换行,而Unix仅使用\n,因此在Windows中需要写两个cin.ignore() ]或cin.ignore(2, '\n'),而在Unix中,一个cin.ignore()就足够了。

© www.soinside.com 2019 - 2024. All rights reserved.