简体   繁体   中英

error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}')

So I'm new to programming and this is part of a university engineering program. I've decided to have fun and write something outside of class. I'm trying to write a program in c++ that allows the user to enter 3 values for length, width, and height of a rectangular prism and the program will use the 3 values to calculate the surface area and volume of the prism.

This is what I have so far. (Written in 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

How do I fix it?

The type of your length , width and height variables is string which cannot be interpreted as a number. If you want it to compile just change their type to float or double (or int )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM