Whenever I run my code the variable bookcost changes itself to the value: 3435973836 .
It does this after it reaches the processingData function. I do not know why it keeps doing this! I have already written this program without functions and it worked fine 🙁 This is my second programming class, it is online and there is almost no support from the professor.
#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;
}
You’re passing the addresses of your variables to
inputData… try this:You have a similar problem with
totalinoutputInfo. You need to read carefully though your code and attend to details, paying attention to which variables are pointers and which are scalar values. It helps to name pointers differently, as I did here.