2016-11-25 25 views
-1

私は先週始まったC++の初心者です。私はいくつかのインチを足インチ表記に変換する簡単なプログラムを作成しようとしています。 62しかし、私がコンパイルしようとすると、私はライン8でいくつかのエラーを持って、私はそれが何であるかわからない。事前に感謝を。std :: stringstreamを使用すると予期しない結果が発生する

#include <iostream> 
#include <sstream> 
using namespace std; 
string ConvertToFootInchMeasure (int totalInches){ 
    string feet = ""+totalInches/12; 
    string inches = "" + totalInches%12; 
    stringstream converted; 
    conveted<<feet; 
    converted<<"'"; 
    converted<<inches; 
    converted<<"\""; 
    return converted.str(); 
} 
+0

'string feet =" "+ totalInches/12;'と 'string inches =" "+ totalInches%12;はあなたが思っていることをしません。 –

+0

どうしますか? – mtheorylord

答えて

3

コードは簡単にこのように固定することができる。5'2" に変身します:

string ConvertToFootInchMeasure (int totalInches){ 
    stringstream converted; 
    // Do inline calculations and use formatted text output for the results 
    converted << (totalInches/12) << "'" << (totalInches%12) << "\""; 
    return converted.str(); 
} 

さらに説明するためには:あなたはstd::string変数にtotalInches/12totalInches%12事業の数値結果を連結しようとしたが、文の右側には、それをしていません

注:

std::string operator+(std::string, char c)あなたが使用しようとしたとして、文字列と連結に数値の変換を行いません。

string feet = ""+totalInches/12; 

も理由用語は

""+totalInches/12 
あなたの場合にはさらに悪化するようです

ポインタはconst char*ポインタ用のポインタ演算を行い、string変数は例えばポインタアドレス"" + (62/12)

関連する問題