I am posting my complete code, so that anyone who wishes to comment may do
so.
#include <stdio.h>
#include <stdlib.h>
/* Features to add */
/* 1. Add password protection */
/* 2. Have multiple data files*/
/* 3. Save/Delete data files. (remove function)*/
/* 4. Create usage log of time of use*/
/* 5. Output to a printer */
typedef struct Vehicle
{
unsigned int Vehicle_Number;
unsigned int Vehicle_Purchase_Cost;
unsigned int Vehicle_Sale_Price;
}Vehicle;
/* BEGIN FUNCTIONS DECLARATIONS*/
void Vehicle_With_Highest_Sale_Price(void);
void Average_Cost_Of_Vehicles_In_Stock(void);
void Gross_Profit_If_All_Cars_Are_Sold(void);
unsigned int ReadDataFromFile(void); /* Function will return current index
number*/
void SaveDataToFile(void);
/* END FUNCTIONS DECLARATIONS*/
#define MAX_ARRAY_SIZE 10
#define FALSE 0
#define TRUE 1
/* BEGIN VARIABLE DECLARATIONS*/
int NEW_DATA_FILE=0;
unsigned int Index=0;
unsigned int n;
char temp_value[10];
int keyp;
int MAX_HAS_BEEN_REACHED=FALSE;
Vehicle VehicleArray[MAX_ARRAY_SIZE];
float AverageCarCost; /* Holds average cost of all cars */
unsigned int GrossProfit; /* Holds gross profit of all cars
*/
unsigned int HighestCarNumber; /* Holds car number with highest sale price
*/
unsigned int HighestPrice = 0; /* Holds highest price of all cars
*/
/* END VARIABLE DECLARATIONS*/
int main(void)
{
Index = ReadDataFromFile();
printf("Index returned from function = %d\n",Index);
if((Index==0)&&(NEW_DATA_FILE==1))Index=1;/* File just created, so set
Index to 1*/
do
{
if(MAX_HAS_BEEN_REACHED==FALSE)
{
do{
/* Will flush the input stream from the fgetc call at end of
loop*/
fflush(stdin);
printf("Car Number: %u\n", Index);
printf("\nCar Purchase Price: ");
fflush(stdout);
if(NULL==fgets(temp_value, sizeof temp_value,stdin))
printf("INPUT ERROR: Could not read line.\n");
else
sscanf(temp_value, "%u",
&VehicleArray[Index].Vehicle_Purchase_Cost);
printf("\nCar Sale Price: ");
fflush(stdout);
if(NULL==fgets(temp_value, sizeof temp_value,stdin))
printf("INPUT ERROR: Could not read line.\n");
else
sscanf(temp_value, "%u",
&VehicleArray[Index].Vehicle_Sale_Price);
if (VehicleArray[Index].Vehicle_Purchase_Cost >=
VehicleArray[Index].Vehicle_Sale_Price)
printf("\nThe selling cost of $%u must be greater than
the purchase cost of $%u",
VehicleArray[Index].Vehicle_Sale_Price,
VehicleArray[Index].Vehicle_Purchase_Cost);
}while (VehicleArray[Index].Vehicle_Purchase_Cost >
VehicleArray[Index].Vehicle_Sale_Price);
}/* END MAX REACHED IF*/
printf("Car Number \t Car Cost \t Car Sell\n");
printf("------------------------------------------------\n");
for(n=1; n<=Index; n++)
printf("%u \t\t $%u \t\t $%u \n",
n, VehicleArray[n].Vehicle_Purchase_Cost,
VehicleArray[n].Vehicle_Sale_Price);
Vehicle_With_Highest_Sale_Price();
Average_Cost_Of_Vehicles_In_Stock();
Gross_Profit_If_All_Cars_Are_Sold();
printf("\n\n Highest Selling Price: %u $%u \n",
HighestCarNumber, HighestPrice);
printf(" Average cost of all cars: $%2.2f \n", AverageCarCost);
printf(" Gross profit from all cars: $%u \n", GrossProfit);
Index++;
printf("\n\nDo You Wish To Enter Another Car?(Y/N)");
fflush(stdout);
keyp = fgetc(stdin);
if(MAX_HAS_BEEN_REACHED == TRUE)
printf("The array is full.\n");
}while (((keyp == 'Y') || (keyp == 'y')) && (Index <= MAX_ARRAY_SIZE));
SaveDataToFile();
return 0;
}
void Vehicle_With_Highest_Sale_Price(void)
{
for(n=1; n<=Index; n++)
{
if (VehicleArray[n].Vehicle_Sale_Price > HighestPrice)
{
HighestPrice = VehicleArray[n].Vehicle_Sale_Price;
HighestCarNumber = n;
}
}
}
void Average_Cost_Of_Vehicles_In_Stock(void)
{
float Cost=0;
for(n=1; n<=Index; n++)
Cost += VehicleArray[n].Vehicle_Purchase_Cost;
AverageCarCost = Cost / (Index? Index: 1);
}
void Gross_Profit_If_All_Cars_Are_Sold(void)
{
unsigned int TotalOfVehiclePurchases=0;
unsigned int TotalOfVehicleSales=0;
for(n=1; n<=Index; n++)
TotalOfVehiclePurchases += VehicleArray[n].Vehicle_Purchase_Cost;
for(n=1; n<=Index; n++)
TotalOfVehicleSales += VehicleArray[n].Vehicle_Sale_Price;
printf("\n Sales: %u Purchases: %u", TotalOfVehicleSales,
TotalOfVehiclePurchases);
GrossProfit = TotalOfVehicleSales - TotalOfVehiclePurchases;
}
unsigned int ReadDataFromFile(void)
{
FILE *CarDataFile;
printf("Reading Car Data File from disk\nPatience would be
appreciated...");
if((CarDataFile = fopen("CAR_DATA.DAT","r"))==NULL)
{
printf("\nERROR: File probably does not exist in directory.");
/* Attempt to create the data file*/
if((CarDataFile = fopen("CAR_DATA.DAT","a"))==NULL)
{
printf("\nWe cannot create file.\n Program will now end.");
exit(EXIT_FAILURE);
}
else
{
printf("\nData file has been created.");
NEW_DATA_FILE=1;
fclose(CarDataFile);
}
}
else /* Data file has been successfully opened*/
{
printf("\n\nFile opened for reading...\n\n");
if(NEW_DATA_FILE != 1)/* File was NOT just created, so we can read from
it*/
{
Index=1; /* Sets array up from zero for compatibility with the rest of
program*/
/*while (!feof(CarDataFile))
{
fscanf(CarDataFile, "%u %u %u \n",
&VehicleArray[Index].Vehicle_Number,
&VehicleArray[Index].Vehicle_Purchase_Cost,
&VehicleArray[Index].Vehicle_Sale_Price);
Index++;
if(Index==MAX_ARRAY_SIZE)
{
printf("\nERROR: Maximum number of values have been read from
file.\n");
MAX_HAS_BEEN_REACHED=TRUE;
break;
}
}*/
while (fscanf(CarDataFile, "%u %u %u \n",
&VehicleArray[Index].Vehicle_Number,
&VehicleArray[Index].Vehicle_Purchase_Cost,
&VehicleArray[Index].Vehicle_Sale_Price)==3)
{
Index++;
if(Index==MAX_ARRAY_SIZE)
{
printf("ERROR: Maximum num values read from file.\n");
MAX_HAS_BEEN_REACHED=TRUE;
break;
}
}
}
fclose(CarDataFile);
}
return Index;
}/* End Function*/
void SaveDataToFile(void)
{
FILE *CarDataFile;
if ((CarDataFile = fopen("CAR_DATA.DAT","w+"))==NULL)
printf("\nSeems we can't open the data file for writing.");
else
{
for (n=1; n<=(Index-1);n++)
{
fprintf(CarDataFile, "%u %u %u \n",
VehicleArray[n].Vehicle_Number,
VehicleArray[n].Vehicle_Purchase_Cost,
VehicleArray[n].Vehicle_Sale_Price);
}
fclose(CarDataFile);
}
}