![](/img/trans.png)
[英]How to generate .exe for opencv 3.0 c++ program from command prompt in windows10?
[英]Incorrect output from windows 10 command prompt
其中 C++ 編譯器是Cygwin和gcc 版本 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.