4

每当我运行我的代码时,变量 bookcost 都会自行更改为值: 3435973836 。

它在到达 processingData 函数后执行此操作。我不知道为什么它一直这样做!我已经写了这个没有函数的程序,它运行良好:(这是我的第二节编程课,它是在线的,几乎没有教授的支持。

#include <stdio.h>

void inputData(int* inputs,int* booknmbr, int* nmbrpurchased, int* bookcost);
void processingData(int bookcost, int nmbrpurchased, int totalpurch, int costaccu);
void outputInfo(int booknmbr, int nmbrpurchased, int bookcost, int* total);

    int bookcounter = 0;
    int totalpurch = 0;
    int costaccu = 0;
    int totalcostaccu = 0;


int main ()

{
    int booknmbr;
    int nmbrpurchased;
    int bookcost; 
    int bookcounter = 0;
    int cycleRun;
    int total;
    int inputs;

    printf("Run Program? 1 for Yes or -1 for No \n"); // ask user to input product name
    scanf("%d", &cycleRun);
    while (cycleRun != -1) 
    {

        inputData(&inputs, &booknmbr, &nmbrpurchased, &bookcost);
        processingData(bookcost, nmbrpurchased, totalpurch, costaccu);
        outputInfo(booknmbr, nmbrpurchased, bookcost, &total);

        printf("Run Program? 1 for Yes or -1 for No \n"); // ask user to input product name
        scanf("%d", &cycleRun);
    }
}

void inputData(int* inputs, int* booknmbr, int* nmbrpurchased, int* bookcost)
{
    printf( "\nEnter the Book Product Number?\n" );
    scanf("%d", &booknmbr);

    printf( "Enter the Number of Books Purchased?\n" );
    scanf("%d", &nmbrpurchased);

    printf( "Enter the Cost of the Book Purchased?\n");
    scanf("%d", &bookcost);

    printf( "TEST: %u\n", bookcost);

    return;
}

void processingData(int bookcost, int nmbrpurchased, int totalpurch, int costaccu)
{
    int total;

    printf( "TEST: %u\n", bookcost);

    total = bookcost * nmbrpurchased;
    totalpurch = totalpurch + nmbrpurchased;
    costaccu = costaccu + bookcost;
    totalcostaccu = totalcostaccu + (bookcost * nmbrpurchased);

    printf( "TEST: %u\n", bookcost);

    return;
}

void outputInfo(int booknmbr, int nmbrpurchased, int bookcost, int* total)
{
    printf( "\nBook Product number entered is: %u\n", booknmbr);
    printf( "Quantity of Book Purchased is: %u\n", nmbrpurchased);
    printf( "Cost of the Book Purchased is: %u\n", bookcost);
    printf( "Total cost of books is: $%u\n", total);

    return;
}

void outputSummary(int bookcounter, int totalpurch, int costaccu, int totalcostaccu)
{
    printf( "\n\nNumber of records processed = %u\n", bookcounter);
    printf( "Number of books purchased = %u\n", totalpurch);
    printf( "Cost of the books purchased = $%u\n", costaccu);
    printf( "Total cost for all book purchases = $%u\n", totalcostaccu);

    return;
}
4

2 回答 2

5

快速搜索显示 3435973836 是 Windows 在 x64 上提供未分配内存的值 - 这很能说明问题,因为它表明您有指向无处的指针。

让我们看看这里发生了什么。在main()中,您定义了一堆整数。当您说 时,您传递对这些整数的引用inputData(&inputs, &booknmbr, &nmbrpurchased, &bookcost)。该函数的签名为void inputData(int* inputs, int* booknmbr, int* nmbrpurchased, int* bookcost)- 到目前为止非常好。

……那你scanf("%d", &booknmbr)。想一想。

这是正确的。您正在传递scanf()对指向要填充的整数的指针的引用,而不仅仅是指针。这是一个容易掉入的陷阱,因为您可能总是以scanf()这种方式编写调用并且这已成为习惯。将其替换为 ascanf("%d", booknmbr)会使世界再次正确。

于 2012-10-06T01:55:49.763 回答
2

您将变量的地址inputData传递给...试试这个:

/* not using inputs? */
void inputData(int* pinputs, int* pbooknmbr, int* pnmbrpurchased, int* pbookcost)  {
    printf( "\nEnter the Book Product Number?\n" );
    scanf("%d", pbooknmbr);
    printf( "Enter the Number of Books Purchased?\n" );
    scanf("%d", pnmbrpurchased);
    printf( "Enter the Cost of the Book Purchased?\n");
    scanf("%d", pbookcost);
    printf( "TEST: %u\n", *pbookcost);
} 

你有一个类似的问题totalin outputInfo。您需要仔细阅读您的代码并注意细节,注意哪些变量是指针,哪些是标量值。就像我在这里所做的那样,以不同的方式命名指针会有所帮助。

于 2012-10-06T01:50:41.997 回答