简体   繁体   English

我的 Main 函数只要求我输入 1 个。 如何获得将三个输入相乘的函数?

[英]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.

相关问题 我怎样才能只得到一个数字作为主要 function 的参数? - How can I get only one digit as an argument of the main function? 不能给出 y 和 z 的输入。 我只能得到 x 作为输入。 谁能告诉我我做错了什么 - can not give input of y and z. I can only get x as an input. Can anybody please tell me what did I do wrong 我怎样才能得到我的主函数返回的内容? - How can I get what my main function has returned? 如何从这个 function 取回我的主要 function 的迭代值? - How can I get back the value of iterations from this function to my main function? 如何使用chracter数组更改函数输入 - How can I change my function inputs with chracter array 该程序的目的是从输入中删除所有元音。 function 工作正常,直到我在字符串中添加一个空格,我该怎么办? - The aim of this program is to remove all vowels from an input. The function is working fine until I add a space to the string, what can I do? 扫描文本会迫使我进入下一行,以确认输入。 在特殊情况下可以防止这种情况吗? - Scanning text forces me to go in next line, to confirm input. Can I prevent this for a special case? 如何从C函数的三个输入创建多项式函数? - How to create a polynomial function from three inputs in a C function? 我的主要 function 不允许我输入任何输入并在一段时间后崩溃 - My main function does not allow me to enter any input and crashes after a while 我的 C 函数在系统目录上递归并在大输入上出现段错误。 我怎样才能解决这个问题? - My C function recurses over system directories and segfaults over large inputs. How can I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM