【发布时间】:2017-03-08 03:58:43
【问题描述】:
我目前正在编写一个程序,它需要除了一个选项,然后是一段文本,如果是一段文本。如果文本为真,则执行一段代码?至少我认为它是这样工作的,但是,程序直接进入 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;
}
【问题讨论】:
标签: c++ loops if-statement