-2

2 つの temp の差を見つける関数があります。まず、摂氏と華氏のおおよその値を出力してから、それらの違いを見つけて出力します。私の問題は、プログラムを実行したときに発生するのは、すべての差の出力が 58 であることだけです。

次のようなものを出力する必要があります。

C----AF----Diff
1----32----31
2----34----32

私のコード:

void calDiff(int& cel, int& appFar, int diff){
while(cel!= 101){
    diff = appFar - cel;
    cout << diff << endl;
    cel++;
    appFar++;
}
}
4

2 に答える 2

1
  1. 摂氏を華氏に変換する関数が必要です。
  2. celandを変更したくない場合はappFar、参照を削除します&

int cel2far(int cel)
{
     // convert cel to far and return approx. far
}

void calDiff(int cel, int appFar, int diff)
{
    while(cel!= 101){
        diff = appFar - cel;
        cout << diff << endl;
        cel++;
        appFar = cel2far(cel);
    }
}
于 2013-04-18T17:52:59.107 に答える
0

You increment both the celcius and fahrenheit tempertures by one each loop, therefore the difference will be the same everytime. Just because you pass the temperature by reference doesn't mean it will recalculate the fahrenheit for you when you change it. You should increment celcius by one, recalculate the fahrenheit then calculate the difference.

于 2013-04-18T17:54:02.810 に答える