-1
scanf("%lf", &Deposit_Amount); 

This statement returns garbage value instead of 0.00 when I enter a character.

Here's my code


   double Deposit_Amount;

   StartDepositInput:;
   system("cls");
   printf("Enter amount to deposit: ");
   scanf("%lf", &Deposit_Amount); // when I enter 'g'
   fflush(stdin); 
   printf("%lf", Deposit_Amount); // it outputs 1802842372383946000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00 

Isn't the scanf should set the value of Deposit_Amount to 0.00 if I input not a valid double?

markcalendario
  • 218
  • 2
  • 9
  • 2
    What do you expect it to do ? What's your question ? – John3136 Jul 26 '22 at 20:52
  • Isn't the scanf returns 0.0 if I input not a valid double? – markcalendario Jul 26 '22 at 20:53
  • 2
    You are not actually checking the function return value (`int r = scanf(...);` will have return value in `r`). `scanf` will not set the pointer variables if it fails to match. Since your variable is uninitialised it will have an indeterminate (ie, garbage) value. – kaylum Jul 26 '22 at 20:53
  • I got it when I initialized it into 0.0. It is now working. – markcalendario Jul 26 '22 at 20:59
  • 1
    Your code does not initialize it to `0.0`. – Eugene Sh. Jul 26 '22 at 21:00
  • 1
    But do you understand that you still need to check the function return value? Otherwise you can't tell whether 0.0 came from the initialisation or from the `scanf` setting it. – kaylum Jul 26 '22 at 21:00
  • 2
    "Isn't the scanf should return 0.0 if I input not a valid double?" No. – William Pursell Jul 26 '22 at 21:10
  • One of the [hidden secrets](https://stackoverflow.com/questions/72178518#72178652) about `scanf` is that you should *always* check its return value — that is, the value returned by the `scanf` function itself. – Steve Summit Jul 27 '22 at 00:17

2 Answers2

2

you need to check scanf return value

if(scanf("%lf", &Deposit_Amount) != 1) 
{
    Deposit_amount = 0.0;
    /* some other error handling */
}

You should not flush stdin

0___________
  • 60,014
  • 4
  • 34
  • 74
1

Isn't the scanf should return 0.0 if I input not a valid double?

No, the specification of scanf does not say it does that.

In the 2018 C standard, clause 7.21.6.2 paragraph 4 says “… if a directive fails (as detailed below), the function returns.” It does not say any value, zero or otherwise, is put into the object being worked on.

To know whether a conversion succeeded, you should check the return value of the function. Do not just call scanf as a bare call. Either assign the return value to an int or test it in a condition.

Paragraph 16 says “The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned…” Thus, if you are expecting one item to be assigned normally, you should test whether the return value is one. (You can additionally test whether it is EOF to indicate some input failure occurred and, when reading multiple items at once, can test to see how many were assigned.)

Clause 7.21.6.2 is for fscanf, but it applies to scanf because clause 7.21.6.4, about scanf, refers to 7.21.6.2.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312