我目前正在編寫一個程序,需要除了一個選項,然後是一段文本,如果一段文本。如果文本是真的,那麼一段代碼執行?至少我認爲這就是它的工作原理,然而,程序直接到else並且因爲初始條件而不斷循環,所以它不會詢問用戶的另一個輸入是getline()?爲什麼程序循環而不是要求輸入?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
fstream gFile;
int choice;
string gb;
do {
cout << "Student Grade Book Info Program....\n";
cout << "\tPlease Select an Option: (1 or 2) \n" << endl
<< "\t1. Review Grades"
<< "\n\t2. Quit"
<< "\n\tChoose: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\n\tPlease Enter the Name of the File you wish to View the Grades for: " << endl;
cout << "\n\tAvailable Grade Books: gradeBook\n" <<
"\tType in the Grade Book you would like to view. ";
getline(cin, gb);
if (gb == "gradeBook") {
cout << "execute code...";
}
else {
cout << "\nError there is no such entry in the system." << endl;
}
case 2:
break;
}
} while (choice != 2);
return 0;
}
ahh我看到我修好了!有效!太棒了,非常感謝。 –
解釋很好,但解決方案至今無關緊要。不,這不是一個好的解決方案,所以你在提取操作員之前限制了getline的使用! 'cin.flush()'和'cin.sync()'和'cin.ignore()'的作用是什麼? – Raindrop7
'getline'和'std :: getline'有什麼區別??? !!!! – Raindrop7