2017-09-26 109 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")

相關問題