Can someone please check this out?

This is a discussion on Can someone please check this out? within the C Programming forums, part of the General Programming Boards category; When I compile and run this code I can enter values to the array, and then terminate the loop by ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    Can someone please check this out?

    When I compile and run this code I can enter values to the array, and then terminate the loop by pressing n, which is OK. It will then print of what I entered and exit the program. But when I run the program again and enter more data, and then break the loop, it will only print of to me the input from the last time I executed the program, and anything after that is just a load of unreadable garbage. So it is not appending the next structure in the array like i want it to. The idea is that each time data is added it just saves it to the next empty structure. I want that break in there aswell so the user can quit when he/she wants to.

    Is it something to do with the break statement? And Quzah, The code has now been sorted out, so I would be grateful if you could help me.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct database {
            char prod[60];
            float cal;
            float carb;
            float prot;
            float fat;
    };
    
    static float getnum(void);
    
    int main(void)
    {
    
            enum {
                    MAX = 10
            };
    
            struct database mainm[MAX];
    
            char *p;
            int j;
            FILE *fp;
    
            for (j = 0; j < MAX; j++) {
                    
                    printf("\nAnother? (Y/N) "); 
                    if(getch() == 'n') break;
    
                    (void) system("cls");
                    printf("\nEnter the product: ");
                    (void) fflush(stdout);
                    (void) fgets(mainm[j].prod, 60, stdin);
                    if ((p = strchr(mainm[j].prod, '\n')) != NULL) {
                            *p = '\0';
                    }
                    (void) system("cls");
    
                    printf("\nNow Please Enter The Nutritional Information: ");
                    (void) fflush(stdout);
    
                    printf("\nEnter cal: ");
                    (void) fflush(stdout);
                    mainm[j].cal = getnum();
    
                    printf("\nEnter carb: ");
                    (void) fflush(stdout);
                    mainm[j].carb = getnum();
    
                    printf("\nEnter protein: ");
                    (void) fflush(stdout);
                    mainm[j].prot = getnum();
    
                    printf("\nEnter fat: ");
                    (void) fflush(stdout);
                    mainm[j].fat = getnum();
            }
    
            (void) system("cls");
    
            fp = fopen("output.dat", "ab");
            if (fp == NULL) {
                    perror("Unable to open file");
                    exit(EXIT_FAILURE);
            }
            fwrite(mainm, sizeof(struct database), MAX, fp);
            if (fclose(fp) != 0) {
                    perror("fclose");
                    exit(EXIT_FAILURE);
            }
    
            fp = fopen("output.dat", "rb");
            if (fp == NULL) {
                    perror("Unable to open file");
                    exit(EXIT_FAILURE);
            }
            fread(mainm, sizeof(struct database), MAX, fp);
            if (fclose(fp) != 0) {
                    perror("fclose");
                    exit(EXIT_FAILURE);
            }
    
            for (j = 0; j < MAX; j++) {
                    (void) system("cls");
                    printf
                        ("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    printf
                        ("\n\nThe nutritional values for item #%d (%s) are: \n\n",
                         j + 1, mainm[j].prod);
                    printf("CALORIES - %.2f\n", mainm[j].cal);
                    printf("CARBOHYDRATES - %.2f\n", mainm[j].carb);
                    printf("PROTEIN - %.2f\n", mainm[j].prot);
                    printf("FAT - %.2f\n\n", mainm[j].prot);
                    printf
                        ("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    printf("\n\nPress return for next product...\n");
    
                    (void) getchar();
    
            }
    
            return 0;
    }
    
    
    static float getnum(void)
    {
    
            enum {
                    BUFMAX = 12
            };
            char *p;
            char buf[BUFMAX] = { (char) 0 };
            float num;
    
            (void) fgets(buf, BUFMAX, stdin);
            if ((p = strchr(buf, '\n')) != NULL) {
                    *p = '\0';
            }
    
            if ((sscanf(buf, "%f", &num)) != 1) {
                    (void) puts("Error: call to sscanf() failed.");
                    exit(EXIT_FAILURE);
            } else {
                    return num;
            }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It only prints what's in the file the first time, because that's all you ever tell it to print. You never loop through the file's contents displaying them. You read in the first 10 and stop.

    Also, all of those void casts and such are pointless unless you're running your program through lint or something. They're otherwise just an eyesore.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    >> Also, all of those void casts and such are pointless unless you're running your program through lint or something.

    Splint, in the case of this particular program.

    >> They're otherwise just an eyesore.

    Ah, but beauty is in the eye of the beholder.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
            enum {
                    MAX = 10
            };
    That's kind of weird. Why don't you just use a const int or a #define?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by dwks
    Code:
            enum {
                    MAX = 10
            };
    That's kind of weird. Why don't you just use a const int or a #define?

    FWIW:

    "Define numbers as constants, not macros. C programmers have traditionally used #define to manage magic number values. The C preprocessor is a powerful, but blunt tool, however, and macros are a dangerous way to program because they change the lexical structure of the program underfoot. Let the language proper do the work. In C and C++, integer constants can be defined with an enum statement..." from The Practice of Programming by Brian W. Kernighan and Rob Pike, pg 20
    "There is one advantage to enums: unlike #defined names which are typically discarded during compilation, enum names usually persist through to the debugger, and can be used while debugging your code." from Expert C Programming, Deep C Secrets by Peter Van Der Linden, pg 74
    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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