0
所以我是编程新手,这是一部分大学工程课程。我决定在课外玩得开心,写点东西。我试图用C++编写一个程序,允许用户输入3个长度,宽度和高度的矩形棱镜值,程序将使用3个值来计算棱镜的表面积和体积。错误:'操作符*'不匹配(操作数类型是'std :: string {aka std basic_string <char>}'和{aka std basic_string <char>}')
这是我到目前为止。 (用Vocareum写)
//---------------------------------------------------------------------------------------//
//Calculate rectangular prism//
//---------------------------------------------------------------------------------------//
#include <string>
#include <iostream>
using namespace std;
int main()
{
string length; // length of prism in cm
string width; // width of prism in cm
string height; // height of prism in cm
cout << "Please enter the length, width, and height of the rectangular prism." << endl;
cin >> length; //length of prism in cm
cin >> width; //width of prism in cm
cin >> height; //height of prism in cm
double volume; //volume of prism in cm^3
double sa; //surface area of prism in cm^2
volume = length * width * height;
sa = (length * width * 2) + (width * height * 2) + (height * length * 2);
cout << "The volume of the rectangular prism is " << volume << " cm^3." << endl;
cout << "The surface area of the rectangular prism is " << sa << " cm^2." << endl;
return 0;
}
//Whenever I try to compile, I'll get 4 error messages that reads
//"error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}')
//ps these 3 comments aren't in the code
我该如何解决?
我也倾向于写:'CIN >>长度宽度>> >>高度;'和'常量双体积=长度* width * height;' - 倾向于初始化变量,而不是分配给它们,并且倾向于在可能的情况下声明const。 –