1
獲得答案我是一名初級程序員,可能是您的計算機專家可能弄錯的錯誤代碼。我已經記住了一個段落程序供我自己使用,你能想出一個辦法,以便每次都會發生getline嗎?這裏是我的代碼...我該如何讓.getline每次都能通過
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
void main(){
string sentence;
string attempt;
char key;
int counter = 0;
cout << "Insert your sentence/paragraph (will be case sensitive) (don't press enter until you're done)." << endl << endl;
getline (cin, sentence);
cout << endl;
while (true){
system ("cls");
Sleep (5);
cout << "Now enter the sentence/paragraph" << endl;
getline (cin, attempt);
if (sentence == attempt){
cout << "Good job, do you want to go again? N for no, anything else for yes" << endl;
cin >> key;
if (key == 'n' || key == 'N'){
break;
}
}
else{
cout << "You messed up, try again." << endl;
system("pause");
continue;
}
}
system("pause");
}
[C++ getline()的可能重複不等待多次調用時從控制檯輸入](http://stackoverflow.com/questions/7786994/c-getline-isnt-waiting-for-input-from -console - 當所謂的海報倍) – ildjarn