【问题标题】:Can't run on Dev C++无法在 Dev C++ 上运行
【发布时间】:2021-02-25 13:49:17
【问题描述】:

我的代码需要帮助。我的代码是 C 源代码格式,但它不能在 Dev C++ 软件上运行。我和我的队友检查了几次,我的代码没有错误,但它就是无法运行。有人,请帮助我。谢谢。

#include<stdio.h>
#include<stdlib.h>
#define TAX 0.08

void getInput(struct order myOrder[], int order);
void processOrder(struct order myOrder[], int order);

struct order
{
    char productID[6];
    float unitPrice, grossTotal, netTotal, serviceTax;
    int qtyOrdered;
};

main() 
{
    struct order myOrder[50];
    int order;
    printf("ORDER PROCESSING\n");
    printf("=================\n");
    printf("How many orders today ? ");
    scanf("%d", &order);
    getInput(myOrder, order);
    processOrder(myOrder, order);
    system("PAUSE");
}

void getInput(struct order myOrder[], int order)
{
    for (int i = 0;i < order;i++)
    {
        printf("Enter productID :");
        scanf("%s", &myOrder[i].productID);
        printf("Enter unit price : RM ");
        scanf("%f", &myOrder[i].unitPrice);
        printf("Enter quantity ordered : ");
        scanf("%d", &myOrder[i].qtyOrdered);
        printf("\n");
    }
}

void processOrder(struct order myOrder[], int order)
{
    float sum = 0.0;
    for (int i = 0;i < order;i++)
    {
        printf("Order : %s\n", myOrder[i].productID);
        printf("Unit Price : RM %.2f\n", myOrder[i].unitPrice);
        printf("Qty Ordered : %d\n", myOrder[i].qtyOrdered);
        myOrder[i].grossTotal = myOrder[i].unitPrice * myOrder[i].qtyOrdered;
        printf("Gross Revenue : RM %.2f\n", myOrder[i].grossTotal);
        myOrder[i].serviceTax = myOrder[i].grossTotal * TAX;
        printf("Service Tax : RM %.2f\n", myOrder[i].serviceTax);
        myOrder[i].netTotal = myOrder[i].grossTotal - myOrder[i].serviceTax;
        printf(": : Net Revenue : RM%.2f\n\n", myOrder[i].netTotal);
        sum += myOrder[i].netTotal;
    }
    printf("* TOTAL INCOME EARN :: RM%.2f", sum);
}

【问题讨论】:

  • “就是不能跑”是什么意思?
  • C程序从上到下编译执行。以您编写代码的方式。所以你不能引用你还没有写或打算写得更远的东西。

标签: c dev-c++


【解决方案1】:

struct order 的声明应该在使用之前。

错误:

void getInput(struct order myOrder[], int order);
void processOrder(struct order myOrder[], int order);

struct order
{
    char productID[6];
    float unitPrice, grossTotal, netTotal, serviceTax;
    int qtyOrdered;
};

正确:

struct order
{
    char productID[6];
    float unitPrice, grossTotal, netTotal, serviceTax;
    int qtyOrdered;
};

void getInput(struct order myOrder[], int order);
void processOrder(struct order myOrder[], int order);

scanf("%s", &amp;myOrder[i].productID); 也是错误的。 char*(指向字符的指针)应该传递给%s,而不是char(*)[6](指向整个数组的指针)。 &amp; 应该被删除。

【讨论】:

    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多