Thread: I have tried a lot...but this is not working.

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

    I have tried a lot...but this is not working.

    I have worked on this for a long time but just cant seem to figure it out...

    This is a database programme....it compiles and evrything byut the functions dont work in harmony....Most of he programme works until someone chooses to additems...

    Attached is the dataFile(WMinventory), .exe and .cpp of the programme(WMdatamase(rough))....

    If data is entered it should appear in the next six lines of the data file....

    I am really stuck here and any help would be appreciated, I know its too much too ask for, but.....help me.

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    help????

    Please....I have been trying for so long....

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >help????
    I'm afraid to compile and run your code, but in such cases I suggest that you ditch what you have and start over, this time taking a more structured approach. In other words:

    1) Create the inventory type.
    2) Write a function to fill one variable of the inventory type from interactive input.
    3) Write a function to write one variable of the inventory type to interactive output.
    6) Write a function to update one variable of the inventory type.
    4) Write a function to fill one variable of the inventory type from a file stream.
    5) Write a function to write one variable of the inventory type to a file stream.

    With these functions in hand you can do a surprising amount of useful things. More importantly, you are able to work with the smallest object in your program in its entirety. Just by using these functions in a loop, you have a database.

    Your task after that is to work with sequences of inventory objects, such as sorting and searching. Both of these at their most naive are ridiculously easy, so you should have no problem there.

    The key is to break the program down into teeny tiny pieces that you can work with and test individually so that when you put them together, they all work as they should. It saves you a lot of trouble when you have a few hundred (experienced programmers read this as hundred-thousand) lines of code and a bug that you can't seem to find.
    My best code is written with the delete key.

  4. #4
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by Prelude

    1) Create the inventory type.
    2) Write a function to fill one variable of the inventory type from interactive input.
    3) Write a function to write one variable of the inventory type to interactive output.
    6) Write a function to update one variable of the inventory type.
    4) Write a function to fill one variable of the inventory type from a file stream.
    5) Write a function to write one variable of the inventory type to a file stream.
    hmmm....okay....but can you explaing what you mean by^^^3

    >2) Write a function to fill one variable of the inventory type from interactive input.

    but then i have chars, ints and floats.....does that mean 3 diff functions.
    Last edited by nomi; 01-11-2004 at 06:08 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but then i have chars, ints and floats.....does that mean 3 diff functions.
    All of those are in a single structure variable, so you can simply fill it and use that:
    Code:
    struct inventory interactive_fill ( void )
    {
      struct inventory rv;
      char buff[BUFSIZ];
    
      printf ( "Name: " );
      fflush ( stdout );
      fgets ( rv.name, sizeof rv.name, stdin );
      printf ( "Price: " );
      fflush ( stdout );
      fgets ( buff, sizeof buff, stdin );
      sscanf ( buff, "%f", &rv.price );
      ...
    
      return rv;
    }
    My best code is written with the delete key.

  6. #6
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    thx....i'm learning new things....

    Code:
    struct inventory interactive_fill ( void )
    {
      struct inventory rv;
      char buff[BUFSIZ];
    
      printf ( "Name: " );
      fflush ( stdout );
      fgets ( rv.name, sizeof rv.name, stdin );
      printf ( "Price: " );
      fflush ( stdout );
      fgets ( buff, sizeof buff, stdin );
      sscanf ( buff, "%f", &rv.price );
      ...
    
      return rv;
    }
    What is this function doing?

  7. #7
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    I realized i dont know how to deal with functions of type struct...

    Consider my earlier programme....where was i going wrong...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >where was i going wrong...
    The only obvious problem is that saveData doesn't close the file that it opened and you don't cose it elsewhere either. A cursory look doesn't show anything else seriously wrong as long as you avoid the undefined behavior that you know about already and assuming the pretty printing works as it should.
    My best code is written with the delete key.

  9. #9
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    I have simplified my programme to these 3 functions:

    Code:
    /* STEP ONE */
    int loadData(structure inventory[],int* arrayL)
    {
        int i=0;
        char buff1[MAXchar];
        char buff2[MAXchar];
        FILE *dataFile;
        
        
        dataFile = open_file(dataFileName ,"r");
        
        while(!feof(dataFile)){
             fgets(inventory[i].name, MAXchar,dataFile);
             fgets( buff1, MAXchar, dataFile );
             fgets( buff2, MAXchar, dataFile );
             fgets(inventory[i].expiry, MAXchar,dataFile);
             fgets(inventory[i].type, MAXchar,dataFile);
             fgets(inventory[i].detail, MAXchar,dataFile);
             sscanf( buff1, "%f", &inventory[i].price );
             sscanf( buff2, "%d", &inventory[i].noItems );
             i++;
        }
        
        fflush(stdin);
        fclose(dataFile);
        
        *arrayL=i;
    }
    /* STEP TWO*/
    void addItems(structure inventory[],int *arrayL)
    {
        int i=*arrayL;
        char buff1[MAXchar];
        char buff2[MAXchar];
            
        Stemplate();
        
        printf("Please enter inventory items\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++;
        
        getchar();
    }
    /* STEP THREE*/
    void saveData(structure inventory[],int arrayL)
    {
        int i;
        int z=arrayL;
        
        FILE* dataFile;
        
        dataFile = open_file(dataFileName ,"w");
        
        for(i=0;i<(z-1);i++){
            fputs(inventory[i].name,dataFile);
            fprintf(dataFile,"%.2f\n",inventory[i].price);
            fprintf(dataFile,"%d\n",inventory[i].noItems);
            fputs(inventory[i].expiry,dataFile);
            fputs(inventory[i].type,dataFile);
            fputs(inventory[i].detail,dataFile);
        }
        fclose(dataFile);
    }
    This doesnt seem to work.....

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Code:
    int loadData(structure inventory[],int* arrayL)
    I haven't looked at the attached file, so I don't know if the above is an alias or not, but you'd use the struct keyword for structures.

    scrap the fflush(stdin) before the others catch it.
    use strtol instead of atoi
    lose gets(buf), and use fgets instead

    I *believe* you can simply the reading/writing to one function by passing in your data files from main, or open a single file for both reading and writing.

    Code:
    int fileOps(struct *, int *, FILE *, FILE *);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have simplified my programme to these 3 functions
    Your problems are coming from addItems. And since you've apparently narrowed the problem down to these three functions, it should be easy to locate where things go wrong if you've been paying attention.

    >int i=*arrayL;
    Because you use feof as a loop condition when reading the file, *arrayL probably has a value one higher than you want, which is also probably why you subtract one from it when you save the data. As such, this should also be

    int i = *arrayL - 1;

    Or you could fix the code that you "already know" is wrong. When your willful ignorance effects the normal operation of your programs, it's time to take your head out of your ass and learn to do things the right way instead of try to (poorly) work around the issues that could easily be avoided.

    >arrayL++;
    This does nothing but advance the pointer to the next block of memory, it doesn't increment arrayL the way you think it does. Try this instead:

    (*arrayL)++;

    Note that while ++ and * have the same precedence, they are associated from right to left, so the ++ is performed first, resulting in moving the pointer to the next memory block and dereferencing that address. Parentheses force the dereference to be performed first, resulting in incrementing the contents of arrayL as you would expect.

    >but you'd use the struct keyword for structures.
    It is a typedef'd structure, so the code is correct.

    >scrap the fflush(stdin) before the others catch it.
    >lose gets(buf), and use fgets instead
    nomi already asked that we not comment on these in another thread. We can inform the ignorant, but we can't help the stupid if they don't want us to.
    My best code is written with the delete key.

  12. #12
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Thx...prelude....but....i figured everything you pointed out before i even checked the post....

    Although i dont know how gets is wrong???....

    I figured that by using fgets i was putting in the '\n' character which was throwing all my formatting off course....plus that *array++....

    >nomi already asked that we not comment on these in another thread. We can inform the ignorant, but we can't help the stupid if they don't want us to.

    Is that a direct insult to me....coz if it is>...I have just started C and i currently have no plans to go professionally.....and i dont think i will ever...

    >Then Why learn C?
    Coz i had no idea about what programming was about and i always wanted to know some programming....So i have planned on learning a bit of Turing,C and Java...and besides i think you can make a lot of cool stuff with C...

  13. #13
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Moving on to phase 3....

    I decided i will scrap the fflush(stdin)
    Replacement = while(getchar () != '\n');???

    ALthough i dont know how I can replace the gets(buff)?
    >gets(buff1);
    Replacement = fgets(buff1,MAXchar,stdin);???

    Are my replacements okay? and
    Why replace gets with fgets???
    Is it bcoz of '\n', coz i have used a much longer way to take them out.....

    EDIT: cool....my program works, now to format it and put some pretty stuff in...
    Last edited by nomi; 01-12-2004 at 03:42 PM.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Although i dont know how gets is wrong???....
    Look in the Top Secret Files for super secret information.

    >Is that a direct insult to me....
    [edit]

    [/edit]

    [edit]
    I'm sorry. I'm a little off today, but I have no excuse for insulting you like that.
    [/edit]
    Last edited by Prelude; 01-12-2004 at 03:59 PM.
    My best code is written with the delete key.

  15. #15
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    >I'm sorry. I'm a little off today, but I have no excuse for insulting you like that.

    Ya....I can see you banging your head on the computer right there....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  2. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  3. Replies: 5
    Last Post: 09-19-2004, 06:52 AM
  4. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM
  5. Board Not Working Very Well Today
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-31-2001, 10:19 PM