2017-09-26 74 views
-2
#include <iostream> 

using namespace std; 

int main(){ 

    cout << endl; 
    cout << "Welcome to the wonderful world of a spy" << endl; 
    cout << "Today we are to decode some information that has been provided." <<endl; 
    string response; 
    cout << "Are you ready?" << endl; 
    cin >> response; 
    if (response == "yes", "y", "Yes", "Y"){ 
     cout << "Alright, let's go!!!" << endl; 
    } 
    else { 
     cout << "Well, too bad. We are going to do it anyways." << endl; 
    } 
} 

Exact Code这是我的代码到目前为止。我不能说“好吧,我们走吧!!!我做错了什么?问题和如果语句包含一个字符串在C++

+2

让我们从一个实际的代码片段开始,而不是一个截图。代码进入你的问题。 – Thebluefish

+5

请不要试图猜测语法。这个'if'语句的结果是''Y''的评估,它总是'true'(剩下的被丢弃,它们没有副作用)。请获得[好书](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Rakete1111

回答

3

您的if语句条件是错误的

if(response == "yes" || response == "y" || response == "Yes" || response == "Y") 
    { 
     //then do whatever... 
    }else{ 
     //do it anyway... 
    } 
4

if (response == "yes", "y", "Yes", "Y")不会做你认为它的作用。逗号运算符评估它的每个操作数,丢弃结果在左边,右边的结果是表达式的结果,所以你写的东西等于if ("Y"),你需要使用逻辑OR运算符来组合你的不同情况,就像这样if (response == "yes" || response == "y" || response == "Yes" || response == "Y")

相关问题