2

Could anyone advise why my scanf function only works once and it ends in a continuous loop.

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int accno;
    float bbal, charge, rebate, limit, balance;

    printf("Enter account number (-1 to end):");
    scanf("%d", &accno);

    while (accno != -1) // User input phase
    {
        printf("Enter beginning balance:"); // User input phase
        scanf(" %.2f ", &bbal); // leave space after scanf(" 

        printf("Enter total charges:");
        scanf(" %.2f ", &charge);

        printf("Enter total rebates:");
        scanf(" %.2f ", &rebate);

        printf("Enter credit limit:");
        scanf(" %.2f ", &limit);

        balance = bbal - charge + rebate;

        // credit limit exceeded phase
        if (balance > limit)
        {
            printf("Account: %u", accno);
            printf("Credit limit: %.2f", limit);

            balance = bbal - charge + rebate;

            printf("Balance: %.2f", balance);
            printf("Credit limit exceeded!");
        }
        printf("Enter account number (-1 to end):");
        scanf("%d", &accno);
    }

    getch();
}
4
  • 2
    Try removing the space after %.2f Commented Sep 11, 2015 at 14:24
  • 3
    Note: using floating point for currency is a very bad idea! Commented Sep 11, 2015 at 14:28
  • 1
    You should check the return value from scanf() each time you call it to detect whether the conversion worked. if (scanf(" %.2f", &bbal) != 1) { …report error… }. Also, a trailing blank in a format string is a disaster in an interactive scanf(); the call won't terminate until you type the first character other than white space after the number — and that character will be left as the first character of the next input. If you type ahead the next number, you can move on — but the UI is not good. Commented Sep 11, 2015 at 14:43
  • You should never use scanf on user input (or file input come to that). You should read in a line at a time then use scanf on the line you read in. Otherwise you run into all sorts of problems if the input doesn't match what you think it matches Commented Sep 11, 2015 at 14:47

2 Answers 2

3

When I try to compile the code in the question, I get a warning that the scanf format string %.2f is illegal. If I change it to %f (needed to remove the leading and trailing spaces too), it all starts working as expected. I'm using clang on a Mac, but it should be the same as Windows.

/Users/jeremyp/dev/foo/foo.m:19:18: warning: invalid conversion specifier '.' [-Wformat-invalid-specifier]
scanf(" %.2f ", &charge);
       ~~^

Anyway, as one of the comments states, currency is not a floating point number. There are many ways to store currency, possibly the best is a large integer type e.g. a 64 bit int will do but storing the currency in cents or pence or whatever the lowest denominational unit is in your currency of choice.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I can get it running now!
0

In scanf("%.2f",&charge); their shouldn't be any space. Remove all the spacing.

2 Comments

Thank you so much for the tip! I'll keep that in mind!
Your welcome ! Kindly consider upvoting this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.