3

I am working on a problem that requires me to input a float with exactly 2 digits of precision.

I know how to print a float with two digits of precision but how can I input a float under this condition? I think this is the problem with my program because there is no compilation error as such but it just says wrong answer. I am working with C language.

I tried to read an int and a float with two digits of precision as required the following way:

int x;
float balance,y;
scanf("%d %.2f",&x,&y);

For complete references, here are the question and my solution

21
  • scanf("%.2f",var_name)
    – Ankur
    Jan 14, 2015 at 15:15
  • 1
    scanf("%.2f",&var_name)
    – Quentin
    Jan 14, 2015 at 15:16
  • 6
    @Shan, @Quentin: Nope. There is only a field-width for scanf formats, not a precision.
    – mafso
    Jan 14, 2015 at 15:17
  • 1
    You really don't need any precision for this question.just understand logic and try to solve.This is very simple.
    – Ankur
    Jan 14, 2015 at 15:27
  • 1
    You are welcome, I'm not that old here anyway :-). But questions are required to show relevant part of source and eventual error messages directly in question and not in links. Jan 14, 2015 at 16:30

2 Answers 2

5

There is a problem in proposed solution. According to cplusplus.com, the format f only accepts a width and no precision.

If you had controlled the return from the scanf (what you should definitively always do - and now you know why !) you would have seen immediately the problem :

int x, cr;
float balance,y;
cr = scanf("%d %.2f",&x,&y);
if (cr != 2) {
    fprintf(stderr, "Read error : cr = %d\n", cr);
}

Given any input, you will get :

Read error : cr = 1

Possible implementation with very little change :

cr = scanf("%d %f",&x,&y);
if (cr != 2) {
    fprintf(stderr, "Read error : cr = %d\n", cr);
}
// should round to only two digits - within floating point accuracy 
y = (((double)((int)(y * 100 + 0.5))) / 100.);

If you have a system where math.h contains round (not MSVC :-( ), last line is better written as (thanks to chux for proposing it) :

y = round(y * 100)/100

because above formula will fail for negative y and for y > INT_MAX / 100

If you really needed exact precision with two decimal digit, the correct way would be to do all computation as integer on long, taking the numbers multiplied by 100. I leave that as an exercise for the reader :-)

6
  • i am new to programming with beginner level skills ... therefore i did not get you completely but i hope its correct.
    – HawkEyez
    Jan 14, 2015 at 17:07
  • @HawkEyez : as your are a beginner it is normal you make mistakes :-). Learn from them : take time to understand what was wrong and why, so you no longer make same again. Good luck ... PS : for the floating point accuracy problem do read Floating point inaccuracy examples suggested by dandan78. Jan 14, 2015 at 17:23
  • Using (((double)((int)(y * 100 + 0.5))) / 100.) to round to nearest 0.01 is incorrect for negative numbers and fails for values larger than INT_MAX. Better to use round(y * 100)/100 Jan 14, 2015 at 17:30
  • I now see the 0-2000 requirement buried in the comments. T'would been better had those requirements been posted by OP in the post. BTW: I should have said "fails for y values larger than INT_MAX/100". Likely OP's INT_MAX is not the minimal 32767. Jan 14, 2015 at 17:40
  • @chux : requirement was for numbers between 0 and 2000 (seen in OP code) and round is not available below C++11 nor under MSVC ... even if is definitively cleaner than my awkward line ! Jan 14, 2015 at 17:42
0

This answer inputs a float then checks it was in the correct format. But If you are working with money you should be using int cents; unless you have megabucks and don't care about cents, or are working to fractions of a penny (in which case you could work in 10th of a penny).

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

#define BUFLEN  20

int main()
{
    float money;
    char buff[BUFLEN+1], bank[BUFLEN+1], *eptr;
    printf ("Input the money: ");
    if (fgets(buff, BUFLEN, stdin) == NULL) {
        printf("Failed input\n");
        return 1;
    }
    if ((eptr = strchr (buff, '\n')) != NULL)   // newline?
        *eptr = 0;                              // terminate
    if (sscanf(buff, "%f", &money) != 1) {      // get the money
        printf("Failed input\n");
        return 1;
    }
    sprintf(bank, "%0.2f", money);              // check format of entry
    if (strcmp(bank, buff)) {
        printf("2 digit precision please: Euros.cents\n");
        return 1;
    }
    printf("You paid %s\n", bank);
    return 0;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.