Hey guys, I’m still learning C and have created a virtual cash register. One problem, how do I display my output results after the total is calculated that’s all the program renders. I also can’t figure out how to instantiate a cashTendered method where the user will give the program (20.00 for example) and subtract that from it’s total amount of the items Also, if you spot any additional errors and/or have any ideas, please let me know.
#include <stdio.h>
int main() {
// Instantiate the Variables
float itemPrice1 = 0, // Represents the item's price
itemPrice2 = 0, // Represents secondary item price
itemQuantity1 = 0, // Accounts for the specified quantity of the item
itemQuantity2 = 0,
subTotal1 = 0, // Amount prior to taxAmount
subTotal2 = 0,
taxAmount = 0, // Percentage rate of 7% or 0.07
totalAmount = 0, // Accounts for totalAmount including taxAmount
cashTendered = 0, // Amount given towards totalAmount price
change = 0; // Deductable given after payment
// Implementation
printf("Enter the quantity and price for Paint :");
scanf("%f %f", &itemQuantity1, &itemPrice1);
printf("Enter the quantity and price for Blue Brush :");
scanf("%f %f", &itemQuantity2, &itemPrice2);
subTotal1 = itemPrice1 * itemQuantity1;
subTotal2 = itemPrice2 * itemQuantity2;
taxAmount = 0.07*(subTotal1 + subTotal2);
totalAmount = subTotal1 + subTotal2 + taxAmount;
change = cashTendered - totalAmount;
// Program's output results
printf("Your total is: %.2f", totalAmount);
scanf("%f", totalAmount);
printf ("Here is your receipt :\n");
printf ("JcPenny Stores\t\t\n");
printf ("Dayview Mall\t\t\n");
printf ("Article 1\t\t\t 1 @", itemPrice1, subTotal1);
printf ("Article 2\t\t\t 2 @", itemPrice2, subTotal2);
printf("Sub Total\t\t%.2f\n", subTotal1+subTotal2);
printf("Sales Tax(7%%)\t\t%.2f\n", taxAmount);
printf("Total Amount\t\t\t%.2f\n", totalAmount);
printf("Cash Tendered\t\t\t%.2f\n", cashTendered);
printf("Change\t%.2f\n\n", change);
printf("Thank you for shopping with us!");
return 0;
}
Questions
cashTendered method
You can’t create a (simple) cashTendered method (as a void function) with the current layout because all of your variables are only visible inside the scope of the
mainfunction. You would need to either pass all of your data to the cashTendered method (making structs would make this significantly less painful- see below) or make all of your variables global (generally considered to be a bad idea, for a number of complicated reasons).Suggestions
Combining
An item should be a struct, with members
priceandquantity(both should beints: see "Numeric Types" below). Subtotal doesn’t really need to be a member of the item structure, as it’s a property of the total transaction and not the item.Mistakes
Numeric types
You should really be using integers rather than floats for your prices, since you’re not counting a fractional amount of dollars but an integral number of cents (all dollar prices should be multiplied by 100).
For example, when you mark a $10.00 item down by a third, you want your new price to be $6.66, not $6.666666666666. While printf will adjust your output to two places, it won’t adjust your underlying math- it will report that somebody paying $6.66 won’t be giving enough money (since they’ll be short two-thirds of a cent).
There are other issues you’ll run into since float is not a decimal floating point type (6.66 – (6.65 + 0.01) may not equal zero, for instance).