该程序试图做的是将3个不同函数的3个输入(1.0f / 3.0f)相乘,得出金字塔的体积。但是我在使用最后一个函数时遇到了麻烦,该函数只要求我输入1个输入,因此不能将3个输入与(1.0f / 3.0f)相乘。
float volume(float baselength, float baseWidth, float height){
float frac = (1.0f/3.0f);
float vol;
vol = baselength*baseWidth*height*frac;
return vol;
}
float main(){
displayTitle();
float num;
printf("Enter a Number: ");
scanf("%f", &num);
float frac = (1.0f/3.0f);
float baselength;
float baseWidth;
float height;
float result;
result = volume(baselength, baseWidth, height);
printf("The Volume of the Pyramid is %f",result);
}
我希望输出为:
Welcome to the Pyramid Program
Enter a Number: 5
Enter a Number: 4
Enter a Number: 3
The Volume of the Pyramid is 20.00
但输出是
Welcome to the Pyramid Program
Enter a Number: 5
The Volume of the Pyramid is 0.000000
最佳答案
这是因为您仅读取一个输入。
scanf("%f", &num);
读取一个输入并将其存储在变量num中,当前您的baselength,basewidth和height都已初始化为0。您需要为三个输入执行三次此操作。我已经修改了您的代码
float volume(float baselength, float baseWidth, float height){
float frac = (1.0f/3.0f);
float vol;
vol = baselength*baseWidth*height*frac;
return vol;
}
int main(){
//displayTitle();
float num;
float frac = (1.0f/3.0f);
float baselength;
float baseWidth;
float height;
float result;
printf("Enter a Number: ");
scanf("%f", &baselength);
printf("Enter a Number: ");
scanf("%f", &baseWidth);
printf("Enter a Number: ");
scanf("%f", &height);
result = volume(baselength, baseWidth, height);
printf("The Volume of the Pyramid is %f",result);
return 0;
}
Ps始终为int main,并且应始终返回0
关于c - 我的主要功能仅要求我输入1次。如何获得将三个输入相乘的函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55529061/