[英]My if-else statement does not work, does anyone know how to fix it?
int response;
如果你想讀取一個字符, response
應該是一個char
而不是一個int
。
if (response == 'Y')
您忘記實際閱讀用戶輸入。 您的編譯器應該警告您使用未初始化的response
。
else (response == 'N')
這是您得到錯誤的原因。 else
沒有條件。 else
就是“其他條件都不為true
”的情況。 else if
或沒有條件,您要么想要else if
條件。
正確的代碼可能如下所示:
#include <iostream>
int main() {
char response;
std::cout << "is your circuit a parallel circuit?";
std::cin >> response;
if (response == 'Y') {
std::cout << "YES";
} else if (response == 'N') {
std::cout << "NO";
} else {
std::cout << "invalid input";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.