You must pass a pointer to a variable of the specified type when using scanf
.
double itemCost;
double paidMoney;
int changeDue;
printf("How much does the item cost: ");
scanf("%lf", &itemCost);
// ----------^
printf("How much did the coustomer pay: ");
scanf("%lf", &paidMoney);
// ----------^
Also, you’re neglecting to check the return value of scanf
. This is not optional! scanf
returns the number of items successfully assigned. If it returns N, but you specified M variables to be assigned, then the last (N-M) variables are left unassigned (and in your case uninitialized).
Try something like this:
for (;;) {
printf("How much did the coustomer pay: ");
if (scanf("%lf", &paidMoney) == 1)
break; // success
printf("Invalid input!\n");
}
solved Using printf and scanf functions for doubles in C