C++ 我的if-else语句不起作用,有人知道如何修复它吗?
基本上,我有一个项目大约在2天内到期,我找不到如何使这个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
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";
}
}