我正在为算法使用双精度(浮点)变量,但它给了我这个错误:
[错误]类型为'double'和'const char [9]'的无效操作数到二进制'operator!='
那我应该使用哪个运算符?
# include <iostream>
# include <math.h>
using namespace std;
int main ()
{
double a, b, Spa, Tim, Acc, Vel1, Vel2, Velocity, Space, Acceleration;
cout << "Hello. This program may calculate velocity, space, time and acceleration.\n" << endl;
cout << "Please, insert what you want to calculate.\n" << endl;
cin >> a;
do
{
cout << "Incorrect value.\n" << endl;
cout << "Please, insert what you want to calculate.\n" << endl;
cin >> a;
}
while (a != "Velocity" | a != "Acceleration");
if (a == Velocity);
{
cout << "With space or acceleration value?\n" << endl;
cin >> b;
if (b == Space);
{
cout << "Insert space and time values.\n" << endl;
cin >> Spa >> Tim;
Vel1 = Spa / Tim;
cout << "The velocity is " << Vel1 << " km/h.\n" << endl;
}
else
{
if (b == Acceleration);
{
cout << "Insert acceleration and time values.\n" << endl;
cin >> Acc >> Tim;
Vel2 = Acc / Tim;
cout << "The velocity is " << Vel2 << " km/h.\n" << endl;
}
}
}
}
“普通老式数据”(POD)类型在C ++中,如double
,int
,char
等不携带关于它们所代表的事物的任何语义信息; 或以另外一种方式,一些是只是一个数字。
如果您希望在数字中包含某种语义含义,例如,您想要一个数字来表示“真实”(温度,速度,方向,CPU周期等),那么您需要存储该数字在另一种变量中(astd::string
会适合您的程序)
IE
std::string a, b;
double Spa, Tim, Acc, Vel1, Vel2, Velocity, Space, Acceleration;
您需要确保始终将字符串a
与b
字符串进行比较:
if (a == "Velocity")
{
和..
cout << "With space or acceleration value?\n" << endl;
cin >> b;
if (b == "Space")
{
另外,请注意不要;
在if ()
语句末尾添加分号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句