Thread: Inventory Help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Inventory Help

    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();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your car struct, you have things that are not intrinsic to any one car. Those should be removed from the struct, and handled as either another struct, or as a separate variable: numofcarsold (a sum is it not?), newprofit, oldprofit. Summary cost and profit variables should be local variables inside the sales_and_ profit function.

    Your do while() and switch statement in main() should go inside your menu function - that's where you loop around and around.

    fflush(stdin) doesn't work - fflush() works on OUTWARD streams, not input streams. You're trying to flush your kitchen faucet like a toilet! Delete them all. You will manage your input buffer, directly, using spaces scanf("%d ", &choice), instead of scanf("%d", &choice). The space after the d makes a big difference.

    Your menu is returning an int, and you're main() loop is expecting a char, so that is certainly a problem.

    #defines should go right below the last #include file line of code

    Why not use a text based program? Binary makes it more difficult, and in a program like this, adds nothing that I can see for a benefit.

    To sort char arrays, you need to use strcmp(). For such a small array, a bubble sort is fine.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    My apologies for leaving out some information. The "numberofcarsold" member of my struct car is required - you could overwrite if more cars of a specific type is sold, but it is rather inconvienent to re-enter the entire record (I'll look into asking the user if they want to modify a specific entry later on as it is extra). I will change the fflush() - I wasn't aware that it didn't work with input streams! Didn't see that I was returning an integer to a char but that's a simple fix! I am still having some difficulty trying to figure out how to logically go about accumulating the total profit for all cars. Whilst writing the entire structure to the file (after calculating the profit for the respective record), I could add each profit to a new structure member called
    Code:
    float totalprofitforallcars;
    I was thinking next that if I were to modify an existing value then the profit would more than likely change - something I have to take into account so I would also place each car profit (at the time of being calculated) into another structure member called
    Code:
    float old profit;
    However I have had no luck with getting this to work - I thought static variables would somehow help but nothing happened there. I can get it to accumulate the total profit for all cars with creating some static variables (ONLY when a new file is created and the records are entered one by one) but as you know when the program exits those values are lost. I guess I should try and also write that total profit for all cars when the file is firstly created and written to.

    EDIT: Got the sales_and_profits() function working!

    Code:
    void sales_and_profits(void)
    {
    	FILE *fp;
    	int i = 0;
    	struct car showcarinfo;
    	float totalprofitforallcars = 0.0;
    
    	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)
    	{
    		totalprofitforallcars = totalprofitforallcars + showcarinfo.profit;
    		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);
    		i++;
    	}
    	printf("\n\nThe total profit for all cars is: $%.2f.", totalprofitforallcars);	/*prints the total profit for ALL cars*/
    
    	getch();
    
    	fclose(fp);
    }
    Now the last one shouldn't be too hard!

    Quote Originally Posted by Adak View Post
    In your car struct, you have things that are not intrinsic to any one car. Those should be removed from the struct, and handled as either another struct, or as a separate variable: numofcarsold (a sum is it not?), newprofit, oldprofit. Summary cost and profit variables should be local variables inside the sales_and_ profit function.

    Your do while() and switch statement in main() should go inside your menu function - that's where you loop around and around.

    fflush(stdin) doesn't work - fflush() works on OUTWARD streams, not input streams. You're trying to flush your kitchen faucet like a toilet! Delete them all. You will manage your input buffer, directly, using spaces scanf("%d ", &choice), instead of scanf("%d", &choice). The space after the d makes a big difference.

    Your menu is returning an int, and you're main() loop is expecting a char, so that is certainly a problem.

    #defines should go right below the last #include file line of code

    Why not use a text based program? Binary makes it more difficult, and in a program like this, adds nothing that I can see for a benefit.

    To sort char arrays, you need to use strcmp(). For such a small array, a bubble sort is fine.
    Last edited by Fekore; 07-28-2012 at 09:25 PM. Reason: Typo's

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    So for my function that sorts the cars by Company Name, I have used fscanf() to read the names and input them into a two dimensional array and then proceed to use bubblesort on them. My problem is that I'm only printing out the first Company Name (in this case it's a test record with the company name as "J") and it isn't printing anymore. I have a feeling it's with fscanf because since my data is stored in a structure I'll need to properly position my file point after every record to gain access to each Company Name. How would I go about doing that? Would using a for() loop with fseek() (to position the file pointer to each record and then copy the Company name into an array for sorting) be suitable?

    Code:
    Code:
    void sort_car_list(void)
    {
    	FILE *fp;
    	int i = 0, j, last;
    	char names[50][15];
    	char temp[15];
    
    	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 sort!\n\n");
    		printf("\t\t    Please choose option 2. to add records.");
    		getch();
    		exit (0);
    	}
    	
    	for (i=0; !feof(fp); i++)
    	{
    		fscanf(fp, "%s", names[i]);
    	}
    
    	last = i - 1;
    
    	fclose(fp);
    
    	for (i = 0 ; i < last ; i++)
    	{
    		for (j = i+1 ; j <= i ; j++)
    		{
    			if (strcmp(names[i],names[j - 1]) > 0)
    			{
    				strcpy(temp,names[j]) ;
    				strcpy(names[j],names[j - 1]) ;
    				strcpy(names[j - 1],temp) ;
    			}
    		}
    	}
    
    	printf("The Company names sorted are:\n");
    	for (i = 0 ; i <= last ; i++)
    	{
    		printf("\n%s",names[i]);
    	}
    
    	getch();
    }
    Last edited by Fekore; 07-28-2012 at 10:40 PM. Reason: Adding code

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Structs are the C version of "object" programming. A struct for a student, for instance might typically include:

    first name, last name, middle initial, id number, major, gpa, advisor, emergency telephone number, personal mobile phone number.

    It would NOT have a struct member for average height of the students in the school - since that is not a part of any student "object", but belongs to the whole school's student body data.

    I'll post up a little example for you, in about 20 mins.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Amazing discovery! I can code much better when I'm awake!

    A little example program:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define SIZE 5
    
    struct video {
       char title[80];
       int rating;
    };
    
    void menu(struct video videos[SIZE]);
    void sortIt(struct video videos[SIZE], int keyNum);
    void printIt(struct video videos[SIZE]);
    
    int main(void) {
       struct video videos[SIZE];
       char intake[150];
       FILE *fp;
       int i,j;
       if((fp=fopen("videoDB.txt", "rt"))==NULL) {
          printf("Error! File Not Found\n");
          return 1;
       }
       i=0;
       while((fgets(intake, 149, fp))!= NULL) {
          //printf("%s",intake); getchar();
          //printf("%s j: %d*\n", intake, j); getchar();
          j=strlen(intake);
          while(intake[j] != ' ') 
             --j;
          
          intake[j++]='\0';
          //printf("%s*\n",intake); getchar();
          strcpy(videos[i].title, intake);
          sscanf(intake+j,"%d ", &videos[i].rating);
          printf("%s %d\n",videos[i].title, videos[i].rating); 
          
          ++i;
       }
       fclose(fp);
     
       menu(videos);
    
       printf("\n");
       return 0;
    }
    void menu(struct video videos[SIZE]) {
       int choice;
       do {
          printf("\n   1. Display All Videos (n/a)          2. Search For a Video (n/a)\n"\
                 "\n   3. Sort Videos By Title              4. Quit\n");
    
       
          printf("\nEnter your selection: ");
          scanf("%d", &choice);
          printf("\n");
          switch(choice) {
             case 1: printIt(videos);
                     break;
             case 2: printf("Choice not available yet\n");
                     break;
             case 3: sortIt(videos, choice);
                     printIt(videos);
                     break;
             case 4: printf("Goodbye\n\n");
                     break;
             default: printf("try again, please\n");
          }
       }while(choice != 4);
    }
    
    /* later on, keyNum could be used to direct the sort to the proper struct member for that sort */
    void sortIt(struct video videos[SIZE], int keyNum){  //this is insertion sort
       int i,j;
       struct video temp;
       for(i=1;i<SIZE;i++) {
          j=i;
          while(j>0 && strcmp(videos[j].title, videos[j-1].title)< 0) {
             temp = videos[j];
             videos[j] = videos[j-1];
             videos[j-1] = temp;
             --j;
          }
       }
    }
    
    void printIt(struct video videos[SIZE]) {
       int i;
       printf("\n        Title                   Rating\n"\
              "================================================\n");
       for(i=0;i<SIZE;i++) 
          printf("%3d) %-28s %2d\n",i+1,videos[i].title,videos[i].rating);
    }

    Where videoDB.txt file consisted of:
    Code:
    The Ten Commandments 5
    Shooter 4
    Click 1
    The Bourne Identity 4
    Taken 4

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    I completely understand your rationale about some members of my structure not being relevant - I'll be having a chat with my professor about it when I can. Thank you for the example program it certainly got me thinking about my last function and other parts of my code I should polish up. I'm still having a little trouble.. I'm trying to read and write my car records from the existing file into an array of structures of type car - this way I can use strcmp() and Bubblesort. After reading the records from my file I should be able to sort and then print based on descending Company name but it isn't working. I'm going to plug in a few printfs to see if i'm actually reading the records from my file and storing them accordingly but in the meantime here's my code:

    Code:
    void sort_car_list(void)
    {
    	FILE *fp;
    	int i = 0, j, counter = 0;
    	struct car readcar;
    	struct car sortcars[SIZE];
    	struct car temp = {};
    
    	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 sort!\n\n");
    		printf("\t\t    Please choose option 2. to add records.");
    		getch();
    		exit (0);
    	}
    
    	while(fread(&readcar,sizeof(struct car),1,fp) == 1)
    	{
    		sortcars[i] = readcar;
    		i++;
    		counter++;
    	}
    
    	system("cls");
    
    	for (i = 0; i < counter; i)
    	{
    		for (j=i+1; j<=counter; j++)
    		{
    			if(strcmp(sortcars[i].company, sortcars[j].company) > 0)
    			{
    				sortcars[i] = sortcars[j];
    				sortcars[j] = temp;
    				temp = sortcars[i];
    			}
    		}
    	}
    
    	printf("The cars sorted in alphabetical order are:\n");
    	for(i=0; i<counter; i++)
    	{
    		printf("%d\t%s\n", (i+1), sortcars[i].company);
    	}
    
    	fclose(fp);
    }
    Quote Originally Posted by Adak View Post
    Amazing discovery! I can code much better when I'm awake!

    A little example program:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define SIZE 5
    
    struct video {
       char title[80];
       int rating;
    };
    
    void menu(struct video videos[SIZE]);
    void sortIt(struct video videos[SIZE], int keyNum);
    void printIt(struct video videos[SIZE]);
    
    int main(void) {
       struct video videos[SIZE];
       char intake[150];
       FILE *fp;
       int i,j;
       if((fp=fopen("videoDB.txt", "rt"))==NULL) {
          printf("Error! File Not Found\n");
          return 1;
       }
       i=0;
       while((fgets(intake, 149, fp))!= NULL) {
          //printf("%s",intake); getchar();
          //printf("%s j: %d*\n", intake, j); getchar();
          j=strlen(intake);
          while(intake[j] != ' ') 
             --j;
          
          intake[j++]='\0';
          //printf("%s*\n",intake); getchar();
          strcpy(videos[i].title, intake);
          sscanf(intake+j,"%d ", &videos[i].rating);
          printf("%s %d\n",videos[i].title, videos[i].rating); 
          
          ++i;
       }
       fclose(fp);
     
       menu(videos);
    
       printf("\n");
       return 0;
    }
    void menu(struct video videos[SIZE]) {
       int choice;
       do {
          printf("\n   1. Display All Videos (n/a)          2. Search For a Video (n/a)\n"\
                 "\n   3. Sort Videos By Title              4. Quit\n");
    
       
          printf("\nEnter your selection: ");
          scanf("%d", &choice);
          printf("\n");
          switch(choice) {
             case 1: printIt(videos);
                     break;
             case 2: printf("Choice not available yet\n");
                     break;
             case 3: sortIt(videos, choice);
                     printIt(videos);
                     break;
             case 4: printf("Goodbye\n\n");
                     break;
             default: printf("try again, please\n");
          }
       }while(choice != 4);
    }
    
    /* later on, keyNum could be used to direct the sort to the proper struct member for that sort */
    void sortIt(struct video videos[SIZE], int keyNum){  //this is insertion sort
       int i,j;
       struct video temp;
       for(i=1;i<SIZE;i++) {
          j=i;
          while(j>0 && strcmp(videos[j].title, videos[j-1].title)< 0) {
             temp = videos[j];
             videos[j] = videos[j-1];
             videos[j-1] = temp;
             --j;
          }
       }
    }
    
    void printIt(struct video videos[SIZE]) {
       int i;
       printf("\n        Title                   Rating\n"\
              "================================================\n");
       for(i=0;i<SIZE;i++) 
          printf("%3d) %-28s %2d\n",i+1,videos[i].title,videos[i].rating);
    }

    Where videoDB.txt file consisted of:
    Code:
    The Ten Commandments 5
    Shooter 4
    Click 1
    The Bourne Identity 4
    Taken 4

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your swap code is entirely wacked!

    Code:
        for (i = 0; i < counter; i)
        {
            for (j=i+1; j<=counter; j++)
            {
                if(strcmp(sortcars[i].company, sortcars[j].company) > 0)
                {
                   /* this section is nuts! */
                    sortcars[i] = sortcars[j];
                    sortcars[j] = temp;
                    temp = sortcars[i];
                   /* re-do it. Look at my program's swap code */
                }
            }
        }

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Fekore View Post
    Thank you for the example program it certainly got me thinking about my last function and other parts of my code I should polish up.
    A big candidate for polishing up are the following lines:

    Code:
    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 sort!\n\n");
        printf("\t\t    Please choose option 2. to add records.");
        getch();
        exit (0);
    }
    You repeat them in 4 out of 5 functions (leaving aside quit). Ever thought about moving them into a function?

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    I shortly figured out that I was doing the wrong assigning with my Bubblesort - not to mention I forgot to increment "i" in my outer for() loop.. I must've been out of it! Got the sort function to work but I do still have some questions. Is there anyway I can exit appropriately from any function (except quit) for which there is no file to modify/sort/list cars for? Right now I'm exiting with
    Code:
    exit(0)
    but it quits out of the entire program and that's not something that is user-friendly - is there any commands/functions that will let me safely avoid an error like this?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes. You use your functions return. You should exit() or return from your program, from main(). Here's an example from my example program above.

    There was no file to read, so the program returns to the operating system:
    Code:
       if((fp=fopen("videoDB.txt", "rt"))==NULL) {
          printf("Error! File Not Found\n");
          return 1;
       }
    If you are in a function that was called by menu(), then you would return to menu(), at which point you could use other parts of the program, or quit elegantly.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I would change the following:
    Code:
       while((fgets(intake, 149, fp))!= NULL) {
          //printf("%s",intake); getchar();
          //printf("%s j: %d*\n", intake, j); getchar();
          j=strlen(intake);
          while(intake[j] != ' ') 
             --j;
          intake[j++]='\0';
         strcpy(videos[i].title, intake);
         sscanf(intake+j,"%d ", &videos[i].rating);
    
         ++i;
    to something a lot simpler, for example:
    Code:
    while (fgets(intake, sizeof(intake), fp) != NULL) {
        if (sscanf(intake, "%s %d", videos[i].title, &videos[i].rating) != 2)
            break;
        i++;
    }

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have you thought about using a linked list?

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    I haven't been taught about linked lists as yet but I'm sure it looks like a viable option. Thanks for all of the help Adak and everyone else!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory Program, Need Help
    By pfdandk in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2010, 10:46 PM
  2. Inventory System
    By headshot119 in forum C Programming
    Replies: 28
    Last Post: 09-24-2009, 10:53 AM
  3. Game inventory
    By Xannidel in forum Game Programming
    Replies: 2
    Last Post: 02-27-2009, 02:20 PM
  4. Expanding Inventory
    By Sentral in forum Game Programming
    Replies: 11
    Last Post: 08-23-2006, 05:28 AM
  5. need help with an inventory program
    By lackerson191 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2001, 09:32 PM