首页 > 解决方案 > 警告:我的代码上的多字符字符常量 [-Wmultichar]

问题描述

你好呀!我是新来的,所以我希望我不会做错任何事。

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

warning: multi-character character constant [-Wmultichar] 

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

这是代码:

#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");

}

标签: c++

解决方案


这是您翻译成 C++ 的代码:

#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所能做的就是测试输入值是否等于各个分支值。它只能做等价比较。

如果您执行以下操作:

switch (x) {
  case '>y':
    // ...
}

然后你问它x实际上等于(mutibyte) character '>y',不管它是什么。


推荐阅读