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.