Question: Please help with my following code. It does not create a file at all, instead the stuff that I wanted it to print out in

Please help with my following code. It does not create a file at all, instead the stuff that I wanted it to print out in the displaySalesData function is only displayed in the usually output section when code is run but i want a text file to be created for the infomration to be stored there, please help. i used a little bit of infomration in the buy cars function so that the infromation worked out can be a[ended to the file, please help me to fix this. Here is the code:
These are the genral imports i have used in my code - #include // this is for input and output functions
#include // this is for the string operation strcpy
#include // this is required for bool data type
#include // this is for opening and closing files
#include
if (giveDiscount){
printf("Discount was given
");
printf("Your discount value is: %.2f
", carPrices[carIndex]* DISCOUNT_PERCENTAGE);
} else {
printf("Discount was not given
");
}
printf("Total Price: %.2f
", totalPrice);
// Update the car stock
// The carStock array is a global variable so it can be constantly updated
carStock[carIndex]-= carsNeeded;
printf("
The remaining stock for %s: %d
", carModels[carIndex], carStock[carIndex]);
// track the total sale statistics
// the number of sales per transaction
totalSales++;
// the number of cars sold
totalCarsSold += carsNeeded;
// the total price of the cars sold
totalProfit += totalPrice;
// total customers sold to
totalCustomers++;
// writes the sales data to the file, this is connected to view car sales data function
FILE *file = fopen("carSales.txt","a");
if (file == NULL){
printf("Error: Could not open sales data file.
");
return;
}
// writes the sale data which is (car model, units sold, price per unit) to the csv file
fprintf(file,"%s,%d,%.2f
", carModels[carIndex], carsNeeded, carPrices[carIndex]);
fclose(file);
numberOfSalesMade++;
}
void displayCarSalesData(){
printf("This is the current car stock -------------------------");
displayCarStock();
FILE *file = fopen("carSales.txt","r");
if (file == NULL){
printf("Error: Could not open sales data file.
");
}
char data[100];
while(fgets(data, sizeof(data), file)){
printf("This is all the previous sales made ");
printf("Model and Profit -%s
", data);
}
printf("Here is your sales summary!---------------------------------
");
printf("The total number of sales is %d
", totalSales);
printf("The total number of cars sold is: %d, therefore the total profit is %.2f GBP
", totalCarsSold, totalProfit);
printf("The number of customers served is: %d
", totalCustomers);
printf("The total price of discounts given is: %.2f
", totalDiscounts);
fclose(file);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!