0

私は次のコードを持っています、そしてそれが評価しているのを物理的に見ることができる唯一のものはprintf()への呼び出しである18行目です。それ以上は進みません。

#include <stdio.h>
#include <stdlib.h>

int main (void) {

    int cylNum;
    double disp, pi, stroke, radius;
        pi = 3.14159;

    printf("Welcome to the Engine Displacement Calculator!\n");

    cylNum = scanf("Enter number of cylinders (then press enter): \n");

    stroke = scanf("Enter stroke: \n");

    radius = scanf("Enter radius: \n");

    disp = radius * radius * pi * stroke * cylNum;

    printf("Displacement is: %f", disp);

    getchar();
    printf("Press any key to exit!");
    return 0;
}
4

4 に答える 4

2

読み取ろうとしている変数は、scanf()の結果ではなく、「scanf()」のパラメーターである必要があります。

printf("Enter number of cylinders (then press enter): ");
scanf("%d", &cylNum);
...
于 2012-07-22T05:39:50.143 に答える
1

scanf関数は値を読み込むことです。

だからライン

cylNum = scanf("Enter number of cylinders (then press enter): \n"); 

次の行である必要があります

printf("Enter number of cylinders (then press enter): \n");
scanf("%d", &cylNum);

の戻り値をチェックして、scanf1であること、つまり変換が行われたことを確認する必要があります。

したがって、おそらくコードは次のようになります

do {
   printf("Enter number of cylinders (then press enter): \n");
} while (scanf("%d", &cylNum) != 1);

変数については、関数で。の代わりにdisp, pi, stroke, radius使用する必要があります。"%lf"scanf"%d

scanfprintfを参照してください

于 2012-07-22T05:44:42.923 に答える
0

「scanf」は、あなたが試しているようなパラメータを取りません。

printf("Enter number of cylinders (then press enter): \n");
scanf(" %d", &cylNum);

printf("Enter stroke: \n");
scanf(" %lf", &stroke);
于 2012-07-22T05:42:21.143 に答える
0
#include <stdio.h>
#include <stdlib.h>
int main (void) {

    int cylNum;
    float disp, pi, stroke, radius;
    pi = 3.14159;
    printf("Welcome to the Engine Displacement Calculator!\n\n");
    printf("Enter number of cylinders (then press enter): ");
    scanf("%d", &cylNum);
    printf("Enter stroke: ");
    scanf("%f", &stroke);
    printf("Enter radius: ");
    scanf("%f", &radius);
    disp = radius * radius * pi * stroke * cylNum;
    printf("Displacement is: %f\n\n", disp);
    return 0;
}
于 2012-07-22T05:44:04.207 に答える