简体   繁体   English

二进制“ operator!=” |的类型为'double'和'const char [2]'的无效操作数

[英]Invalid operands of types 'double' and 'const char [2]' to binary 'operator!='|

I have a program that I need to make it so if I enter my first number as x it will terminate but i can't do that because my num1 is a double. 我有一个程序需要这样做,所以如果我输入第一个数字x,它将终止,但我不能这样做,因为num1是双精度型。 If anyone could help me figure out how to do that, that would be great. 如果有人可以帮助我弄清楚该怎么做,那就太好了。 Thanks 谢谢

#include <iostream>

using namespace std;
void compute(double, double);

int main()
{
double num1, num2;
while (num1!="x")
{
cout << "Enter First Number: " << endl;
cin >> num1;
cout << "Enter Second Number: " << endl;
cin >> num2;
compute(num1, num2);
}
return 0;
}
void compute(double num1, double num2)
{
double sum,diff,prod,quotient;
cout << "First Number: " << num1 << endl;
cout << "Second Number: " << num2 << endl;
sum=num1+num2;
diff=num1-num2;
prod=num1*num2;
quotient=num1/num2;
cout << "Sum: " << sum << endl;
cout << "Difference: " << diff << endl;
cout << "Product: " << prod << endl;
cout << "Quotient" << quotient << endl;
}

Your main problem here is that you're comparing a number to a const char* (ie, a string literal). 您的主要问题在于,您正在将数字与const char* (即字符串文字)进行比较。

If you want your program to compile, you might want to compare num1 to the character x , for example num1 != 'x' (not single quotes). 如果要编译程序,则可能需要将num1与字符x进行比较,例如num1 != 'x' (不是单引号)。 I will not guarantee this will work correctly, as comparing doubles and characters is a bit ludicrous, but it should compile and may get you a bit further down the road. 我不能保证它会正常工作,因为比较双打和字符有点荒谬,但是它应该可以编译,并且可能会让您走得更远。

You store your input in a std::string and check the content of the string before converting it to a double : 您将输入存储在std::string并在将字符串转换为double之前检查std::string的内容:

std::string s;
std::cout >> s;

if (s == "x")
  {
     // do something
  }
else
  {
     std::stringstream ss(s);
     double num1;
     ss >> num1;
     // do something else
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型为“ double”的无效操作数snd const char [3]”转换为二进制“ operator &lt;&lt;” - invalid operands of types ‘double’ snd const char [3]’ to binary 'operator<<' 二进制“ operator +”类型为“ const char [8]”和“ const char *”类型的无效操作数 - Invalid operands of types ‘const char [8]’ and ‘const char*’ to binary ‘operator+ &#39;const char*&#39; 和 const char[4]&#39; 类型的无效操作数到二进制 &#39;operator+&#39; - invalid operands of types 'const char*' and const char[4]' to binary 'operator+' 'const char [18]' 和 'const char*' 类型的无效操作数到二进制 'operator - invalid operands of types ‘const char [18]’ and ‘const char*’ to binary ‘operator 无效的 char 和 const char[2] 类型的操作数到二元运算符 - Invalid Operands of types char and const char[2] to binary operator 类型为“ double”和“ const char [5]”的C ++无效操作数为二进制“ operator +” - C++ invalid operands of types 'double' and 'const char [5]' to binary 'operator+' 类型为&#39;int&#39;和&#39;const char [15]&#39;的无效操作数为二进制&#39;operator &lt;&lt;&#39;^ - invalid operands of types ‘int’ and ‘const char [15]’ to binary ‘operator<<’ ^ 'int' 和 'const char [11]' 类型的无效操作数到二进制 'operator&lt;&lt;' - invalid operands of types 'int' and 'const char [11]' to binary 'operator<<' 错误:类型为&#39;const char [3]&#39;和&#39;int *&#39;的无效操作数为二进制&#39;operator *&#39; - Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*' “错误:二进制&#39;operator &lt;&lt;&#39;|的类型为&#39;int&#39;和&#39;const char [2]&#39;的无效操作数” - “error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'|”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM