ifelse语句输出错误多多?


#include 
//#include 
using namespace std;
int main() {
  cout<<"please input a key\n";
  char ch=getche()
  if ch==('b');
  cout<<'\a';
  else
     if (ch=='\n')
     cout<<"what a boring select\n";
     else 
       cout<<"bye\n";  
}

错误好多?
ifelseyuju20230316

第8行最后的分号删掉
第7行改为char ch =getchar();


  if ch==('b');
->

  if (ch=='b')

参考GPT和自己的思路:

根据提供的代码,可以看到 ifelse 语句存在一些语法错误和逻辑错误,可能导致输出结果错误或无法编译通过。具体问题如下:

  1. 在第7行代码中,函数名称应该为 getch(),而不是 getche(),这可能导致编译错误。
  2. 在第8行代码中,应该使用括号包括条件判断语句,即 if (ch == 'b'),否则会导致编译错误。
  3. 在第9行代码中,应该使用 cout << '\a',而不是 cout '\a',否则会导致编译错误。
  4. 在第12行代码中,应该在 else 后面加上大括号 {},否则会导致逻辑错误。

综上所述,如果修正上述问题,代码将会变成如下形式:

#include <iostream>
//#include <conio>
using namespace std;
int main() {
    cout << "please input a key\n";
    char ch = getch();
    if (ch == 'b') {
        cout << '\a';
    } else if (ch == '\n') {
        cout << "what a boring select\n";
    } else {
        cout << "bye\n";
    }
}

修正后的代码应该能够正确编译并按照预期输出结果。如果仍然存在问题,可能需要进一步确定问题所在。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^