每当我运行我的代码时,变量 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;
}