Thread: problems with inventory system

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    problems with inventory system

    WAZ UP EVERYONE.

    I am making an inventory system that manages items you know adds items, removes and etc.

    SO ive been able to do a lot of things so far like add new item and quit and ive made a menu so i can navigate functions.
    But ive run into problems i cant get the rest of the code to work can you please help me? when i do the other ones it doesnt work


    Code:
    #include <stdio.h>
    #include <string.h>
    char itemName [50];
     float itemPrice = 0;
      float total = 0;
      int numberOfItems = 0;
       int number = 0;
       char scanItem [50];
       float scanPrice = 0;
        void add(float aPrice,char aName [50]);
        float getTotal();
        float calaverage();
    
        char nameforitem[20];
        int amount;
        char unit[10];
    
    void add(struct ItemsList * detail , int number)
    {
        int i=0,n=1,x,error;
        for (i=0; i<number; i++,n++)
        {
            printf(" ItemName  %d:",n);
            scanf("%19s",&detail[i].ItemName);
            do
            {
                printf("Amount:");
                fflush(stdin);
                error = scanf("%d",&detail[i].amount);
                if (error!=1)
                    printf("Wrong form. Try again!");
            }
            while (error!=1);
            printf("Unit:");
            scanf("%9s",&detail[i].unit);
        }
        int main(void)
        {
            struct ItemsList *points;
        int numberOfItem;
        printf("How many items:");
        scanf("%d",&numberOfItem);
        points=(struct ItemsList*) malloc (numberOfItem * sizeof(struct ItemsList*));
    
    
    
        printf("List of Items...\n");
        int k;
        for (k=0; k<numberOfItem; k++)
        {
            printf("%-20s%-10d%-20s\n", points[k].Item Name, points[k].amount, points[k].unit);
        };
    
    while (1)
    {
    printf("1. Add new Item\n");
    printf("2. Print amount of items stored\n");
    printf("3. Remove an item\n");
    printf("4. Display info on items\n");
    printf("5. Search for items\n");
    printf("6. Check for given items in storage\n");
    printf("7. Print price for items stored\n");
    printf("8. Manage items\n");
    printf("9. Save storage to file\n");
    printf("10. Load storage from file\n");
    printf("11. Quit\n"); printf("Enter Option: ");
          scanf("%d",&number);
          if(number==1)
            {
                printf ("Enter item name: ");
      scanf ("%s", scanItem);
      printf ("\n");
                printf ("\nEnter Price: ");
     scanf("%f",&scanPrice);
    
       add(scanPrice,scanItem);
       }
    
       else if (number == 2) printf ("\nManage items    %s\n"),
        scanf("20%s-20%f", scanItem, &scanPrice);
    
       else if (number == 3)
    
        printf("Enter the Item Name wish you want to delete \n");
        scanf ("%s", scanItem);
       if ( itemName >= itemName  )
          printf("Deletion not possible.\n");
       else
       {
           printf("Delete item")
                  scanf("delete item")
    
       }
    
    
       else if (number == 4)
       else if (number == 5)
    
        printf("Search for items\n")
    
       else if (number == 6)
       else if (number == 7) printf ("Total is %f\n\n", getTotal());
       else if (number == 8)
       else if (number == 9)
       else if (number == 10) printf ("The average is %f\n\n", calaverage ());
       else if (number == 11) break; else printf("Invalid Option\n");
       }
       return 0;
       }
        void add(float aPrice,char aName [50])
        {
            strcpy (itemName, aName);
            itemPrice = aPrice;
    
              total=total+aPrice;
              numberOfItems++;
              }
              float getTotal()
              {
                  return total;
                  }
                  float calaverage()
                  {
                       return total/numberOfItems;
                       }
    Last edited by a29bbas; 01-07-2014 at 11:21 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You should describe in detail what doesn't work about it. Compiler errors/warnings? Copy-paste the errors/warnings with line numbers. Does it crash? Give no output? Give incorrect output? Tell us the input you gave it, the output you actually got (if any -- including crash/error messages) and the actual (correct) output you expect to see.

    Also, get an editor with auto-indent and use it to properly format your code (link). You probably already have one, and just aren't using it correctly. For example, when I indent your code:
    Code:
    void add(struct ItemsList * detail , int number)
    {
        int i=0,n=1,x,error;
        for (i=0; i<number; i++,n++)
        {
            printf(" ItemName  %d:",n);
            scanf("%19s",&detail[i].ItemName);
            do
            {
                printf("Amount:");
                fflush(stdin);
                error = scanf("%d",&detail[i].amount);
                if (error!=1)
                    printf("Wrong form. Try again!");
            }
            while (error!=1);
            printf("Unit:");
            scanf("%9s",&detail[i].unit);
        }
        int main(void)
        {
    Notice how main is indented along with the rest of add()? You can't have a function inside another function. Your brackets are wrong for the add function, you seem to be missing a closing bracket somewhere.

    You might have other similar problems, but this error could mask them. Sort out all your formatting, brackets and which code belongs in which function first.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, also, you can not have a completely empty if/else body. It's not valid C. You need something to explicitly state there is an empty body, like a semicolon or empty brackets (you don't need both):
    Code:
    // bad
    else if (number == 6)
    else if (number == 7) printf ("Total is %f\n\n", getTotal());
    else if (number == 8)
    else if (number == 9)
    
    // good
    else if (number == 6)
        ;
    else if (number == 7) printf ("Total is %f\n\n", getTotal());
    else if (number == 8) {
    }
    else if (number == 9)
    I very, very rarely have empty if/else statements in my code. It's generally best to omit them if they don't do anything.

    If, however, you were intending a sort of "fall-through", so 6 and 7 execute the same printf, you may want to consider switch/case statement instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to implement an inventory system for a 2D game?
    By Pikmeir in forum Game Programming
    Replies: 10
    Last Post: 07-06-2012, 09:06 PM
  2. How to implement an inventory system for a 2D game?
    By Pikmeir in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2012, 05:09 PM
  3. Need help on Electronic Inventory System...
    By beh007 in forum C Programming
    Replies: 1
    Last Post: 12-27-2011, 08:42 PM
  4. Inventory System
    By headshot119 in forum C Programming
    Replies: 28
    Last Post: 09-24-2009, 10:53 AM
  5. inventory system
    By linucksrox in forum Game Programming
    Replies: 0
    Last Post: 04-19-2006, 01:19 PM