[英]My Main function is only asking me for 1 input. How can I get the function to multiply three inputs?
What the program is trying to do is multiply 3 inputs (1.0f/3.0f) from 3 different functions to arrive at the volume of a pyramid.该程序试图做的是将来自 3 个不同函数的 3 个输入 (1.0f/3.0f) 相乘以获得金字塔的体积。 But I'm having trouble with the last function, which is asking me only for 1 input and therefore not multiplying the 3 inputs and (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);
}
I expect the output to be:我希望输出是:
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
but the output is但输出是
Welcome to the Pyramid Program
Enter a Number: 5
The Volume of the Pyramid is 0.000000
This is because you are only reading one input.这是因为您只读取一个输入。
scanf("%f", &num);
reads one input and stores it in the variable num, currently your baselength,basewidth and height are all initialised to 0 .You need to execute this three times for three inputs.读取一个输入并将其存储在变量 num 中,目前您的 baselength、basewidth 和 height 都被初始化为 0 。您需要为三个输入执行三次。 I have modified your code
我已经修改了你的代码
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 its always int main and it should always return 0 Ps 它总是 int main 并且它应该总是返回 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.