2017-11-27 8 views
-2

誰かが私が次のコードのエラーを取得している理由を説明してもらえますか(私はコードとエラーの両方を添付しています)。実際これは宿題に関する質問です。私はPythonの質問を正常に完了しました。今ではC++で同じ質問をする必要がありました。私は基本的に貼り付けをコピーして構文を変更しましたが、何とか動作しません。関数に関するC++コードのエラー

CODE

#include <iostream> 
using namespace std; 

float reduce(int num,float denom) 
{ 
    float a; 
    if (num>denom) 
    { 
     a = denom; 
    } 
    if (denom>num) 
    { 
     a = num; 
    } 
    float sol = 0; 
    while (a>1) 
    { 
     if ((num<=0) || (denom<=0)) 
      a = -10; 
     if ((num%a == 0) && (denom%a == 0)) 
     { 
      sol = 1; 
      a = -10; 
     } 
     a-=1; 
    } 
    return sol; 
} 

int main() 
{ 
    float num;float denum; 
    cout<<"Numerator: ";cin>>num; 
    cout<<"Denominator: ";cin>>denum; 
    float sol = Reduce(num,denom); 
    cout<<sol; 

} 

エラー

[Error] invalid operands of types 'int' and 'float' to binary 'operator%' 
[Error] invalid operands of types 'float' and 'float' to binary 'operator%' 
+0

明確ではありませんか?浮動小数点数ではモジュラス演算子を使用できません。整数にのみ適用されます。 – StoryTeller

+1

エラーメッセージが読み取られるはずです。彼らはまたgoogleに良いです。 – klutt

答えて

1

あなたがfloatとint型のMODを取得することはできませんが、一緒オブジェクト。
使用fmod機能:あなたが提案どおり絶対モッズ

float a = 10.5; 
int b = 100; 
int d = b/a; 
float mod = b - (a * d); 

編集をしたい場合は、その後

float a = 10.0; 
int b = 10; 
// parse float to int and get the mod 
int mod = b % ((int)a); 

を整数MODが必要な場合。 <math.h> fmod reference

+0

'fmod'関数は、浮動小数点mod –

+0

yupに使用できます。私はちょうどそれがどのようにすべきか説明していた。私もそれを追加します。そのことを教えてくれてありがとう – crook

関連する問題