首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >警告:代码中的多字符字符常量[-Wmultichar]

警告:代码中的多字符字符常量[-Wmultichar]
EN

Stack Overflow用户
提问于 2021-03-29 18:27:11
回答 1查看 447关注 0票数 0

你好!,我是新来的,所以我希望我没有做错什么。

我对我在代码中遇到的一个小问题感到沮丧,这就是问题所在:

代码语言:javascript
运行
复制
warning: multi-character character constant [-Wmultichar] 

每当我运行这个程序的时候,这个问题就会出现在我的所有三个案例中,是的,它确实运行,但是没有给我适当的结果。

这是代码:

代码语言:javascript
运行
复制
#include <iostream>
using namespace std;
int main()
{
  int a,b,c, delta=0;

  cout<<"insert the value of a : "<<endl;
  cin>>a;
  cout<<"insert the value of b: "<<endl;    
  cin>>b;
  cout<<"insert the value of c: "<<endl;
  cin>>c;

  delta= b*b -4*a*c;

  switch (delta){
        case '>0':  cout<<"the solutions are different "<<endl;break;
        case '=0':  cout<<"the solutions are equal "<<endl;break;
        case '<0':  cout<<"the solutions are impossible "<<endl;break;
}
system("pause");

}
EN

回答 1

Stack Overflow用户

发布于 2021-03-29 18:36:10

下面是翻译成C++的代码:

代码语言:javascript
运行
复制
#include <iostream>

// Avoid using namespace std, that prefix exists for a reason

int main()
{
  int a,b,c;

  std::cout << "insert the value of a : " << std::endl;
  std::cin >> a;
  std::cout << "insert the value of b: " << std::endl;
  std::cin >> b;
  std::cout << "insert the value of c: " << std::endl;
  std::cin >> c;

  int delta = b*b - 4*a*c;

  if (delta > 0) {
    std::cout << "the solutions are different " << std::endl;
  }
  else if (delta < 0) {
    std::cout << "the solutions are equal " << std::endl;
  }
  else {
    std::cout << "the solutions are impossible " << std::endl;
  }

  return 0;
}

请注意这里使用的if,如果您没有根据值的直接匹配进行分支,那么这是必需的。switch所能做的就是测试输入值实际上等于各种分支值。它只能做等价比。

如果你做了这样的事情:

代码语言:javascript
运行
复制
switch (x) {
  case '>y':
    // ...
}

然后,您就会要求x实际上等于(多字节)字符'>y',不管它是什么。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66859613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档