簡體   English   中英

如何在 C++ 中使用 fflush(stdin)

[英]How to use fflush(stdin) in C++

我有一個關於 C++ 中的fflush()的簡單問題(我想是的)。 每次我寫一些代碼在 C++ 中輸入字符串時,我都必須嘗試很多方法,因為我的程序每次都會導致錯誤。 所以我會問一個非常簡單的問題。 我的代碼在這里:

void main()
{
    int n;
    string str;
    cin >> n;
    fflush(stdin);
    fflush(stdin);
    getline(cin, str);
    cout << n << endl << str << endl;
}

並且編譯器不允許我輸入字符串str ,我現在該怎么做?

而且我不想在這里談論cin.ignore() ,只是fflush()

刷新stdin是未定義的行為,因為 fflush 意味着在 output stream 上調用。 這取自 C 標准[7.21.5.2]/2

 int fflush(FILE *ostream);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; 否則,行為未定義。

您可以在下面的鏈接中找到標准 C,go 到第 139 頁

ANSI/ISO 9899-1990

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM