c - 如何在 C 中获取输入参数并使其返回 float

标签 c int

我有一项作业,我必须编写自己的 C 代码,该代码接受一个输入参数并给出一个 float 作为输出。目前我正致力于实现这一目标。我尝试了很多不同的方法来谷歌搜索,但一无所获。

我的代码允许用户输入他们想要微波炉加热的墨西哥卷饼数量,并根据他们的瓦数,为他们提供建议的在微波炉中加热的时间。

这是我现在拥有的。

#include <stdio.h>
int LowWatts(int burritos);
int MediumWatts(int burritos);
int HighWatts(int burritos);
int main ()
{

int burritos, menuSelect;
float Results;
burritos = (double)1; 


while (burritos > 0)
{
 printf ("Enter the amount of burritos you wish to cook \n: ");
 scanf("%d", &burritos);
 printf("%d\n", burritos);

 if (burritos > 0)
 {
 printf ("Enter 1 1100W, 2 to 1200W, 3 1250W microwave \n: ");
 scanf("%d", &menuSelect);
 printf("%d\n", menuSelect);
 if (menuSelect == 1)
 {
 Results = LowWatts(burritos);
 printf("For %d burrito(s) %.2f minutes is recomended for "
 "1100W microwave\n", burritos, Results);
 }
 else if (menuSelect == 2)
 {
 Results = MediumWatts(burritos);
 printf("For %d burrito(s) %.2f minutes is recomended for "
 "1200W microwave\n", burritos, Results);
 }
  else if (menuSelect == 3)
 {
 Results = HighWatts(burritos);
 printf("For %d burrito(s) %.2f minutes is recomended for "
 "1250W microwave\n", burritos, Results);
 }
 else
 printf("\nInvalid selection, please choose 1, 2, or 3 \n");
 } 
 } 
return 0;
}
int LowWatts(int burritos)
{
 return (double)(burritos*1.10);
} 

int MediumWatts(int burritos)
{
 return burritos*1.00;
}

int HighWatts(int burritos)
{
 return burritos*0.55;
}

最佳答案

您的函数都设置为 int 返回类型,因此无论您在 中进行何种类型的转换,它们都只会返回 int >返回语句。

将它们更改为返回floatdouble

关于c - 如何在 C 中获取输入参数并使其返回 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669538/

相关文章:

c++ - 使用 GetProcessTimes 在 Windows 上测量 CPU 时间

c++ - Int 输出不对应给定值

C++仅使用整数数学将 float 转换为十六进制

c++ - 用户空间和内核空间进程中的一组信号处理程序

php - PHP静态变量在内部是如何实现的?

c - 如何知道在 C 中转换为 void 指针的结构大小?

python-3.x - Python3 : convert big int to float - wrong result - how do it right?

c++ - 使用 vector 进行归并排序 (int) C++

java - int 上出现 NullPointerException?

c - 如何在C程序中显示当前时间,在CodeVisionAVR中编译