Thread: more structure troubles...

  1. #1
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

    more structure troubles...

    I am trying to add to my structure....help....


    Code:
    int addItems(structure inventory[],int *arrayL)
    {
        int i;
        int z,n;
        char buff1[MAXchar];
        char buff2[MAXchar];
            
        Stemplate();
        
        printf("Please enter number of items you wish to enter: ");
        scanf("%d",&z);
        fflush (stdin);
      
        printf("Please enter inventory items\n");
      
        for (i=*arrayL+1,n=0; n<z; i++,n++)
        {
            printf("Enter Item name: ");
            gets(inventory[i].name);
            printf("Enter Item price: ");
            gets(buff1);
            printf("Enter number of Items: ");
            gets(buff2);
            printf("Enter expiry (if applicable): ");
            gets(inventory[i].expiry);
            printf("Enter type of Item: ");
            gets(inventory[i].type);
            printf("Enter other Details: ");
            gets(inventory[i].detail);
            sscanf( buff1, "%f\n", &inventory[i].price );
            sscanf( buff2, "%d\n", &inventory[i].noItems );
            fflush(stdin);
         }  
         *arrayL=i-1;
    }
    and then modifying the size of the array of structure(arrayL).Later when i copy the structure to a file or to the screen i get a jumble of numbers....plus i really screw up on pointer so i need help...

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Please do not comment on the fflush(stdin)...i know its wrong but its okay for now...

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I am guessing that the problem lies with the way you are calling the function. Please provide the caller function also.

  4. #4
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Code:
        do{
            choice = mainMenu();
            if (choice==1){
                addItems(inventory,&arrayL); 
                saveData(inventory,arrayL);
                }
            else if (choice==5){
                viewItems(inventory,arrayL);
                }
            else if (choice==0){
                saveData(inventory,arrayL);
                exitScreen();
                }
        }while(choice != 0);
    and the prototype:
    Code:
    int addItems(structure inventory[],int *);
    Plus i want to know if theres a way to force the user to type in only integars...

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    how is inventory declared? See the problem with posting code snippets

    What I see so far is that you are passing a singlar item but the function is expecting an array of that item.

  6. #6
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    umm....k

    Heres main and everythin above it....

    Code:
    #define dataFileName "WMinventory.dat"
    #define MAXchar (100)
    
    typedef struct{
          char name [50];
          float price;
          int noItems;
          char expiry[50];
          char type[50];
          char detail[50];
    }structure;
    
    int mainMenu();
    int addItems(structure inventory[],int *);
    void saveData(structure inventory[],int );
    int loadData(structure []);
    void viewItems(structure [],int);
    FILE *open_file(char [], char []);
    void Stemplate();
    void exitScreen();
    void welcomeScreen();
    
    int main(void)
    {
        FILE *dataFile;
        structure inventory[1000];
        int choice,i,arrayL;
        
        arrayL = loadData(inventory);
        welcomeScreen();
        
        do{
            choice = mainMenu();
            if (choice==1){
                addItems(inventory,&arrayL);
                saveData(inventory,arrayL);
                }
            else if (choice==5){
                viewItems(inventory,arrayL);
                }
            else if (choice==0){
                saveData(inventory,arrayL);
                exitScreen();
                }
        }while(choice != 0);
    
        return 0;
    }
    except for the libraries...

  7. #7
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    The rest of my functions work fine, coz they read my data from a file , load it to a structure then put it back into the file....

  8. #8
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Heres a different approach:
    Code:
    int addItems(structure inventory[],int *arrayL)
    {
        int i;
        int z,n;
        char buff1[MAXchar];
        char buff2[MAXchar];
            
        Stemplate();
        
        printf("Please enter number of items you wish to enter: ");
        scanf("%d",&z);
        fflush (stdin);
      
        printf("Please enter inventory items\n");
      
        for (i=*arrayL+1,n=0; n<z; i++,n++)
        {
            printf("Enter Item name: ");
            gets(inventory[i].name);
            printf("Enter Item price: ");
            gets(buff1);
            inventory[i].price=atoi(buff1);
            printf("Enter number of Items: ");
            gets(buff2);
            inventory[i].noItems=atoi(buff2);
            printf("Enter expiry (if applicable): ");
            gets(inventory[i].expiry);
            printf("Enter type of Item: ");
            gets(inventory[i].type);
            printf("Enter other Details: ");
            gets(inventory[i].detail);
         }  
         *arrayL=i-1;
    }
    but it still doesnt work...

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is yours, amended to work (as few changes as possible). I guess it does what you want.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXchar 100
    
    typedef struct
    {
      char  name[50];
      float price;
      int   noItems;
      char  expiry[50];
      char  type[50];
      char  detail[50];
    } structure;
    
    int addItems(structure inventory[], int *arrayL)
    {
      int   i;
      int   z, n;
      char  buff1[MAXchar];
      char  buff2[MAXchar];
    
      /* Stemplate(); */
      printf("Please enter number of items you wish to enter: ");
      scanf("%d", &z);
      fflush(stdin);
    
      printf("Please enter inventory items\n");
    
      for (i = *arrayL, n = 0; n < z; i++, n++)
      {
        printf("Enter Item name: ");
        gets(inventory[i].name);
        printf("Enter Item price: ");
        gets(buff1);
        inventory[i].price = atoi(buff1);
        printf("Enter number of Items: ");
        gets(buff2);
        inventory[i].noItems = atoi(buff2);
        printf("Enter expiry (if applicable): ");
        gets(inventory[i].expiry);
        printf("Enter type of Item: ");
        gets(inventory[i].type);
        printf("Enter other Details: ");
        gets(inventory[i].detail);
      }
    
      *arrayL = i;
      return(1);
    }
    
    void PrintMe(structure *s)
    {
      printf
      (
        "Name:    %s\nPrice:   %f\nnoItems: %d\nexpiry:  %s\ntype:    %s\ndetail:  %s\n",
        s->name,
        s->price,
        s->noItems,
        s->expiry,
        s->type,
        s->detail
      );
    }
    
    int main(void)
    {
      structure inventory[1000];
      int       arrayL = 0;
      int       i;
    
      addItems(inventory, &arrayL);
    
      for (i = 0; i < arrayL; i++)
      {
        PrintMe(&inventory[i]);
      }
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Thanks....but i have a moved a step forward involving files...

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by nomi
    Thanks....but i have a moved a step forward involving files...
    And that has what to do with loading data into structures exactly?

    I take it you're done with this thread then. Shame I wasted my time answering your question.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Noticed i have offended some ppl...but I hope to still receive help...

    No hard feeling.....rite???

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>No hard feeling.....rite???
    Nah, no hard feelings

    Just try to remember people give their time freely, so a small thank you can go a long way.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM