简体   繁体   English

在函数中使用scanf和循环

[英]Working with scanf and loops in functions

I want to use a function to ask the user for a balance. 我想使用一个函数来询问用户是否有余额。 If the balance is below 0 the user is prompted to enter a value above 0. Here is the code I have done so far: 如果余额低于0,则提示用户输入大于0的值。这是我到目前为止所做的代码:

#include <stdio.h>
#include <string.h>

float getPositiveValue();
int main()
{
  float begbal;
  begbal = getPositiveValue();
  getPositiveValue();
  printf("balance: %f\n", begbal);

  return 0;
}

float getPositiveValue()
{
  float money;
  printf("Please enter the beginning balance: \n");
  scanf("%f", &money);
  if(money < 0)
  {
    printf("Enter a balance amount above 0:");
    scanf("%f", &money);
  }else{

  }
}

I get the error "warning: control reaches end of non-void function". 我收到错误“警告:控制到达非空函数的结束”。 I know I need to end the else statement, but not sure what to put in there if they did enter a value above 0. Also, when the program is run it asks the user twice to enter the beginning balance for some reason. 我知道我需要结束else语句,但是如果输入的值超过0,则不确定要放在那里的内容。此外,当程序运行时,它会要求用户两次输入起始余额。 Seems like this should be a simple fix, for some reason I have trouble getting my head around functions heh. 看起来这应该是一个简单的修复,由于某种原因我无法理解功能嘿。

Any help much appreciated. 任何帮助非常感谢。

revised working code(thanks): 修改后的工作代码(谢谢):

#include <stdio.h>
#include <string.h>

float getPositiveValue();
int main()
{
  float begbal;
  begbal = getPositiveValue();

  printf("balance: %f\n", begbal);

  return 0;
}

float getPositiveValue()
{
  float money;
  printf("Please enter the beginning balance: \n");
  scanf("%f", &money);
 while(money < 0)
  {
    printf("Enter a balance amount above 0:");
    scanf("%f", &money);
  }
        return money;
  }
  1. You need to return a float value, so in this case you can return money . 您需要返回浮点值,因此在这种情况下您可以返还money
  2. You are calling your function twice in the main function. 您在main函数中调用了两次函数。

     begbal = getPositiveValue(); getPositiveValue(); 

    Just remove the last statement 只需删除最后一个语句

getPositiveValue() is supposed to return a value (float). getPositiveValue()应该返回一个值(float)。 You could add return money; 你可以加return money; before its closing }. 在它关闭之前}。

What if the user is particularly dense and doesn't enter a positive amount? 如果用户特别密集并且没有输入正数,该怎么办? Do you want to give them only one chance? 你想给他们一次机会吗? If not, you probably want to use a while (money < 0.0) loop. 如果没有,你可能想要使用while (money < 0.0)循环。

"Also, when the program is run it asks the user twice to enter the beginning balance for some reason." “此外,当程序运行时,由于某种原因,它会要求用户两次输入起始余额。”

because you have called function getPositiveValue() TWICE IN YOUR CODE 因为你在你的代码中调用了函数getPositiveValue() TWICE

and your function needs to return the float value which is "money" in this case. 并且你的函数需要返回浮点值,在这种情况下是“money”。

The user could persist in entering the wrong thing, so you need a loop to iterate till they get it right. 用户可能会坚持输入错误的内容,因此您需要一个循环来迭代,直到他们做对了。 Also, you need to return a value from this function. 此外,您需要从此函数返回一个值。

float getPositiveValue()
{
    float money;
    fputs("Please enter the beginning balance: ", stdout);
    for (;;) {
        scanf("%f\n", &money);
        if (money >= 0)
            break;
        fputs("Balance must be nonnegative.\n"
              "Please enter the beginning balance: ", stdout);
    }
    return money;
}

But that's only the tip of the iceberg here. 但这只是冰山一角。 scanf should never be used , and floating-point numbers should not be used to keep track of money. 绝对不应使用scanf不应使用浮点数来跟踪资金。 What you should really be doing here is reading a line of text with getline (or fgets , if getline is unavailable), and parsing it with strtoul and custom logic, presumably as [$]dddd[.cc] (where square brackets indicate optional text), into a uint64_t value scaled to cents. 什么,你真的应该在这里做是读一本线与文本的getline (或fgets ,如果getline不可用),并与解析它strtoul和自定义逻辑,大概是作为[$] DDDD [.CC(其中方括号表示可选文本),转换为uint64_t值,缩放到美分。

As the user may enter an invalid range (a negative number) or non-numeric text, need to consume offending input before next prompt. 由于用户可能输入无效范围(负数)或非数字文本,因此需要在下一个提示之前消耗违规输入。

float getPositiveValue() {
  float money = 0.0;
  printf("Enter a balance amount above 0:");
  while ((scanf("%f", &money) != 1) || (money < 0)) {
    int ch;
    while (((ch = fgetc(stdin)) != '\n') && (c != EOF));
    if (c == EOF) break;
    printf("Enter a balance amount above 0:");
  }
  return money;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM