gpt4 book ai didi

c++ - 错误 : no match for 'operator*' (operand types are 'std: :string {aka std basic_string}' and {aka std basic_string}')

转载 作者:太空宇宙 更新时间:2023-11-04 16:03:28 28 4
gpt4 key购买 nike

所以我是编程新手,这是大学工程类(class)的一部分。我决定找点乐子,在课外写点东西。我正在尝试用 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

我该如何解决?

最佳答案

lengthwidthheight 变量的类型是 string,不能解释为数字.如果你想让它编译,只需将它们的类型更改为 floatdouble (或 int)

关于c++ - 错误 : no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39505833/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com