C++ 在C+中使用用户输入(如是和否)来控制程序流+;

C++ 在C+中使用用户输入(如是和否)来控制程序流+;,c++,if-statement,C++,If Statement,我正在制作一个使用if-else语句的小程序,但是我不想用数字来控制流,我希望能够用yes和no来控制流 例如: cout << "would you like to continue?" << endl; cout << "\nYES or NO" << endl; int input =0; cin >> input; string Yes = "YES"; string No = "NO"; if (input == no) {

我正在制作一个使用if-else语句的小程序,但是我不想用数字来控制流,我希望能够用yes和no来控制流

例如:

cout << "would you like to continue?" << endl;
cout << "\nYES or NO" << endl;
int input =0;
cin >> input;
string Yes = "YES";
string No = "NO";

if (input == no)
{
    cout << "testone" << endl;
}
if (input == yes)
{
    cout << "test two" << endl;
         //the rest of the program goes here i guess?
}
else
{
    cout <<  "you entered the wrong thing, start again" << endl;
              //maybe some type of loop structure to go back
}

cout你不是在读
字符串
,而是在读
int

试试这个:

string input;
而不是

int input = 0;

也是,C++是区分大小写的,所以不能定义一个变量,叫做<代码>是/代码>,然后尝试使用它作为<代码>是/代码>。它们必须在同一个案例中


顺便说一句,你的第二个
if
语句应该是
else if
,否则如果你输入“NO”,那么它仍然会进入最后一个
else
块。

你不是在
字符串中阅读
,而是在
int
中阅读

string input;
cin >> input;
if (input == "yes"){

}
else if (input == "no"{

}

else {
    //blah
}
试试这个:

string input;
而不是

int input = 0;

也是,C++是区分大小写的,所以不能定义一个变量,叫做<代码>是/代码>,然后尝试使用它作为<代码>是/代码>。它们必须在同一个案例中


顺便说一句,你的第二个
if
语句应该是
else if
,否则如果你输入“NO”,那么它仍然会进入最后一个
else
块。

首先,
输入必须是
std::string
,而不是
int

string input;
cin >> input;
if (input == "yes"){

}
else if (input == "no"{

}

else {
    //blah
}
另外,您写的
yes
no
错误:

             v
if (input == No)
// ..
//                v
else if (input == Yes)
^^^^
如果您想让程序使用“no…”,可以使用
std::string::find

if( std::string::npos != input.find( "no" ) )
// ..
“是”也一样


此外,您可以这样做,几乎不区分大小写-将输入转换为大写字母(或小写字母,随便什么),然后使用
find
。这样,
yEs
仍然是有效答案。

首先,
input
必须是
std::string
,而不是
int

bool yesno(char const* prompt, bool default_yes=true) {
  using namespace std;
  if (prompt && cin.tie()) {
    *cin.tie() << prompt << (default_yes ? " [Yn] " : " [yN] ");
  }
  string line;
  if (!getline(cin, line)) {
    throw std::runtime_error("yesno: unexpected input error");
  }
  else if (line.size() == 0) {
    return default_yes;
  }
  else {
    return line[0] == 'Y' || line[0] == 'y';
  }
}
另外,您写的
yes
no
错误:

             v
if (input == No)
// ..
//                v
else if (input == Yes)
^^^^
如果您想让程序使用“no…”,可以使用
std::string::find

if( std::string::npos != input.find( "no" ) )
// ..
“是”也一样

此外,您可以做到几乎不区分大小写-将输入转换为大写字母(或小写字母,无论什么),然后使用
find
。这样,
yEs
仍然是有效答案。

bool yesno(char const*prompt,bool default\u yEs=true){
bool yesno(char const* prompt, bool default_yes=true) {
  using namespace std;
  if (prompt && cin.tie()) {
    *cin.tie() << prompt << (default_yes ? " [Yn] " : " [yN] ");
  }
  string line;
  if (!getline(cin, line)) {
    throw std::runtime_error("yesno: unexpected input error");
  }
  else if (line.size() == 0) {
    return default_yes;
  }
  else {
    return line[0] == 'Y' || line[0] == 'y';
  }
}
使用名称空间std; if(提示(&C.tie()){ *cin.tie()
bool yesno(char const*prompt,bool default_yes=true){
使用名称空间std;
if(提示(&C.tie()){

*第(1)款这不是Windows编程。这只是标准的,平台无关的C++。我建议你阅读任何初学者C++书籍,而不是寻找Windows编程教程。这不是Windows编程。这只是标准的,平台无关的C++。我建议你阅读任何初学者C++书籍,而不必寻找Windows编程教程。ng input;足够,无需初始化。字符串输入;足够,无需初始化。