C 收银机
大家好,我还在学习 C,并且创建了一个虚拟收银机。一个问题是,在计算出所有程序渲染的总数后,如何显示我的输出结果。我也无法弄清楚如何实例化 cashTendered 方法,用户将在该方法中提供程序(例如 20.00)并从项目总数中减去它此外,如果您发现任何其他错误和/或有任何想法,请告诉我。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题
cashTendered 方法
您无法使用当前布局创建(简单)cashTendered 方法(作为 void 函数),因为所有变量仅在
main
函数的范围内可见。您需要将所有数据传递给 cashTendered 方法(创建结构将使这显着减轻痛苦 - 见下文)或将所有变量设置为全局变量(由于许多复杂的原因,通常被认为是一个坏主意) 。建议
组合
项目应该是一个结构体,包含成员
price
和quantity
(两者都应该是int
:请参阅下面的“数字类型”)。小计实际上并不需要成为项目结构的成员,因为它是总交易的属性,而不是项目的属性。错误
数字类型
您实际上应该使用整数而不是浮点数来表示价格,因为您计算的不是美元的小数,而是美分的整数(全部为美元)价格应乘以 100)。
例如,当您将 10.00 美元的商品价格下调三分之一时,您希望新价格为 6.66 美元,而不是 6.666666666666 美元。虽然 printf 会将您的输出调整到两个位置,但它不会调整您的基础数学 - 它会报告支付 6.66 美元的人不会给出足够的钱(因为他们会缺三分之二美分)。
由于 float 不是 十进制浮点 类型(6.66 - 例如,(6.65 + 0.01) 可能不等于零)。
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
main
function. 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
price
andquantity
(both should beint
s: 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).
在某些操作系统上,程序的输出显示在窗口中。程序一完成,窗口就会消失。
防止这种情况的一种方法是提示用户按“ENTER”继续或结束程序。
On some Operating Systems, the program's output is displayed in a window. As soon as the program finishes, the window disappears.
One method to prevent this is to prompt the User to press "ENTER" to continue or end the program.