使用 C++ 序列只运行一次的最佳方式可能是什么?为了更清楚,例如,我有一个程序需要猜测一个字符串,如果用户输入提示,我将显示该单词的提示,但我只允许它一次......我目前正在这样做:
bool hintLock = false;
...
if (guess == "hint"){
if (!hintLock){
cout << hint << endl;
hintLock = true;
}
else
cout << "I've given you the hint" << endl;
}
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
void main(){
string guess;
bool hintLock = false;
cout << "Guess one of StackExchange's best site: Type \"hint\" for hint" << endl << endl;
do{
cout << "Guess > ";
cin >> guess;
if (guess == "hint"){ // Here it is
if (!hintLock){
cout << hint << endl;
hintLock = true;
}
else
cout << "I've given you the hint" << endl;
}
}
while (guess != "stackoverflow");
cout << "You've got it right!" << endl;
}
有没有更好的声明来做到这一点?还是已经是最好的了?