我有一个while循环来继续循环我的int main()。我通过创建一个名为keepGoing的bool来做到这一点。现在我坚持使用印刷机" q"或键入"退出"结束计划。
我尝过像if(exitCon ==' quit')然后退出程序的东西。但我尝试的一切都会显示错误消息。
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdexcept>
#include <string>
#include <fstream>
using namespace std;
//function prototypes
double DoubleInput(string strQuestion);
double GradePoint(double &p);
int main()
{
const string PCENT = "%";
bool keepGoing = true;
cout << "===================================================================" << endl;
cout << " GPA CALCULATOOR " << endl;
cout << "===================================================================" << endl;
do{
double input = DoubleInput("\nPlease enter your numeric grade percentage: ");
double g = GradePoint(input);
cout << "You entered a grade of " << input << PCENT << endl;
cout << input << PCENT << " is equal to " << g << " grade points" << endl;
}while(keepGoing);
}
}
这是我通过
方式获取输入的功能double DoubleInput(string strQuestion)
{
double doubleHolder;
cout << strQuestion;
cin >> doubleHolder;
while(cin.fail())
{
cin.clear();
cin.ignore(10000, '\n');
cout << "No non-numeric inputs. Please try again!" << endl << endl;
cout << strQuestion;
cin >> doubleHolder;
}
cout << endl;
return doubleHolder;
}
答案 0 :(得分:0)
您需要切换布尔变量keepGoing的值以退出循环。没有其他确定的方法来处理这种情况。所以你必须在while循环中接收输入并检查它是'q'还是'exit'并将变量keepGoing重置为false。
答案 1 :(得分:0)
您可能正在阅读DoubleInput
中的输入,现在您有两个选择:
1不使用该功能,而是执行以下操作:
string str;
getline(cin, str);
if(str == "q" || str == "quit")
keepGoing = true;
else input = stod(str);
2使用该函数但返回一个double,其值为您预先定义的值,表示用户想要退出然后检查它:
double input = DoubleInput(msg);
if(input == EXIT)
keepGoing = true;
else doStuff();