Thread: structures

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    structures

    I am having a problem with the menu, it isn't reading 'd' to delete..


    Code:
    #include <stdio.h>
    
    #define NAME_LEN 25
    #define MAX_PARTS 50
    
       struct part {
          int number;
          char name[NAME_LEN +1];
          int on_hand;
       } inventory[MAX_PARTS];
       
    	
       struct item {
          int id;
          char name [20];
          double cost;
          char category;
       	
       } ;
    
       int num_parts=0;
    
       int find_part(int number);
       void insert(void);
       void delete(void);
       void search(void);
       void update(void);
       void print(void);
    
    // main:  Prompts the user to enter an operation code, then calls a 
    //  function to perfrom the requested action. Repeats until the user 
    //        enters the command 'q'.  Prints an error message if the user 
    //        enters an illegal code 
    
        int main ()
       { 
          char code;
       
          printf("Grocery Store Database\n");
          printf("Choose an option\n");
          printf("i-Insert\t d-Delete\t s-Search\t u-Update\t p-Print\t q-Quit\n");
          for(; ;)
          {
             printf("Enter operation code:  ");
             scanf(" %c", &code);
             while (getchar() != '\n')  //skips to end of line
                ;
             switch (code)
             {
                case 'i': insert();
                   break;
                case 'd': delete();
                   break;
                case 's': search();
                   break;
                case 'u': update();
                   break;
                case 'p': print();
                   break;
                case 'q': 
                   return 0;
                default: printf("ILLEGAL CODE!!!\n");
             }
             printf("\n");
          }//end for loop
       }//end main
    
    
    
        int find_part(int number)    //looks up ID# returns array index or -1 if not found
       {
          int i;
          for (i=0; i<num_parts; i++)
             if (inventory[i].number == number)
                return i;
          return -1;
       }
    
    
    
        void insert(void)         	//add new item and info
       {
          int part_number;
       
          if(num_parts==MAX_PARTS)
          {
             printf("DATABASE IS FULL; CANNOT ADD MORE PARTS.\n");
             return;
          }
       
          printf("Enter part number:  ");
          scanf("%d", &part_number);
          if (find_part(part_number) >= 0)
          {
             printf("Part already exists.\n");
             return;
          }
       
          inventory[num_parts].number = part_number;
          printf("Enter part name: ");
          scanf("%s",inventory[num_parts].name);
          printf("Enter quantity on hand: ");
          scanf("%d",&inventory[num_parts].on_hand);
          num_parts++;
       }
    
        void delete(void)					//finds item in database and removes it
       {
          int i, number;
       	
          printf("Enter part number:  ");
          scanf("%d", &number);
       	
          i=find_part(number);
          if (i>=0)
          {
             if (inventory[i].number == number)
                number = 0;
             return;
          }
          else printf("PART NOT FOUND!\n");
       }
    		
    
    
        void search(void) 				// SEARCH and display results
       {
          int i, number;
       
          printf("Enter part number: ");
          scanf("%d", &number);
       
          i=find_part(number);
          if (i>=0)
          {
             printf("Part name: %s\n", inventory[i].name);
             printf("Quantity on Hand: %d\n", inventory[i].on_hand);
          }
          else
             printf("PART NOT FOUND!!!\n");
       }
     
    
    
    
        void update(void) 					//Edits items currently in the database
       {
          int i, number, change;
       
          printf("Enter part number: ");
          scanf("%d", &number);
       
          i=find_part(number);
          if (i>=0)
          {
             printf("Enter change in quantity to add to on hand: ");
             scanf("%d", &change);
             inventory[i].on_hand += change;
          }
          else
             printf("PART NOT FOUND!!!\n");
       }
    
    
    
        void print (void)    			//Prints all information in database
       {
          int i;
       
          printf("Part Number\tPart Name\t\tQuantity on Hand\n");
          for (i=0; i<num_parts; i++)
             printf("%7d       %-25s%11d\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
       }
    also, I am trying to put in the cost and category of the items for insert. if anyone could help I would appreciate it

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    For the delete problem:
    When you delete an item, all you are currently doing is setting the local variable number to 0, which achieves nothing:
    Code:
    if (inventory[i].number == number)
                number = 0;
    You need to somehow mark the element of the inventory array as free so that it can be reused later (via insert), and ignored by all the print and search routines. You could set inventory[i].number to 0, and your whole program will then need to be aware that inventory[i].number == 0 means "empty slot in the array".

    The cost and category appear to be part of a currently unused stucture. How do you want to relate this structure to the one already in use?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    When the user chooses to input a new item I would like it to also ask for the cost and category.. such as 'F' for food and 'D' for drink.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    can anyone help?

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    es vous pouvez traduire en francai pour que je puisse vous aider
    et precisez a quoi sert ce programme

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is an english speaking forum enjoy
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (inventory[i].number == number)
    > number = 0;
    Don't you want to do
    Code:
             if (inventory[i].number == number)
                inventory[i].number = 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    sort of, what is happening now is that the item is not getting erased but is just setting the part number to zero..

    Code:
    #include <stdio.h>
    
    #define NAME_LEN 25
    #define MAX_PARTS 50
    
       struct part {
          int number;
          char name[NAME_LEN +1];
          int on_hand;
       } inventory[MAX_PARTS];
       
    	
       struct item {
          int id;
          char name [20];
          double cost;
          char category;
       	
       } ;
    
       int num_parts=0;
    
       int find_part(int number);
       void insert(void);
       void delete(void);
       void search(void);
       void update(void);
       void print(void);
    
    // main:  Prompts the user to enter an operation code, then calls a 
    //  function to perfrom the requested action. Repeats until the user 
    //        enters the command 'q'.  Prints an error message if the user 
    //        enters an illegal code 
    
        int main ()
       { 
          char code;
       
          printf("Grocery Store Database\n");
          printf("Choose an option\n");
          printf("i-Insert\t d-Delete\t s-Search\t u-Update\t p-Print\t q-Quit\n");
          for(; ;)
          {
             printf("Enter operation code:  ");
             scanf(" %c", &code);
             while (getchar() != '\n')  //skips to end of line
                ;
             switch (code)
             {
                case 'i': insert();
                   break;
                case 'd': delete();
                   break;
                case 's': search();
                   break;
                case 'u': update();
                   break;
                case 'p': print();
                   break;
                case 'q': 
                   return 0;
                default: printf("ILLEGAL CODE!!!\n");
             }
             printf("\n");
          }//end for loop
       }//end main
    
    
    
        int find_part(int number)    //looks up ID# returns array index or -1 if not found
       {
          int i;
          for (i=0; i<num_parts; i++)
             if (inventory[i].number == number)
                return i;
          return -1;
       }
    
    
    
        void insert(void)         	//add new item and info
       {
          int part_number;
       
          if(num_parts==MAX_PARTS)
          {
             printf("DATABASE IS FULL; CANNOT ADD MORE PARTS.\n");
             return;
          }
       
          printf("Enter part number:  ");
          scanf("%d", &part_number);
          if (find_part(part_number) >= 0)
          {
             printf("Part already exists.\n");
             return;
          }
       
          inventory[num_parts].number = part_number;
          printf("Enter part name: ");
          scanf("%s",inventory[num_parts].name);
          printf("Enter quantity on hand: ");
          scanf("%d",&inventory[num_parts].on_hand);
          num_parts++;
       }
    
        void delete(void)					//finds item in database and removes it
       {
          int i, number;
       	
          printf("Enter part number:  ");
          scanf("%d", &number);
       	
          i=find_part(number);
          if (i>=0)
          {
             if (inventory[i].number == number)
                inventory[i].number = 0;;
             return;
          }
          else printf("PART NOT FOUND!\n");
       }
    		
    
    
        void search(void) 				// SEARCH and display results
       {
          int i, number;
       
          printf("Enter part number: ");
          scanf("%d", &number);
       
          i=find_part(number);
          if (i>=0)
          {
             printf("Part name: %s\n", inventory[i].name);
             printf("Quantity on Hand: %d\n", inventory[i].on_hand);
          }
          else
             printf("PART NOT FOUND!!!\n");
       }
     
    
    
    
        void update(void) 					//Edits items currently in the database
       {
          int i, number, change;
       
          printf("Enter part number: ");
          scanf("%d", &number);
       
          i=find_part(number);
          if (i>=0)
          {
             printf("Enter change in quantity to add to on hand: ");
             scanf("%d", &change);
             inventory[i].on_hand += change;
          }
          else
             printf("PART NOT FOUND!!!\n");
       }
    
    
    
        void print (void)    			//Prints all information in database
       {
          int i;
       
          printf("Part Number\tPart Name\t\tQuantity on Hand\n");
          for (i=0; i<num_parts; i++)
             printf("%7d       %-25s%11d\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
       }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you really want to delete it (rather than just mark is as deleted), then you need to
    - move all entries which follow it down one slot in the array
    Code:
    while ( i < num_parts-1 ) { inventory[i] = inventory[i+1]; i++ }
    - decrement the count of parts
    Code:
    num_parts--;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    thanks, also how would i go about getting the menu to come up after each operation is completed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM