我正在构建一个带有成员函数的菜单创建类,该成员函数应该显示菜单,从用户那里获取选择,测试它是否是有效的菜单项,并返回项目的编号。出于某种原因,编译器在下面的run()成员函数中的一个简单的cin语句中给出了“操作符'>>''模糊错误”。运行时,该函数正确捕获无效输入,但在该无效之后考虑所有输入。如果第一个输入正确,程序将完全终止。这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
class NumericalMenu {
private:
string prompt;
vector<string> options;
string canceltext;
string errortext;
bool repeatprompt;
int sel;
public:
NumericalMenu() {
prompt = "Choose an option:";
canceltext = "Cancel";
errortext = "Error!";
repeatprompt = true;
sel = 0;
};
void setPrompt(string text) {
prompt = text;
};
int size() const {
int size = options.size() + 1;
return size;
};
int addOption(string text) {
options.push_back(text);
int position = options.size() - 1;
return position;
};
void setCancelText(string text) {
canceltext = text;
};
void setRepeatPromptOnError(bool repeat) {
repeatprompt = repeat;
};
void setErrorText(string text) {
errortext = text;
};
int run() const{
cout << prompt << "\n\n";
for (unsigned i=0; i<options.size(); i++) {
cout << i+1 << " - " << options[i] << "\n";
}
int errorpos = this->size();
cout << errorpos << " - " << canceltext << "\n\n";
cin.clear();
cin.ignore();
cin >> sel;
if(cin.fail() || sel<=0 || sel>errorpos) {
cout << "\n" << errortext << "\n\n";
if(repeatprompt == true) {
cin.clear();
cin.ignore();
this->run();
}
}
if (sel == errorpos) {
return -1;
} else {
return sel;
}
};
};
int main() {
NumericalMenu menu;
menu.setPrompt("Choose an option:");
menu.addOption("Enter new values");
menu.addOption("Help");
menu.addOption("Save");
menu.setCancelText("Exit");
menu.run();
}
编辑:知道了!感谢大家。工作标题:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class NumericalMenu {
private:
string prompt;
vector<string> options;
string canceltext;
string errortext;
bool repeatprompt;
public:
NumericalMenu() {
prompt = "Choose an option:";
canceltext = "Cancel";
errortext = "Error!";
repeatprompt = true;
};
void setPrompt(string text) {
prompt = text;
};
int size() const{
int size = options.size() + 1;
return size;
};
int addOption(string text) {
options.push_back(text);
int position = options.size() - 1;
return position;
};
void setCancelText(string text) {
canceltext = text;
};
void setRepeatPromptOnError(bool repeat) {
repeatprompt = repeat;
};
void setErrorText(string text) {
errortext = text;
};
int run() const{
cout << prompt << "\n\n";
for (unsigned i=0; i<options.size(); i++) {
cout << i+1 << " - " << options[i] << "\n";
}
int errorpos = this->size();
cout << errorpos << " - " << canceltext << "\n\n";
int sel;
cin.clear();
cin >> sel;
if(cin.fail() || sel<=0 || sel>errorpos) {
cout << "\n" << errortext << "\n\n";
if(repeatprompt == true) {
cin.clear();
cin.ignore(1000, '\n');
int sele = this->run();
return sele;
}
}
if (sel == this->size()) {
return -1;
}
else {
return sel;
}
};
};
答案 0 :(得分:1)
您将run()
函数声明为const
,并且编译器正在强制阻止对成员变量的修改。
class NumericalMenu {
private:
int sel;
...
int run() const {
cin >> sel; // Not allowed
如果您需要修改成员变量,请删除内联const
函数定义中的run()
。
此外,作为一种良好做法,请尝试包含您在代码中直接使用的所有标题(例如<string>
)。