Hello everyone,

I'm currently in the midst of writing an inventory program for a Car database using File I/O and structures. I have six functions that will be used for

  1. Displaying a list of all car records
  2. Adding a new record
  3. Modifying an existing record
  4. Showing sales and profits (including total profits for all cars)
  5. Sorting the list in alphabetical order (based on Company Name)
  6. Quitting from the program


I have been successful with the first 3 functions but the last two - 5. and 6. are giving me quite a dilemma (I don't consider quitting from the program to really need a function but nevertheless it has it's own place). I've tried brainstorming ideas about how to go about finding the total profit for ALL cars but I just can't seem to get the logic behind it to work! No matter what I've tried it just displays the last car entry and that profit. I also have the 5th function to write and I would normally the car entires from the file and use Bubble-sort to arrange them in descending order. Would I be able to read the car entries into an array and then sort them? Any and all brainstorming would be much appreciated!

Below you will find my code, I'm sorry for the lack of commenting especially in a fairly large program.

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

struct car
{
    char company[30];
    char model[10];
    char colour[10];
    float ucost;
    float sellprice;
    float totalval;
    int stock;
    int numofcarsold;
    float profit;

    float totalprofit;
    float oldprofit;
};


int menu(void);
void list(void);
void add_record(void);
void modify_record(void);
void sales_and_profits(void);

void quit(void);

#define N 10

int main()
{
    struct car cars[N];
    char choice;

    do
    {
        choice=menu();
    
        switch(choice)
        {
            case 1:    list();
                break;
            case 2:    add_record();
                break;
            case 3:    modify_record();
                break;
            case 4:    sales_and_profits();
                break;
            case 5:    /*option 5 for sorting by alphabetical order to be added later*/
                break;
            case 6:    quit();
                break;
        }
    }
    while(choice != 6);        /*can fix this up to make it cleaner later*/

    return 0;
}

int menu (void)
{
    int userchoice = 0;

    system("cls");
    printf("\t\t\t     Car Sales Inventory\n\n\n\n");
    printf("\t\t1.\tDisplay the list of all car records\n");
    printf("\t\t2.\tAdd a new car record\n");
    printf("\t\t3.\tModify an existing car record\n");
    printf("\t\t4.\tShow sales and profits\n");
    printf("\t\t5.\tSort the list of cars\n");
    printf("\t\t6.\tExit");

    printf("\n\n\t\tPlease select your choice ==> ");
    fflush(stdin);
    scanf("%d",&userchoice);

    return userchoice;
}

void list(void)
{
    FILE *fp;
    struct car list;
    int i=0;

    if ((fp = fopen("H:\\SummerProject.txt","rb")) == NULL) 
    {
        system("cls");
        printf("\n\n\n\n\n\n\n\n\n\n\n\t\t      There are currently no car records!\n\n");
        printf("\t\t    Please choose option 2. to add a record.");
        getch();
        exit (0);
    }

    system("cls");

    printf("Company\tModel\tColour\tCost\tPrice\tValue\tStock\tSold\tProfit\n\n");

    while(fread(&list,sizeof(struct car),1,fp) == 1)
    {
        printf("%s\t%s\t%s\t%.2f\t%.2f\t%.2f\t%d\t%d\t%.2f\n",list.company,list.model,list.colour,list.ucost,list.sellprice,list.totalval,list.stock,list.numofcarsold,list.profit);
    }

    fclose(fp);
    getch();
}

