所以我在業餘時間接受了學習C++的任務(Web Developer通過交易,所以我並沒有完全忘記代碼)。C++睡眠功能沒有按預期工作
#include <iostream>
#include <windows.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
string response;
cout << "Would you like to buy some bread? : " << flush;
getline(cin, response);
if (response == "yes" || response == "y" || response == "Yes" || response == "Y") {
cout << "\nYou have successfully bought some bread\nThanks." << endl;
Sleep(2000);
return 0;
}
else if (response == "no" || response == "n" || response == "No" || response == "N") {
cout << "\nYou didn't buy some bread.\n" << endl;
}
string second_response;
cout << "Would you like to go back and buy some bread? : " << flush;
getline(cin, second_response);
if (second_response == "yes" || second_response == "y" || second_response == "Yes" || second_response == "Y") {
cout << "\nYou have successfully bought some bread 2nd time round";
Sleep(2000);
}
else
{
cout << "\nMaybe next time?";
Sleep(2000);
}
return 0;
}
正如你所看到的,它只是要求依賴於他們的答案的人他們是否願意買麪包(我不是很有想象力)。
問題是,睡眠函數似乎正在工作,但字符串second_response部分中出現錯誤,會發生什麼情況是它等待2000毫秒,然後輸出文本時,它的意思是相反的方向?
我一直在谷歌尋找一個答案,但我認爲這將是更好的來這裏,所以我可以實際說出什麼是錯的正確。
謝謝你們!
太棒了,會做! –
好了,讀完這個之後,我決定在每個second_response之後放入新的換行符,並且所有的東西都似乎正常工作。編輯:正確地讀取緩衝/ cout的時間 –