C收银机

时间:2011-05-23 18:39:37

标签: 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;
}

2 个答案:

答案 0 :(得分:4)

问题

cashTendered方法

您无法使用当前布局创建(简单)cashTendered方法(作为void函数),因为所有变量仅在main函数的范围内可见。您需要将所有数据传递给cashTendered方法(使结构化会使这个问题显着减少 - 见下文)或使所有变量全局化(由于一些复杂的原因,通常被认为是一个坏主意)

建议

结合

项目应该是一个结构,成员pricequantity(两者都应该是int s:请参阅下面的“数字类型”)。小计实际上不需要是项目结构的成员,因为它是总交易的属性而不是项目。

的错误

数字类型

你应该使用整数而不是浮点数来计算价格,因为你不计算美元的小数,而是整数美分(所有美元)价格应乘以100)。

例如,当您将$ 10.00项目标记为三分之一时,您希望新价格为6.66美元,而不是6.666666666666美元。虽然printf会将您的输出调整到两个位置,但它不会调整您的基础数学 - 它会报告支付6.66美元的人将无法提供足够的资金(因为它们将缩短三分之二的分数)。

由于浮点数不是decimal floating point类型(例如6.66 - (6.65 + 0.01)可能不等于零),因此还会遇到其他问题。

答案 1 :(得分:0)

在某些操作系统上,程序的输出显示在窗口中。程序一完成,窗口就会消失。

防止这种情况的一种方法是提示用户按“ENTER”继续或结束程序。

相关问题