I'm a beginner at learning C and am using CodeStepByStep to practice, but I've recently been stuck at the receipt2 exercise:
The following console program always computes the same output. Modify it to be an interactive program that prompts the user to enter the meal cost as shown and computes the rest of the values based on that meal cost. Here are two example logs of execution:
What was the meal cost? $50
Subtotal: $50.00
Tax: $4.00
Tip: $7.50
Total: $61.50
What was the meal cost? $125
Subtotal: $125.0
Tax: $10.00
Tip: $18.75
Total: $153.75
This is the code I came up with:
int main() {
double subtotal;
double tax = subtotal * .08;
double tip = subtotal * .15;
double total = subtotal + tax + tip;
printf("What was the meal cost? $");
scanf("%1f\n", &subtotal);
printf("Subtotal: $%.2f\n", subtotal);
printf("Tax: $%.2f\n", tax);
printf("Tip: $%.2f\n", tip);
printf("Total: $%.2f\n", total);
return 0;
}
And this is what I get:
What was the meal cost? $50
Subtotal: $1487596185306552356576543796496142744039674099895955705687117069098083127278735...
Tax: $119007672208353363661503531346055809843579575493495085918530749993454701233205893051...
Tip: $223139385390662542606067286932452039577591885184656158736966400576173810359277903991...
Total: $1829742960203432862406782001386817676654305864334200732372522204186617249017377688...
I'm not sure what caused this so I'd greatly appreciate an explanation as well as a possible solution.
Thank you!