0

I am in a programming class since 2 weeks and have some trouble with scanning keyboard input an assigning it to a variable in C.

#include <stdio.h>
#include <stdlib.h>

int main()

{
  float creditSum, interestRate, repayment;

  system("clear");

  printf("\n please enter the total Credit sum: ");
  scanf("%.2f", &creditSum);
  printf("\n Please enter the interest rate: ");
  scanf("%.2f", &interestRate);
  printf("\n Please enter the monthly repayment amount: ");
  scanf("%.2f", &repayment);

  printf("\n\n %.2f | %.2f | %.2f\n\n", creditSum, interestRate, repayment);


   return 0;
}

I can compile an run the program but am getting

user@db10:~/$ ./credit 

 please enter the total Credit sum: 100

 Please enter the interest rate: 
 Please enter the monthly repayment amount: 

 0.00 | 0.00 | 0.00

So I still can enter the first of 3 values, which is not assigned to the variable as planned.

The course teacher has everybody add a fflush(stdin) before the scanf() on windows machines but this does not work for me (in a linux environment).

I have seen some issues here dealing with the fflush on linux issue, but can't really apply anything successfully for my case (which may be due to me being a complete novice in coding).

Can anyone help here?

The teacher takes a "can't troubleshoot Linux problems as Windows is the OS of choice" approach, so there is not help to be expected from that side.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
vrms
  • 217
  • 2
  • 8
  • [`fflush(stdin);` is undefined behaviour, don't do it.](https://stackoverflow.com/a/38325926/2173917)" – Sourav Ghosh Nov 13 '20 at 08:40
  • Check for the return value of `scanf()`. – Sourav Ghosh Nov 13 '20 at 08:40
  • 2
    Try `scanf("%.2f",` -> `scanf("%f",` – Support Ukraine Nov 13 '20 at 08:48
  • 1
    And: `if (scanf(...) != 1) { puts("INPUT ERROR"); exit(1);}` – Support Ukraine Nov 13 '20 at 08:49
  • thanks for the comments. The return value of `scanf()`, not using `fflush` is in my post where I am just scanning 3 keybard inputs and assign them to variables. Actually there is no value assigned. Not to use `fflush` is not a solution because I am not doing it anyway. The reason for this post is that the `scanf()` operation does not seem to work. – vrms Nov 13 '20 at 08:50
  • 1
    @vrms Did you try what I suggested above? – Support Ukraine Nov 13 '20 at 08:51
  • 2
    You can't use `%.2f` in a `scanf` input format. Change them to `%f`. – Tom Karzes Nov 13 '20 at 08:54
  • Compile with all warnings enabled. (`-Wall`) – Jabberwocky Nov 13 '20 at 08:55
  • @SouravGhosh: [Windows (VS 2019) defines the behaviour of `fflush()` for input streams](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fflush?view=msvc-160): *"If the stream was opened in read mode, or if the stream has no buffer, the call to fflush has no effect, and any buffer is retained."* **still undefined in other environments -- it's best to avoid using fflush with input streams** – pmg Nov 13 '20 at 09:07
  • it seems the `%.2f` is the culprid. `%f` does not cause any problems. – vrms Nov 13 '20 at 09:08
  • I guess you need to scan all decimal (`%f`) even if the variable is of `%.2f` type. – vrms Nov 13 '20 at 09:15
  • @vrms there is not `%.2f` type. And be aware that `scanf` is not `printf` even if some of the `%` specifiers are similar. – Jabberwocky Nov 13 '20 at 09:28
  • 1
    @vrms Consider this: What would you like to happen in case the user input is: `100.123 9.00 8.00` ? If you could tell `scanf` to stop after 2 decimals the input stream would contain `3 9.00 8.00`. Is that really what you want? – Support Ukraine Nov 13 '20 at 09:28
  • @vrms You can limit the maximum number of characters used in a scan using e.g. `%6f` but I'm pretty sure that's not what you want. – Support Ukraine Nov 13 '20 at 09:29

1 Answers1

1

The scanf catches the input format that is not expected.

The right code should be this:

#include <stdio.h>
#include <stdlib.h>

int main()

{
  float creditSum, interestRate, repayment;

  system("clear");
  

  printf("\n please enter the total Credit sum: ");
  if(scanf("%f", &creditSum)!= 1) return -1;
  printf("\n Please enter the interest rate: ");
  if(scanf("%f", &interestRate)!= 1) return -1;
  printf("\n Please enter the monthly repayment amount: ");
  if(scanf("%f", &repayment)!= 1) return -1;

  printf("\n\n %.2f | %.2f | %.2f\n\n", creditSum, interestRate, repayment);


   return 0;
}

You probably don't want to capture only 2 decimal of the float. You want take the float number and then print it with only 2 decimal.

In any case is important to check the return value of the scanf:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

because it can avoid bugs on code.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35