c++ - 警告:我的代码上的多字符字符常量 [-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++ 的代码:
#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'
,不管它是什么。
推荐阅读
- java - 为图像添加标签 SWT Java
- internationalization - 如何让WSO2 EI碳UI支持中文
- c# - 如何使用 Net Topology Suite 在 Entity Framework Core 中将点(地理)缓冲 1 米
- php - 返回所有整数除数或返回字符串 '(integer) is prime' 如果它是素数的函数
- javascript - 当 0 为 false ReactJS 时如何设置此输入值
- amazon-web-services - 我应该使用什么服务器地址(mosquitto 代理 url)将 node-red 连接到 mosquitto?
- reactjs - 当尝试使用 fetch 在 Cypress IO (JS) 中针对 React 应用程序存根请求时,请求仍会调用服务器
- mongodb - 在mongodb中获取最大_id
- arrays - 在数组结构内调用 init
- django - 如何通过模型显示来自多对多的字段值