stock program

This is a discussion on stock program within the C Programming forums, part of the General Programming Boards category; I have created another function and the looks like this now but it still does not work... Code: VOID writeALL(void) ...

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    I have created another function and the looks like this now but it still does not work...
    Code:
    VOID writeALL(void) {
    FILE *fp;
    fp = fopen("stockdb.txt", "r");
    
    char c;
    struct stock stk1;
     
    while((fscanf(fp, "%s %s %s %d%c",stk1.name,stk1. code,stk1.price, &stk1.quantity,&c)) > 3) {
       printf("%s %s %s %d \n", stk1.name,stk1.code,stk1.price,stk1.quantity);
     
    }
     
    fclose(fp);  
    getch();
    I still getting the size error...

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by akerman View Post
    I have created another function and the looks like this now but it still does not work...
    Code:
    VOID writeALL(void) {
    FILE *fp;
    fp = fopen("stockdb.txt", "r");
    
    char c;
    struct stock stk1;
     
    while((fscanf(fp, "%s %s %s %d%c",stk1.name,stk1. code,stk1.price, &stk1.quantity,&c)) > 3) {
       printf("%s %s %s %d \n", stk1.name,stk1.code,stk1.price,stk1.quantity);
     
    }
     
    fclose(fp);  
    getch();
    I still getting the size error...
    Add enough to your little snippet of a program, so it will show compile or at least, show the error.

    Along with a bit of data so we can run it, in case studying it isn't enough to spot the problem.

    I don't recall the "size error" -- copy and paste it, please.

  3. #18
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    Quote Originally Posted by Adak View Post
    Add enough to your little snippet of a program, so it will show compile or at least, show the error.

    Along with a bit of data so we can run it, in case studying it isn't enough to spot the problem.

    I don't recall the "size error" -- copy and paste it, please.
    Name:  pscr.jpg
Views: 57
Size:  84.7 KB

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't read your post for the most part. With a magnifying glass, I got the size error. Looks like the definition of the struct "stock" was not made in global space ( above main() ).

    You're going to have to post your code (or a complete example), for more specific help. I can't keep squinting into a magnifying glass!

  5. #20
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    I'm sorry for that but I have sorted it somehow it was just the array type which somehow wasn't correct. Now I'm trying to sort out this delete function, however I'm trying to work of the same function you have

    Code:
     int idelete(void) {
       char buff[80];
       int i;
       printf("\nEnter the name of the item in stock you want to delete:  ");
       fgets(buff, MAXNAME, stdin);
       buff[strlen(buff)-1]='\0';
       i = fetch(buff); //get buff's index in the array
       if(i) {
          printf("Delete this record [y/n] ? ");
          printf("\n\t Name: %s \n\t Code: %s \n\t Quantity: %d",items[i].name,\
          items[i].code, items[i].quantity);
          fgets(buff, 2, stdin);
        DelRec();
          if(buff[0]=='y' || buff[0]=='Y') {
             printf("\nItem has been deleted");
             getchar();
             getchar();
            
          }
          return i;
       }
       else
          return 0;
    }
    Code:
    int DelRec(void) {
    
      int i;
      FILE *fp;
     
      //remove("stock.bak");
    
      if(rename("stockodb.txt", "stockdb.bak")==0) {
    
        
    
       if((fp=fopen("stockdb.txt", "wt"))==NULL) {
    
          printf("\nError opening file. No records were written.\n");
    
          return 1;
    
        }
    
    /*   
    
    }else {
    
        printf("\nError renaming in function writeAll()\n");
    
        return 1;
    
      }
    
    */
    
      i=0;
    
      while(1) {
    
        fprintf(fp, "%s, %s, %s, %d\n",items[i].title,items[i].code,items[i].price, items[i].quantity);
    
        ++i;
    
        if(i>NumItems) break;
    
      }  
    
      fclose(fp);
    
     
      return 0;
    }
    Can you please tell me which part of the DelRec I need to edit for it to delete completely record from the file, basically I'm not sure what line to add or delete...
    Are there any chances that I could delete empty rows as well?
    thanks

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This would be the new writeAll function for it, just change the video stuff to the stock stuff. Be sure and test this, btw!

    Code:
    int writeAll(void) {
       int i;
       FILE *fp;
    
       if((fp=fopen("videodb.txt", "wt"))==NULL) {
          printf("\nError opening videos.txt file. No records were written.\n");
          return 1;
       }
       i=0;
       while(1) {
          if(videos[i].title[0]) {  //only write out current records
             fprintf(fp, "%s, %s, %s, %d\n",videos[i].title,videos[i].actor,videos[i].genra, videos[i].rating);
             ++i;
          } 
          if(i>NumVideos) break;
       }  
       fclose(fp);
    
       return 0;
    }
    Now add the call to writeAll() IN THE SWITCH STATEMENT, after the add, edit or delete a record functions are called just before the very last line of code in that case statement, so the change in records will be written out to the data file.

    Also, in add and delete switch case blocks of code I have --NumVideos or ++NumVideos. The call to writeAll() must go AFTER the increment or decrement to NumVideos.

    The edit function has no change to NumVideos, of course.

    In your delete function, you should have logic like this, which confirms that the record should be deleted, and marks the record:
    Code:
          if(buff[0]=='y' || buff[0]=='Y') {  //confirm deletion with user
             videos[i].title[0]=='\0';           //mark the record field to be deleted by writeAll()
             printf("\nHas been deleted");
             getchar();
             getchar();
          }
    There is a bug in my program, in idelete, line 20 (of your post, above).

    Replace:
    return i;

    with:
    return 1;
    Last edited by Adak; 11-15-2012 at 06:43 PM.

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    the new function does not write to the file, at least its not shown in the file shouldn't it be creating another file and then writing to it?

  8. #23
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    basically when the new function is applied it does not write to a file and like before it does not return to previous function... There must be some easier way just to edit the delete function and make it work the way it deletes just one row from a file? Are there any chances you could write a small code to do so. I have no idea how it suppose to be written. Please help . At the moment I have left old WriteALL function which works and now searching how to make this delete function working but I have not found anything
    Last edited by akerman; 11-16-2012 at 11:40 AM.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by akerman View Post
    basically when the new function is applied it does not write to a file and like before it does not return to previous function... There must be some easier way just to edit the delete function and make it work the way it deletes just one row from a file? Are there any chances you could write a small code to do so. I have no idea how it suppose to be written. Please help . At the moment I have left old WriteALL function which works and now searching how to make this delete function working but I have not found anything
    I've made a new project, so I can write your code from your model, instead of translating it from mine.

    Yes, there is a way to do what you want, and no, I won't write that code for you. Reason is that you dabble with code, and the very first time you dabbled with that code, your data file would be corrupted.

    I have to do some running around atm, but I will fix what the problem is for the function with a copy of my video data. If you want it fixed for sure with your data, you need to post up a 7 records (rows) of your data, so I can use it.

    I'll post up a corrected version when I get back - about 4 hours.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Several changes were made to the program. Note the warning! I will not be responsible for any lost data, or losses you face because you used this program, which is certainly not tested or complete.

    Code:
    /* Warning:
       This is an unfinished program. Do not use it, until you have finished it. 
       Anyone using it does so at their own risk, which is substantial.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    #define MAXTITLE 77
    #define MAXACTOR 78
    #define MAXGENRA 50
    #define MAXVIDS 1000
    
    struct video {
       char title[MAXTITLE];
       char actor[MAXACTOR];
       char genra[MAXGENRA];
       int  rating;          //1-5 stars
    };
    typedef struct video vid;
    
    vid videos[MAXVIDS];
    int NumVideos;
    
    //int Size = sizeof(vid);
    
    int addVideos(void);    //function proto's
    int idelete(void);
    int edit(void);
    int fetch(char *buff);
    void menu(void);
    void sortRecords(void);
    void view(void);
    int writeAll(void);
    
    int main(void) {
       char buff[214];
       int i;
       
       FILE * fp_in;
      
       if((fp_in = fopen("videodb.txt", "r")) == NULL) {  
          fputs("Error! Can't find or open videodb.txt\n", stderr);
          return 1;
       }
       i=0;
       while((fgets(buff, sizeof(buff), fp_in))!=NULL) {
          buff[strlen(buff)-1]='\0';
          //printf("Buff: %s\n\n",buff);getchar();
          sscanf(buff, "%[^,], %[^,], %[^,], %d ", videos[i].title,videos[i].actor,videos[i].genra,&videos[i].rating);
          //printf(" title: %s\n  actors: %s\n   genra: %s\n  rating: %d\n\n",
            // videos[i].title,videos[i].actor,videos[i].genra,videos[i].rating); getchar();
          
          if(++i==MAXVIDS) {
             printf("Error! The array to hold video data, is too small. Please enlarge it\n\n");
             getchar();
             break;
          }
       }
       
       fclose(fp_in);
       NumVideos=i;
       sortRecords();
       //view();
       menu();
       printf("                                                                               \n");
       printf("                                                                               \r");
    
       return 0;
    }
    // this function is 90% done 
    int addVideos(void) {
       int i,j=0,ok=0,len;
       char buff[80]={'\0'};
       char ch='y';
       vid vid1;
    
       while(ch=='y' || ch=='Y') {
          _gotoxy(1,11); 
          printf("                                                                               \n"); 
          printf(" title:                                                                        ");
          _gotoxy(9,12);
          fgets(buff, MAXTITLE, stdin);
          len=strlen(buff)-1;
          buff[len]='\0';
          strcpy(vid1.title, buff);
    
          printf("                                                                              *\n"); 
          printf(" principal actor(s):                                                          *");
          _gotoxy(22,14);
          fgets(buff, MAXACTOR, stdin);
          len=strlen(buff)-1;
          buff[len]='\0';
          strcpy(vid1.actor, buff);
    
          printf("                                                                              *\n"); 
          printf(" genra:                                                                       *");
          _gotoxy(9,16);
          fgets(buff, MAXGENRA, stdin);
          len=strlen(buff)-1;
          buff[len]='\0';
          strcpy(vid1.genra, buff);
    
          printf("                                                                              *\n"); 
          printf(" rating [0-5]:                                                                * ");
          _gotoxy(16,18);
          fgets(buff, sizeof(videos[0].rating), stdin);
          sscanf(buff, "%d", &vid1.rating);
    
          if(NumVideos<MAXVIDS) {
             videos[NumVideos]=vid1;
             ++NumVideos;
             ok=1;
          }else { 
             ok=0;
             printf("\nArray is full, couldn't add that record");
             break;
          }
          printf("\nAdd another video? [y/n]: ");
          scanf(" %c", &ch);
          getchar();
       
       }
       sortRecords();
       writeAll();
       //clean up screen
       for(i=12;i<22;i+=2) {
          _gotoxy(1,i);
          printf("                                                                              *\n"); 
       }
       return ok;       //success or failure variable
    }
    int edit(void) {
       char buff[80];
       int i=0;
    
       printf("\nEnter the title of the video you want to edit: ");
       fgets(buff, MAXTITLE, stdin); 
       if(buff[strlen(buff)-1]=='\n') 
          buff[strlen(buff)-1]='\0';
    
       i = fetch(buff); //in the list?
       if(i>-1)   
          //return 0;
    
       //video was found, i is it's index in the array
       printf("\n\t  Title: %s\n\t  Actor: %s\n\t rating: %2d\n",
       videos[i].title, videos[i].actor, videos[i].rating);
       printf("\n\t  Enter new title [hit <enter> to leave it unchanged]: ");
       fgets(buff, MAXTITLE, stdin);
       if(strlen(buff) >1) { 
          if(buff[strlen(buff)-1]=='\n') {
            buff[strlen(buff)-1]='\0';
            strcpy(videos[i].title, buff);
          }
       }
       printf("\n\t  Enter new actor [<enter> to leave it unchanged]: ");
       fgets(buff, MAXACTOR, stdin);
       if(strlen(buff) >1) { 
          if(buff[strlen(buff)-1]=='\n') {
            buff[strlen(buff)-1]='\0';
            strcpy(videos[i].actor, buff);
          }
       }
       printf("\n\t  Enter new rating [0-6, or <enter> to leave it unchanged]: ");
       fgets(buff, sizeof(videos[0].rating), stdin);
       if(strlen(buff) >1) { 
          sscanf(buff, "%d", &videos[i].rating);
       }
       sortRecords();
       
       return i;
    }
    int fetch(char *buff) {
       int lo, hi, mid;
       lo=0;
       hi = NumVideos-1;
          
       while(lo <= hi) {
          mid=(lo + hi)/2;
          if((strcmp(videos[mid].title, buff)) <0) {
             lo=mid+1;
          }
          else if((strcmp(videos[mid].title, buff)) >0) {
             hi=mid-1;
          }
          else
             return mid;
       }
       return -1;
    }
    int idelete(void) {
       char buff[80];
       int i;
       printf("\nEnter the title of the vid you want to delete:  ");
       fgets(buff, MAXTITLE, stdin); 
       buff[strlen(buff)-1]='\0';
       printf("\n\n%s     length: %d  \n",buff,strlen(buff)); 
       i = fetch(buff); //get buff's index in the array
       if(i>-1) {
          printf("Delete this record [y/n] ? ");
          printf("\n\t Title: %s \n\t Actor: %s \n\t Rating: %d",videos[i].title,\
          videos[i].actor, videos[i].rating); 
          fgets(buff, 2, stdin);
          if(buff[0]=='y' || buff[0]=='Y') {
             videos[i].title[0]='~';
             videos[i].title[1]='\0';
             printf("\nHas been deleted \n");
             getchar();
             //*vids[i].title = "";
             //*vids[i].actor = "";
             //*vids[i].rating = 0;
             sortRecords();
             --NumVideos;
             writeAll();
          }
          return 1;
       }else {
          printf("That record was not found.\n");
       }
          
       return 0;
    }
    void menu(void) {
       char ch;
       char buff[80];
       int i,n, index, ok=0;
      
       do {
          //clear the menu area of the display
          _gotoxy(1,1);
          //for(i=0;i<3;i++) {
          //printf("                                                                              *\n"); 
          //}
          printf("                                                                               \n");  
          _gotoxy(1,2);
          printf("                <<<<      Welcome to the Main Menu       >>>>                  \n");
          printf("                                                                               \n"); 
          printf("        [a] Add a video            [d] Delete a video                          \n");
          printf("        [e] Edit a video record    [s] Search for a video                      \n");
          printf("        [v] View all videos        [w] Write out all videos                    \n");
          printf("                                                                              *\n");
          printf("                     Your Choice, or Q to quit:                                \n");
          printf("                                                                              *\n"); 
          //i=_wherey();
          _gotoxy(49,8);    //row 8 is the Choice row in the menu
          fgets(buff, sizeof(buff), stdin);
          sscanf(&buff[0]," %c",&ch);
          
          switch (ch) {
             case 'a':   
                printf("\n Would you like to add a new video? [y/n]: ");
                ch = getchar();
                getchar();
                if(ch=='y' || ch=='Y') {
                   n=addVideos();
                   if(n) {   //a successful add
                      sortRecords();
                      _gotoxy(1,12);
                      printf(" One Video has been added. %d Videos in all now.                    **\n",NumVideos);
                      getchar(); 
                   }
                }
                _gotoxy(1,12);
                printf("                                                                              *\n"); 
                ch='\0';
                break;
             case 'd':  
                ok=idelete(); 
                ch='\0';
                break;
          case 'e': edit();  //8888888888888888888888
                    break;    
          case 's':  
             for(i=0;i<10;i++) {
                printf("                                                                              *\n"); 
             }
             _gotoxy(1,10);   
             printf("Enter the video's  \n  title: ");
             fgets(buff, MAXTITLE, stdin); //sizeof(videos[0])
             buff[strlen(buff)-1]='\0';
             index = fetch(buff);
             //printf("Index: %d\n",index); getchar();
             if(index>-1) {
                printf(" #%d) title: %s\n  actors: %s\n   genra: %s\n  rating: %d\n\n",
                        index+1,videos[index].title,videos[index].actor,videos[index].genra,videos[index].rating);
                printf("                                                                              *\n"); 
                printf("                                                                              *\n"); 
                printf("                                                                              *\n"); 
                printf("                                                                              *\n"); 
                getchar();
             }else {
                printf("That video was not found. \n ");
             }
             ch='\0';  
             break;
          case 'v':  view(); ch='\0';break;
          case 'w':  writeAll(); break;
    
          case 'Q':  ch='q';
          case 'q':  break;
          default: printf("\n\t\t    Please enter a valid selection\n");
        };
    
      }while(ch!='q'); 
    }
    
    void sortRecords(void) {
       int i, j; 
       vid temp;     
       for(i=1;i<NumVideos;i++) {  
          temp = videos[i];
          j = i-1;
          while((strcmp(videos[j].title, temp.title)) > 0) {
             videos[j + 1] = videos[j];
             --j;
             if(j<0) break;
          }   
          videos[j+1] = temp;
       }
       //printf("\n%s %s %s %d  \n",videos[0].title, videos[0].actor, videos[0].genra, videos[0].rating);  
       //printf("%s %s %s %d  \n",videos[241].title, videos[241].actor, videos[241].genra, videos[241].rating);  
       //getchar();
    
    }
    void view(void) {
       int i=0,row=12;
       _gotoxy(2,row-1);
          printf("                                                                              \n");  
       while(i < NumVideos) {
          _gotoxy(2,row);
          
          printf("                                                                              \r");
          printf("%3d) title: %-60s\n",i+1,videos[i].title);
          printf("  actors: %-68s\n",videos[i].actor);
          printf("   genra: %-60s\n",videos[i].genra);
          printf("  rating: %-60d\n",videos[i].rating);
          printf("                                                                              \r");
          ++i;
          if(i%5==0 && i) {
             row=7; 
             _getch();
          }
          row+=5;
          
       }
       for(i=0;i<4;i++) {
          printf("                                                                              \n");
          if(i==2)
             printf("Last video record has been reached. Hit Enter to Continue.                 \n");
       }
       i=_wherey();
       _gotoxy(60,i-2);
       getchar(); 
       //printf("\n\n");
    }
    int writeAll(void) {
       int i;
       FILE *fp;
    
       if((fp=fopen("videodb.txt", "wt"))==NULL) {
          printf("\nError opening videos.txt file. No records were written.\n");
          return 1;
       }
       i=0;
       while(i<NumVideos) {
          if(videos[i].title[0]) {
             fprintf(fp, "%s, %s, %s, %d\n",videos[i].title,videos[i].actor,videos[i].genra, videos[i].rating);
             ++i;
          } 
       }  
       fclose(fp);
    
       return 0;
    }

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    I had to add getchar() at the end of writeAll() however all the function work the way I wanted it to, So THANK YOU very much, thanks for your time and help, really appropriate it.

  12. #27
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    well since you need to start very large amounts of data a better data structure like a hash table would work or you could dynamically allocate memory for the array as it fills up...

  13. #28
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    Code:
    /*This program creates a database to manage a stock of goods.*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    #define MAXNAME 50
    #define MAXCODE 100
    #define MAXPRICE 50
    #define MAXITEMS 1000
    
    
    struct video {
       char name[MAXNAME];
       char code[MAXCODE];
       char price[MAXPRICE];
       int  quantity;
    };
    
    typedef struct video Stk;/*This defines the properties of items in stock.*/
    
    Stk items[MAXITEMS];
    
    int NumItems;
    
    int Size = sizeof(stk); /*display size of total items in stock*/
    
    
    /*this functions manipulate the items found in stock.*/
    int addItems(void);/*Adds items to a stock.*/
    int idelete(void); /*This deletes items from stock*/
    int edit(void);   /*This changes the amount of items in stock.*/
    int fetch(char *buff);  /*this fetches an item with its details from stock.*/
    void menu(void);  /*This shows the menu of items in every stock.*/
    void view(void);  /*This views the items currently store  in stock.*/
    void sortRecords(void);  /*This sorts the items found in stock*/
    
    
    /*Main():This function calls the different functions used for the manipulation of stock items.*/
    int main(void)
    {
       char buff[214];
    
       int i = 0; //number of entries
    
       FILE * fp_in = NULL;
    
       if((fp_in = fopen("stockdb.txt", "r")) == NULL)
       {
          fputs("Error! Can't find or open stockdb.txt\n", stderr);
          return 1;
       }
    
    
       while((fgets(buff, sizeof(buff), fp_in))!=NULL)
       {
          buff[strlen(buff)-1]='\0';
          sscanf(buff, "%[^,], %[^,], %[^,], %d ", items[i].name,items[i].code,items[i].price,&items[i].quantity);
    
          if(++i == MAXITEMS)
          {
             fprintf(stderr, "Error! The array to hold data, is too small. Please enlarge it or delete stock record\n\n");
             getchar();
             break;
          }
       }
    
       fclose(fp_in);
    
       NumItems = i;
    
       sortRecords();
    
       //view();
       menu();
    
       printf("                                  \n");
       printf("                                  \r");
    
       return 0;
    }
    
    
    /* this function is 90% done */
    
    /*addItems():This function adds new elements to the stock and returns 0 if the add was successful.*/
    int addItems(void)
    {
       int i = 0,ok = 0, j = 0, len = 0;
    
       char buff[MAXNAME]={'\0'};
    
       char ch = 'y';
    
       Stk stk1; //variable only for this function
    
       while( ch =='y' || ch =='Y')
       {
          printf("\nEnter Name:  ");
          fgets(buff, MAXCODE, stdin);
    
          len = strlen(buff);
          buff[len + 1]='\0';
          strcpy(stk1.name, buff);
    
    
          printf("\nEnter Bar code:  ");
          fgets(buff, MAXCODE, stdin);
    
          len = strlen(buff);
          buff[len + 1]='\0';
          strcpy(stk1.code, buff);
    
    
          printf("\nEnter Price:  ");
          fgets(buff, MAXPRICE, stdin);
    
          len = strlen(buff)1;
          buff[len + 1]='\0';
          strcpy(stk1.price, buff);
    
    
          printf("\nEnter Quantity: ");
          fgets(buff, MAX_INT, stdin);
          sscanf(buff, " %d", &stk1.quantity);
    
    
         if( NumItems < MAXITEMS && !ok)
         {
            items[NumItems] = stk1;
            ok = 0;
            ++NumItems;
    
            break;
         }
    
          else
          {
             fprintf(stderr,"\nArray is full, couldn't add that record");
             --NumItems;
             ok = 1;
             return NumItems;
          }
    
          printf("\nWould you like to add another item to the stock? [y/n]: ");
          scanf(" %1c", &ch);
           getchar();
       }
    
    
       return NumItems;       //successful add if ok = 0;
    }
    
    
    /*Edit():This edits items on the stock.*/
    int edit(void)
    {
       char buff[MAXNAME];
    
       int i = 0;
    
       printf("\nEnter the name of the item you want to edit: ");
       fgets(buff, MAXNAME, stdin);
    
       if(buff[strlen(buff) + 1]=='\n')
          buff[strlen(buff) + 1]='\0';
    
       i = fetch(buff); /*This checks if there is an item by that name on the list and returns */
    
    
       if(i < 0)
       {
           fputs("\nElement is not found on list.", stderr);
           return 0;
       }
    
    
       //video was found, i is it's index in the array
    
       printf("\n Name: %s, \t  Bar_code: %s\n Quantity: %2d\n", items[i].name, items[i].code, items[i].quantity);
    
       printf("\nEnter new name [ or Hit  <enter> to leave it unchanged]: ");
       fgets(buff, MAXNAME, stdin);
       if(strlen(buff) > 1)
       {
           if(buff[strlen(buff) + 1]=='\n')
           {
                buff[strlen(buff) + 1]='\0';
                strcpy(items[i].name, buff);
          }
       }
    
    
       printf("\nEnter new barcode [ Hit <enter> to leave it unchanged]: ");
       fgets(buff, MAXCODE, stdin);
       if(strlen(buff) >1)
       {
          if(buff[strlen(buff)-1]=='\n')
          {
                buff[strlen(buff)-1]='\0';
                strcpy(items[i].code, buff);
          }
       }
    
    
       printf("\nEnter new quantity [<enter> to leave it unchanged]: ");
       fgets(buff, sizeof(items[0].quantity), stdin);
       if(strlen(buff) >1)
       {
          sscanf(buff, "%d", &items[i].quantity);
       }
    
       sortRecords();
    
       return i;
    }
    
    /*fetch(): This function fetches for an item on the item list.*/
    int fetch(char *buff)
    {
       int lo, hi, mid;
    
       lo = 0;
    
       hi = NumItems;
    
       while(lo < hi + 1)
       {
          mid = (lo + hi)/2;
          if((strcmp(items[low].name, buff)) <0)
             lo++;
    
          else if((strcmp(items[0].name, buff)) >0)
             lo++;
    
          else
             return low;
       }
    
       return -1;
    }
    
    /*idelete():This function deletes elements from the stock and returns 0 if successfull or -1 if otherwise*/
    int idelete(void)
    {
       char buff[80];
       int i;
    
       printf("\nEnter the name of the item in stock you want to delete:  ");
       fgets(buff, MAXNAME, stdin);
       buff[strlen(buff) + 1]='\0';
       i = fetch(buff); //get buff's index in the array
       if(i >= 0)
       {
          printf("\nWould you like to Delete this record [y/n] ?:  ");
          printf("\n Name: %s \t Bar code: %s \nQuantity: %d\n", items[i].name, items[i].code, items[i].quantity);
          fgets(buff, 2, stdin);
    
          if(buff[0]=='y' || buff[0]=='Y')
          {
              free(items[i]->name);
              free(items[i]->code);
              free(items[i]->price);
              free(items[i]->quantity);
          }
    
          printf("\nItem has been deleted");
    
          return 0;
       }
    
        return -1;
    }
    
    /*Menu(): This function display the main menu that shows the functions and command for the user
     *        to execute or enter and the corresponding functions executed.
     */
    
    void menu(void)
    {
       char ch;
       char buff[MAXNAME];
       int i = 0,n = 0, index  = 0, ok = 0;
    
    
       do
        {
          printf("                                                                             \n");
    
          printf("                <<<<      Welcome to the Main Menu       >>>>                \n");
          printf("                                                                             \n");
          printf("        [a] Add an item            [d] Delete an item                        \n");
          printf("        [e] Edit an item           [s] Search for an item                    \n");
          printf("        [v] View all items                                                   \n");// not ready yet
          printf("                                                                            *\n");
          printf("                     Your Choice, or Q to quit:                              \n");
          printf("\n\nEnter your Choice: ");                                                                       *\n");
          ch = getchar();
          getchar();
    
    
          switch (ch)
          {
             case 'a': case 'A':
                printf("\n Would you like to add a new item? [y/n]: ");
                ch = getchar();
                getchar();
                n = addItems();
    
                if(n > 0)
                {   //a successful add
    
                    sortRecords();
                      printf(" Items   has been added and there are  %d items in the Stock List. \n",NumItems);
                      getchar();
                }
    
                printf("\n");
                break;
    
    
             case 'd': case 'D':    ok = idelete();
                                    break;
    
             case 'e': case 'E':    edit();
                                    break;
    
             case 's': case 'S':
    
             printf("Enter item's Name: ");
             fgets(buff, MAXNAME), stdin);
             buff[strlen(buff) + ]='\0';
    
             index = fetch(buff);
             if(index >= 0)
             {
                 printf(" #%d) title: %s\t  bar_code: %s\n   price: %s\t  quantity: %d\n\n",
                        index+1,items[index].name, items[index].code,items[index].price,items[index].quantity);
             }
    
             break;
    
            case 'Q':  case 'q':  fprintf(stderr,"\nThanks for using my Software.\nWe would like to receive"
                                               " your comments and feedback to better our services.");
                                    exit(EXIT_SUCCESS);
            break;
    
           default:       printf("\nPlease Invalid code. please try again.");
    
          };
    
        }while( ch != 'q' || ch != 'Q');
    
    
    }
    
    
    /*sortRecords(): This sorts the elements in the list or items. */
    void sortRecords(void)
    {
       int i, j;
       Stk testy;
    
       for(i = 1; i < NumItems; i++)
       {
           testy = items[i];
           j = i-1;
    
           while((strcmp(items[j].name, testy.name)) > 0)
           {
               items[j + 1] = items[j];
               --j;
               if( j < 0)
                    break;
          }
    
          items[j+1] = testy;
       }
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stock Program
    By Adrian K'Ski in forum C Programming
    Replies: 15
    Last Post: 06-03-2012, 07:30 AM
  2. Stock system
    By dan.goodridge in forum C Programming
    Replies: 7
    Last Post: 05-21-2012, 04:54 AM
  3. C Stock Sorting Program Issue
    By litebread in forum C Programming
    Replies: 18
    Last Post: 12-09-2011, 10:37 PM
  4. Stock Data
    By bskaer1 in forum C++ Programming
    Replies: 0
    Last Post: 10-16-2010, 11:59 PM
  5. Stock Taking program
    By C Babe in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2003, 07:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21