0

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;
  }
Dillion24
  • 1
  • 1

5 Answers5

1
  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

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Faisal Khalid
  • 620
  • 10
  • 22
1

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.

Phil Perry
  • 2,126
  • 14
  • 18
  • Thanks! I changed the if to a while statement and got rid of the extra getPositiveValue(). Also, added return money at the end of the function too. Code working as intended now :) – Dillion24 Dec 10 '13 at 20:58
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

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

vivek
  • 198
  • 4
  • 10
  • Calling the function twice is not an issue! – ericbn Dec 10 '13 at 20:55
  • @Eric Nielsen.... He clearly mentioned ""Also, when the program is run it asks the user twice to enter the beginning balance for some reason." First part answers why this is happening. – vivek Dec 10 '13 at 21:04
0

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.

Community
  • 1
  • 1
zwol
  • 135,547
  • 38
  • 252
  • 361
0

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;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256