简体   繁体   中英

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:

#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. 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 .
  2. You are calling your function twice in the main function.

     begbal = getPositiveValue(); getPositiveValue(); 

    Just remove the last statement

getPositiveValue() is supposed to return a value (float). You could add 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.

"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

and your function needs to return the float value which is "money" in this case.

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. 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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