Reputation: 11
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?
Upvotes: 0
Views: 25137
Reputation: 652
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
)
Upvotes: 5