2017-03-05 4 views
-3

2つの数値(面積と幅)を入力して2つの変数(面積と幅)を分割して矩形の高さを出力するプログラムを作成しようとしています。高さの結果を得る。このプログラムを実行すると、結果として0.0000が得られます。Cプログラム2つの変数を分割するときにエラーを出力する

私はそれが私のscanfまたはprintf行のいずれかの変換指定子と関係するかもしれないと思います。

私はC言語を学んでいるので、問題の解決に問題があります。

#include <stdio.h> 

int main() 
{ 
    /* initilize variables (area, width and height) */ 
    float area, width, height; 
    /* assign variables */ 
    area = 0.0; 
    width = 0.0; 
    height = 0.0; 
    /* Gather user input */ 
    printf("Enter the area in square metres\n"); 
    scanf("%f", &area); 
    printf("Enter the width in square metres\n"); 
    scanf("%f", &width); 
    /* Height of a rectangle = area/width */ 
    height = area/width; 
    /* Print result */ 
    printf("The height of the rectangle is: %f", &height); 

    return 0; 
} 
+0

最後の 'printf'が間違っています。あなたは 'height'のアドレスを渡すべきではありません('& 'を取り除いてください)。 – StoryTeller

+3

*デバッガの使い方を学ぶことを本当にお勧めします。*あなたは、 'height'が*正しい*値を得ていることをすぐに知ることができます。したがって、問題は「2つの変数を分割する」とは無関係で、print文だけが間違っています。 –

+0

@MartinRそれを使用する方法、私はまたそれを学びたいが、私は最高の情報源を知らない。 –

答えて

1

エラーは、それがheightの代わり&heightあるべきラインに

printf("The height of the rectangle is: %f", &height) 

です。

printf("The height of the rectangle is: %f", height) 

に変更し、それを、あなたが行ってもいいです。

私は今それをテストしており、それは良いです。

Enter the area in square metres 
12 
Enter the width in square metres 
3 
The height of the rectangle is: 4.000000 

との質問にコメントで MartinRで指摘したように、あなたは、あなたが height、正しい値を取得することをすぐに見ることがデバッガを使用することを学ぶ必要があります。したがって、問題は「2つの変数を分割する」とは無関係で、print文だけが間違っています。それは基本的にタイプミスの問題、あなたは高さ変数( &height)のアドレスを印刷しているされて

0

これはちょっとした間違いでした。

#include <stdio.h> 

int main() { 
    /* initilize variables (area, width and height) */ 
    float area, width, height; 
    /* assign variables */ 
    area = 0.0; 
    width = 0.0; 
    height = 0.0; 
    /* Gather user input */ 
    printf("Enter the area in square metres\n"); 
    scanf("%f", &area); 
    printf("Enter the width in square metres\n"); 
    scanf("%f", &width); 
    /* Height of a rectangle = area/width */ 
    height = area/width; 
    /* Print result */ 
    printf("The height of the rectangle is: %f", height); 

    return 0; 
} 

マニュアルテスト

Enter the area in square metres 
25 
Enter the width in square metres 
5 
The height of the rectangle is: 5.000000 
Process finished with exit code 0 
0

他の人が述べたように
#include <stdio.h> 

int main() 
{ 
    /* initilize variables (area, width and height) */ 
    float area, width, height; 
    /* assign variables */ 
    area = 0.0; 
    width = 0.0; 
    height = 0.0; 
    /* Gather user input */ 
    printf("Enter the area in square metres\n"); 
    scanf("%f", &area); 
    printf("Enter the width in square metres\n"); 
    scanf("%f", &width); 
    /* Height of a rectangle = area/width */ 
    height = area/width; 
    /* I have just changed &height to height */ 
    printf("The height of the rectangle is: %f", height); 

    return 0; 
} 
1

、すべてのコードは、最終的なprintf()ラインを除いて正しいです。

その他の回答は、デバッガを示唆しています。デバッガに精通していない場合は、onlinegdbなどのオンラインデバッガを使用して開始することができます。

デバッガがブレークポイントにヒットすると、ローカル変数の右側のペインにarea,およびheightの値が表示されます。

したがって、問題はブレークポイントを置いた最後の行にしかないと推測できます。

関連する問題