Thread: Headers

  1. #1
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23

    Headers

    Hi

    I have several headers in my program, in main.h are defined and initialized all global variables, then I include main.h in other headers, so functions could access global variables.
    I get a warning for every global variable that has been defined in both modul.
    I tried with "extern" but still does not work.

    btw - program crashes

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Show us your code.

    ssharish2005

  3. #3
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23
    main.c
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "main.h"
    #include "utils.h"
    #include "file.h"
    
    int main()
    {
            int i;
            int value;//star location
    
            //alocate the memory
            for(i = 0; i < MAX_PRODUCT; i++)
            {
                    product[i] = (struct goods *)malloc(sizeof(struct goods));
                    if (product[i] == NULL)
                    {
                            _setcursortype(_NOCURSOR);
                            clrscr();
                            printf("Not enough memory!");
                            getch();
                            goto out;
                    }
                    product[i]->status = FREE;
            }
    
            //save some products in memory
            strcpy(product[0]->ProductName, "MILK");
            product[0]->code = 123;
            product[0]->price = 2;
            product[0]->supply = 30;
            product[0]->status = TAKEN;
            strcpy(product[1]->ProductName, "JUICE");
            product[1]->code = 121;
            product[1]->price = 2.39;
            product[1]->supply = 30;
            product[1]->status = TAKEN;
            strcpy(product[2]->ProductName, "CHOCOLATE");
            product[2]->code = 567;
            product[2]->price = 1.5;
            product[2]->supply = 30;
            product[2]->status = TAKEN;
            strcpy(product[3]->ProductName, "APPLES");
            product[3]->code = 852;
            product[3]->price = 1.25;
            product[3]->supply = 30;
            product[3]->status = TAKEN;
    
            do
            {
                    value = Menu();
                    //turn on cursor
                    _setcursortype(_NORMALCURSOR);
                    switch(value)
                    {
                            case 0: EnterProduct();
                                    break;
                            case 1: DeleteProduct();
                                    break;
                            case 2: FindProduct();
                                    break;
                            case 3: SellProduct();
                                    break;
                            case 4: DisplayProducts();
                                    break;
                            case 5: LoadDatabase();
                                    break;
                            case 6: SaveDatabase();
                                    break;
                            case 7: CheckSave(z);
                                    break;
                    }
            }while(value != 7);
    
            //free the memory
            for (i = 0 ; i < MAX_PRODUCT; i++)
            {
                    free(product[i]);
            }
    
            out:;
            return 0;
    }
    main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    #define MAX_PRODUCT 100 //maximum number of products
    #define TAKEN 1         //prevent writing in memory location
    #define FREE 0          //make memory location writeable
    #define ARROW_UP 328    
    #define ARROW_DOWN 336
    #define RETURN 13
    
    void CheckSave(int r);
    void SortProducts(void);
    void DisplayProducts(void);
    void FindProduct(void);
    void SellProduct(void);
    void EnterProduct(void);
    int KeyPressed(void);
    int Menu(void);
    
    struct goods
    {
    	char ProductName[15];
    	int code;
            int supply;
            int status;
    	float price;
    }*product[MAX_PRODUCT];
    
    int z = 1;//products saved?
    char buf[BUFSIZ];
    
    #endif //MAIN_H
    delete.c
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "main.h"
    #include "utils.h"
    
    void DeleteProduct(void)
    {
            int j = 0;
            char option;
            int code;
    
            do
            {
                    back:;
                    clrscr();
                    printf("DELETE PRODUCT\n\n");
                    printf("Code: ");
                    fgets (buf, sizeof buf, stdin);
                    if(sscanf (buf, "%d", &code) != 1)
                    {
                            fprintf (stderr, "Invalid input!");
                            getch();
                            goto back;
                    }
                    //find and delete a product
                    for(j = 0; j < MAX_PRODUCT; j++)
                    {
                            if(product[j]->code == code)
                            {
                                    z = 0;
                                    product[j]->code = 0;
                                    product[j]->status = FREE;
                                    printf("Product has been deleted.\n\n");
                                    printf("Delete another product?(Y/N)\n");
                                    option = getch();
                                    break;
                            }
                    }
                    if(j == MAX_PRODUCT)
                    {
                            printf("Code %d doesn't belong to any product.\n", code);
                            printf("Delete another product?(Y/N)");
                            option = getch();
                    }
            }while(option == 'Y' || option == 'y');
    }
    Here is my "combined" piece of code, I hope that is pasted correctly.
    I know about conio, so please spare me - next time I promise there wont be any unstandared library

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, its working fine for me

    i couldn't check the other part thats is about your program crashes. cose i dint have your one more header file.h. apart from that i get the values of MAX_PRODUCT from the main.h which is included in main.c

    mean time which do u need that out:; for???

    ssharish2005

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ssharish2005
    mean time which do u need that out:; for???
    The label (followed by a null statement) for the goto.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I get a warning for every global variable that has been defined in both modul.
    I tried with "extern" but still does not work.
    A global variable can only exist once -- you can declare it in other files as extern to access the same variable. To have a "global" variable that is private to a source file (so each source file will have a separate variable), use a static global variable.
    Code:
    const static double PI = 3.14159265358979324;
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I get a warning for every global variable that has been defined in both modul.
    You shouldn't have definitions in header files, otherwise you get "multiply defined" errors when you include the file in several source files.

    So in the header, you would have
    Code:
    struct goods
    {
    	char ProductName[15];
    	int code;
            int supply;
            int status;
    	float price;
    };
    
    extern struct *product[MAX_PRODUCT];
    extern int z;//products saved?
    extern char buf[BUFSIZ];
    And in main.c,
    Code:
    struct goods *product[MAX_PRODUCT];
    int z = 1;//products saved?
    char buf[BUFSIZ];
    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 marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23
    Thank you guys, especially you Salem, now it works correctly.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Dave_Sinkula
    The label (followed by a null statement) for the goto.
    Ohhh thanks Dave, i never saw his for loop.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM