2016-09-15 9 views
0

私はプログラミングの初心者です。大学エンジニアリングプログラム。私は楽しい時間を過ごし、クラス以外の何かを書くことに決めました。私は長方形のプリズムの長さ、幅、高さの3つの値を入力できるようにするプログラムをC++で作成しようとしています。プログラムは3つの値を使ってプリズムの表面積と体積を計算します。エラー: '演算子*'に一致するものはありません(オペランドの種類は 'std :: string {別名std basic_string <char>}'と{aka std basic_string <char>} ')

これはこれまで私が行ってきたことです。 (ボカレムで書かれている)

//---------------------------------------------------------------------------------------// 
//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 

私はそれをどうやって修正するのですか?

+0

floatまたはdouble(またはint)に自分のタイプを変更コンパイルしたい場合* width * height; ' - 変数を初期化し、それらに代入するのではなく、できる限りconstを宣言することを好みます。 –

答えて

5

あなたのlengthwidthおよびheightのタイプは、数値として解釈できないstringです。 `、と` constの2倍量=長さ; `CIN >>長>>幅>>高さ: は、あなたはそれだけで私も書きする傾向がある

関連する問題