以下代碼不會給出第二個提示「輸入消息」。我如何解決它?使用cin函數
cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
getline(cin, msg);
以下代碼不會給出第二個提示「輸入消息」。我如何解決它?使用cin函數
cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
getline(cin, msg);
嘗試使用getline
任何地方在此之前一個
cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
cin.ignore();
getline(cin, msg);
使用cin.ignore();
。
謝謝!有效。但是,我是否必須手動刷新緩衝區? –
@ANKI_YUME是的。 – 2015-07-12 03:36:04
'cin.ignore()'類似於'cin >> ws'嗎?我想我可能會在這種情況下使用後者。 – celticminstrel
當你輸入班次時,有一個換行符,通過geline功能讀取。所以你需要忽略那個換行符。
寫:
cout << "Enter shifts:" << endl;
cin >> shifts;
getchar();
cout << "Enter message:" << endl;
getline(cin, msg);
[cin和函數getline跳過輸入]的可能重複(http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Alejandro