0

我有下面的代码,我唯一能看到它评估的是第 18 行,它是对 printf() 的调用。它不会更进一步。

#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);

您需要检查 的返回值scanf以确保它为 1,即已发生转换。

所以也许代码应该读

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 回答