C 如何使用数组使此代码正确运行?

C 如何使用数组使此代码正确运行?,c,C,所以我正在尝试修复这个代码。当我运行编译器时,它会运行,但结果填充为零,而不是正确的美元金额。我错过了什么 // Array Discussion Code // Sales Tax Calculator int main() { //main function initialization int x, num; //initialization of integer values float array[10], total, tax;//initializatio

所以我正在尝试修复这个代码。当我运行编译器时,它会运行,但结果填充为零,而不是正确的美元金额。我错过了什么

// Array Discussion Code
// Sales Tax Calculator
int main() 
{
    //main function initialization
    int x, num; //initialization of integer values
    float array[10], total, tax;//initialization of array, total, and tax floats

    printf("Enter the sales tax rate in your state (in 0.xx format): ");//user is prompted to enter their state sales tax rate
    scanf("%f", &tax);//user enters their sales tax rate
    printf("\nEnter the number of items you purchased: ");//prompts the user for the number of items they bought
    scanf("%d", &num);//user inputs the number of products they bought
    printf("\nEnter the price of each item: ");//prompts the user to begin entering the prices of each item

    for (x = 0; x<num; x++){//for loop to allow user to input the number of values they want
        scanf("%f", &array[num]);//user inputs a value
        total = 0;//initial value of total
    }
    for (x = 0; x < num; x++){//loop executes as many times as there are items
        total = total + array[x];//for loop to find the total of all items

        printf("\nYour total before tax is $%.2f", total); //program displays total before tax

        tax = tax * total;//total sales tax is calculated

        printf("\nYour sales tax is $%.2f", tax);//total sales tax is displayed

        total = tax + total;//grand total is calculated

        printf("\nYour grand total is $%.2f", total);//grand total is displayed
    }
    return 0;
}//end main function
//数组讨论代码
//销售税计算器
int main()
{
//主函数初始化
int x,num;//整数值的初始化
浮动数组[10],总计,税;//数组,总计和税浮动的初始化
printf(“输入您所在州的销售税率(0.xx格式):”;//系统将提示用户输入其所在州的销售税率
scanf(“%f”,&tax);//用户输入其销售税率
printf(“\n输入您购买的商品数量:”);//提示用户输入他们购买的商品数量
scanf(“%d”,&num);//用户输入他们购买的产品数量
printf(“\n输入每个项目的价格:”);//提示用户开始输入每个项目的价格

对于(x=0;x来说,代码的第一个问题就是对项目进行scanf()操作。将值存储到数组[num]中,而不是数组[x]。但是,问题远不止这些。您允许用户选择项目的数量,但您没有强制执行限制。此外,当您获得总计、总计和税收总计时,您在循环中运行它们,这意味着每次运行循环时,您都会重新运行所有这些计算

为了向您展示这些问题,我首先向您展示了带有问题修复的代码,然后我向您展示了一种更优化的方法来完成下面的工作

你的方法有一些修正。但是仍然是相同的计算

#include <stdio.h>
// Array Discussion Code
// Sales Tax Calculator
int main() 
{
    const int maxItem = 100;
    //main function initialization
    int x, num; //initialization of integer values
    float array[maxItem] = { 0.0 }, total = 0, tax = 0;//initialization of array, total, and tax floats

    printf("Enter the sales tax rate in your state (in 0.xx format): ");//user is prompted to enter their state sales tax rate
    scanf("%f", &tax);//user enters their sales tax rate
    printf("\nEnter the number of items you purchased(maximum %d): ", maxItem);//prompts the user for the number of items they bought
    scanf("%d", &num);//user inputs the number of products they bought

    while(num > maxItem){
      printf("\nNumber of item purchased can only be at maximum of %d. Please re-enter an amount: ", maxItem);//prompts the user for the number of items they bought
      scanf("%d", &num);//user inputs the number of products they bought
    }

    printf("\nEnter the price of each item: ");//prompts the user to begin entering the prices of each item

    for (x = 0; x<num; x++){//for loop to allow user to input the number of values they want
        printf("\nItem %d: ", x);
        scanf("%f", &array[x]);//user inputs a value
    }
    for (x = 0; x < num; x++){//loop executes as many times as there are items
        total = total + array[x];//for loop to find the total of all items

        printf("\nYour total before tax is $%.2f", total); //program displays total before tax

        tax = tax * total;//total sales tax is calculated

        printf("\nYour sales tax is $%.2f", tax);//total sales tax is displayed

        total = tax + total;//grand total is calculated

        printf("\nYour grand total is $%.2f", total);//grand total is displayed
    }
    return 0;
}//end main function
#包括
//数组讨论代码
//销售税计算器
int main()
{
常量int maxItem=100;
//主函数初始化
int x,num;//整数值的初始化
浮动数组[maxItem]={0.0},总计=0,税=0;//数组、总计和税浮动的初始化
printf(“输入您所在州的销售税率(0.xx格式):”;//系统将提示用户输入其所在州的销售税率
scanf(“%f”,&tax);//用户输入其销售税率
printf(“\n输入您购买的商品数量(最多%d):”,maxItem);//提示用户输入他们购买的商品数量
scanf(“%d”,&num);//用户输入他们购买的产品数量
while(num>maxItem){
printf(“\n购买的商品数量最多只能为%d。请重新输入金额:”,maxItem);//提示用户购买的商品数量
scanf(“%d”,&num);//用户输入他们购买的产品数量
}
printf(“\n输入每个项目的价格:”);//提示用户开始输入每个项目的价格
对于(x=0;x maxItem){
printf(“\n购买的商品数量最多只能为%d。请重新输入金额:”,maxItem);//提示用户购买的商品数量
scanf(“%d”,&num);//用户输入他们购买的产品数量
}
printf(“\n输入每个项目的价格:”);//提示用户开始输入每个项目的价格

对于(x=0;x通常
scanf(“%f”和&array[num])
应该是
scanf(“%f”和&array[x])
。此外,在计算税收时,始终使用
total
而不是
array[x]
,这是对供应商或州政府(或两者)的巨大帮助。
wall-to-wall注释没有多大帮助:您应该在尾部注释之前留出一些空间(建议至少两个空间)。相比之下,基本缩进非常有用。它显示
total=0;
行在循环中,不需要(您可以将
total
初始化为零)。它表明,最终循环的每次迭代都会打印总计;不需要打印。您需要更仔细地计算和累积税款和总计-如果购买了多个项目,您将收取太多的税款。您应该检查每个输入-并且您可能应该验证税款。如果有人键入
9%
而不是你所期望的
0.09
,或者
35美元而不是你所期望的
35.00
,那么你将遇到重大问题。我认为你的代码的问题是你让用户选择他们用num购买多少物品。但是,你将数组长度限制为10。因此,如果我选择11,我的代码将在哪里是否要存储第11个项目?此外,对于循环,您始终使用num变量作为scanf的索引值。该变量假定为x,而不是num。否则,您只需处理该单个索引。
#include <stdio.h>
// Array Discussion Code
// Sales Tax Calculator
int main() 
{
    const int maxItem = 100;
    //main function initialization
    int x, num; //initialization of integer values
    float array[maxItem] = { 0.0 }, total = 0, tax = 0;//initialization of array, total, and tax floats

    printf("Enter the sales tax rate in your state (in 0.xx format): ");//user is prompted to enter their state sales tax rate
    scanf("%f", &tax);//user enters their sales tax rate
    printf("\nEnter the number of items you purchased(maximum %d): ", maxItem);//prompts the user for the number of items they bought
    scanf("%d", &num);//user inputs the number of products they bought

    while(num > maxItem){
      printf("\nNumber of item purchased can only be at maximum of %d. Please re-enter an amount: ", maxItem);//prompts the user for the number of items they bought
      scanf("%d", &num);//user inputs the number of products they bought
    }

    printf("\nEnter the price of each item: ");//prompts the user to begin entering the prices of each item

    for (x = 0; x<num; x++){//for loop to allow user to input the number of values they want
        printf("\nItem %d: ", x);
        scanf("%f", &array[x]);//user inputs a value
        total = total + array[x];
    }

    printf("\nYour total before tax is $%.2f", total); //program displays total before tax

    tax = tax * total;//total sales tax is calculated

    printf("\nYour sales tax is $%.2f", tax);//total sales tax is displayed

    total = tax + total;//grand total is calculated

    printf("\nYour grand total is $%.2f", total);//grand total is displayed

    return 0;
}//end main function
#include <stdio.h>
// Array Discussion Code
// Sales Tax Calculator
int main() 
{

    //main function initialization
    int x, num; //initialization of integer values
    float total = 0.0, tax = 0.0, item = 0.0;//initialization of array, total, and tax floats

    printf("Enter the sales tax rate in your state (in 0.xx format): ");//user is prompted to enter their state sales tax rate
    scanf("%f", &tax);//user enters their sales tax rate
    printf("\nEnter the number of items you purchased: ");//prompts the user for the number of items they bought
    scanf("%d", &num);//user inputs the number of products they bought

    printf("\nEnter the price of each item: ");//prompts the user to begin entering the prices of each item

    for (x = 1; x<=num; x++){//for loop to allow user to input the number of values they want
        printf("\nItem %d: ", x);
        scanf("%f", &item);//user inputs a value
        total += item;
    }


    printf("\nYour total before tax is $%.2f", total); //program displays total before tax

    tax = tax * total;//total sales tax is calculated

    printf("\nYour sales tax is $%.2f", tax);//total sales tax is displayed

    total = tax + total;//grand total is calculated

    printf("\nYour grand total is $%.2f", total);//grand total is displayed

    return 0;
}//end main function