void add_record(void)
{
    FILE *fp;
    struct car addcar;

    fp = fopen("H:\\SummerProject.txt","ab");
    
    system("cls");

    printf("Enter the Company name: ");
    fflush(stdin);
    scanf("%s",addcar.company);

    printf("\nEnter the Car model: ");
    fflush(stdin);
    scanf("%s",addcar.model);

    printf("\nEnter the Car colour: ");
    fflush(stdin);
    scanf("%s",addcar.colour);

    printf("\nEnter the Unit cost: ");
    fflush(stdin);
    scanf("%f",&addcar.ucost);

    printf("\nEnter the Sell price: ");
    fflush(stdin);
    scanf("%f",&addcar.sellprice);

    addcar.totalval = (addcar.sellprice - addcar.ucost);

    printf("\nEnter the Stock of the car: ");
    fflush(stdin);
    scanf("%d",&addcar.stock);

    printf("\nEnter the number of cars sold: ");
    fflush(stdin);
    scanf("%d",&addcar.numofcarsold);

    addcar.profit = (addcar.sellprice * addcar.numofcarsold);

    fwrite(&addcar,sizeof(struct car),1,fp);

    fclose(fp);
}

void modify_record(void)
{
    FILE *fp;
    int line_number = 0;
    struct car modifycar;

    system("cls");

    if ((fp = fopen("H:\\SummerProject.txt","r+b")) == NULL) 
    {
        system("cls");
        printf("\n\n\n\n\n\n\n\n\n\n\n\t\t  There are currently no car records to modify!\n\n");
        printf("\t\t    Please choose option 2. to add a record.");
        getch();
        exit (0);
    }

    printf("Which record would you like to modify? ");
    scanf("%d",&line_number);

    printf("\nEnter the new Company name: ");
    fflush(stdin);
    scanf("%s",modifycar.company);

    printf("\nEnter the new Car model: ");
    fflush(stdin);
    scanf("%s",modifycar.model);

    printf("\nEnter the new Car colour: ");
    fflush(stdin);
    scanf("%s",modifycar.colour);

    printf("\nEnter the new Unit cost: ");
    fflush(stdin);
    scanf("%f",&modifycar.ucost);

    printf("\nEnter the new Sell price: ");
    fflush(stdin);
    scanf("%f",&modifycar.sellprice);

    printf("\nEnter the new Total value: ");
    fflush(stdin);
    scanf("%f",&modifycar.totalval);

    printf("\nEnter the new Stock of the car: ");
    fflush(stdin);
    scanf("%d",&modifycar.stock);

    printf("\nEnter the new number of cars sold: ");
    fflush(stdin);
    scanf("%d",&modifycar.numofcarsold);

    if(modifycar.profit < 0)
    {
        modifycar.profit = 0.0;
    }
    else
    {
        modifycar.profit = ((modifycar.sellprice * modifycar.numofcarsold) - modifycar.totalval);
    }

    fseek(fp,((sizeof(struct car))*(line_number -1)),0);
    fwrite(&modifycar,sizeof(struct car),1,fp);

    fclose(fp);
}

void sales_and_profits(void)
{
    FILE *fp;
    struct car showcarinfo;
    int i, lines = 0;
    char c;

    system("cls");

    if ((fp = fopen("H:\\SummerProject.txt","r+b")) == NULL)
    {
        system("cls");
        printf("\n\n\n\n\n\n\n\n\n\n\n\t       There are currently no car records to show profit for!\n\n");
        printf("\t\t    Please choose option 2. to add a record.");
        getch();
        exit (0);
    }

    printf("Car Model\tNo. of Car\tCar Sold\tPrice\t\tProfit\n");
    while(fread(&showcarinfo,sizeof(struct car),1,fp) == 1)
    {
        showcarinfo.totalprofit = showcarinfo.totalprofit + showcarinfo.profit;        /*whilst printing the information below, calculate the totalprofit for all cars by repeatedly reading
                                                                                      and adding that float value to itself to accumulate the total value*/
        printf("\n%s\t\t%d\t\t%d\t\t%.2f\t\t%.2f", showcarinfo.model, showcarinfo.stock, showcarinfo.numofcarsold, showcarinfo.sellprice, showcarinfo.profit);
    }
    printf("\n\nThe total profit for all cars is: $%.2f.", showcarinfo.totalprofit);    /*prints the total profit for ALL cars*/

    getch();

    fclose(fp);
}

void quit(void)
{
    system("cls");
    printf("\n\n\n\n\n\n\n\n\n\n\t            Thank you for using my Car Sales Program!");
    getch();
}