我正在尝试做一个简单的数学问题,但我不断收到此错误消息.怎么了?我正在使用cloud9 ide.
/home/ubuntu/workspace/Sphere.cpp:在函数'int main()'中:/ home /ubuntu/workspace/scc.c:20:63:错误:类型'int'和'const char [15]的无效操作数]'到二元'运算符<<'cout <<"圆的面积是:"<< 3.14*米^ 2 <<"米平方"<< endl;
这是整个代码:
#include <iostream>
using namespace std;
int main() {
// Declare the radius
int meters;
cout << "Please enter the radius in meters: ";
cin >> meters;
// Calculate Diameter
cout << "The diameter of the circle is: " << meters*2 << "m" << endl;
//Calculate Area
double PI;
PI = 3.14;
cout << "The area of the circle is: " << 3.14*meters^2 << "meters squared" << endl;
}
Run Code Online (Sandbox Code Playgroud)
在C++中,^操作者不意味着幂.它意味着对两个整数值执行按位XOR运算.
而且,由于^具有较低的优先级比<<,编译器把你的语句
((cout << "The area of the circle is: ") << (3.14*meters)) ^
((2 << "meters squared") << endl);
Run Code Online (Sandbox Code Playgroud)
并挂断了2 << "meters squared"应该做的事情.
通常,C++具有std::pow取幂功能.但是仅仅对一个数字进行平方就太过分了,最好只将该数字乘以它:
std::cout << "The area of the circles is: " << 3.14*meters*meters
<< " meters squared" << std::endl;
Run Code Online (Sandbox Code Playgroud)