错误:'operator *'不匹配(操作数类型为'std :: string {aka std basic_string <char>}'和{aka std basic_string <char>}')

时间:2016-09-15 07:57:33

标签: c++

所以我是编程新手,这是大学工程课程的一部分。我决定玩得开心,在课堂外写点东西。我正在尝试用c ++编写一个程序,允许用户为矩形棱镜的长度,宽度和高度输入3个值,程序将使用3个值来计算棱镜的表面积和体积。

这是我到目前为止所拥有的。 (写在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

我该如何解决?

1 个答案:

答案 0 :(得分:5)

lengthwidthheight变量的类型为string,不能解释为数字。 如果您希望它进行编译,只需将其类型更改为floatdouble(或int