Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的if-else语句不起作用,有人知道如何修复它吗?_C++ - Fatal编程技术网

C++ 我的if-else语句不起作用,有人知道如何修复它吗?

C++ 我的if-else语句不起作用,有人知道如何修复它吗?,c++,C++,基本上,我有一个项目大约在2天内到期,我找不到如何使这个if-else语句工作。我以前做过。我不知道我做错了什么 #include <iostream> using namespace std; int main() { int response; cout << "is your circuit a parallel circuit?"; if (response == 'Y') { cout << "ye

基本上,我有一个项目大约在2天内到期,我找不到如何使这个
if-else
语句工作。我以前做过。我不知道我做错了什么

#include <iostream>
using namespace std;

int main()
{
    int response;
    cout << "is your circuit a parallel circuit?";

    if (response == 'Y')
    {
        cout << "yes";
    }
    else (response == 'N')
    {
        cout << "no";
    }
    return 0;
}
#包括
使用名称空间std;
int main()
{
int响应;
库特
如果要读取字符,
response
应该是
char
而不是
int

您忘记实际读取用户输入。编译器应该警告您使用未初始化的
response

这就是您得到错误的原因。
else
没有条件。
else
是“其他条件均为
true
”的情况。如果
没有条件,则需要
else

正确的代码可能如下所示:

#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";
    }
}
#包括
int main(){
字符响应;
std::cout>反应;
如果(响应='Y'){

std::cout首先,
response
是一个
int
,它包含一个数字。其次,你从不要求用户输入。你在哪里要求
response
的输入?另外,如果你期望
Y
N
作为值,为什么
response
int
?它应该是
else if
,而不是
else
(忽略代码中的其他问题)。
if (response == 'Y')
else (response == 'N')
#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";
    }
}