Peculiar Problem with typedef

This is a discussion on Peculiar Problem with typedef within the C Programming forums, part of the General Programming Boards category; Hi , I have a problem with this code in this "typedef" typedef struct { char codename[120]; int money; int ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    Peculiar Problem with typedef

    Hi , I have a problem with this code in this "typedef"

    typedef struct {
    char codename[120];
    int money;
    int point;
    }item2;

    if I remove the "int point;" the code works ok. When I use "int point;" run the program and choose option 2,it shows me a windows error message "has encountered a problem and needs to close". Any suggestions? I have also included 2 files that are read with option 1 and 2. options 3-4 are under construction

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    
    
    
    void menu(void);
    void insert_points();
    void menu_choice();
    void insert_clients();
     
    
    
    typedef struct {
        int low_limit;
        int hi_limit;
        int points;
    }item1;
    
    typedef struct {
        char codename[120];
        int money;
        int point;
    }item2;
        
    item1 *array1;
    item2 *array2;
    
    
    
    int main(int argc, char *argv[])
    {
      printf("\n<<<<Welcome to Super Market Program>>>\n");
      arxi:
      
      menu(); 
      menu_choice();
    
      goto arxi;
      
      system("PAUSE");	
      return 0;
    }
    
    
    void menu(void)
    {
        
        
        printf("\nPlease choose one of the following:\n\n");
        printf("1. Insert Points File\n");
        printf("2. Insert Shopping File\n");
        printf("3. Best Clients\n");
        printf("4. Client's Data\n\n");
        printf(">");
    }
    
    void menu_choice(){
      int choice; 
     scanf("%d",&choice);
     switch(choice)
     {
       case 1:
       insert_points();        
       break;
       
       case 2:
       insert_clients();     
       break;
       
       case 3:
       break;
       
       case 4: 
       break;
       
       default:
       printf("\nERROR: The number must be between 1-4\n\n");
       break;
      }
    }
    
    
    void insert_clients()
    {}
    Attached Files Attached Files
    Last edited by kopros; 12-14-2006 at 07:01 AM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    i is unitialized in insert_clients()

    read faq on feof controling loop,

    no updating of i, so even if i is initialized, only 1 position in your array will ever change

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    avoid using goto
    no need here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    so any sugestion of what to do to fix it? I hope you understood the prob..

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Thanx for help but my prob. is located in typedef. Plz read again my problem and run the code to see what im saying

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should initialize i
    currently you don't know what memory you accessing
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    You're right indeed. Thnx a lot for the time present

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > Thanx for help but my prob. is located in typedef
    No, it isn't.
    That's just some random effect you've discovered which just moves the problem from being fatal to being invisible.

    The real problems are those which have been mentioned previously, namely your uninitialised variables.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    thanx a lot Im now having some problems with hashing.When Im tryiing to find a code t seems that I have saved in my hash table only the 1st line of my file.Any suggestions? Thnx alot for any help you ve given me until now"YOU ARE THE BEST"


    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    #include <unistd.h>
    
    #define HASHSIZE 997
    
    
    void menu(void);
    void insert_points();
    void menu_choice();
    void insert_clients();
     
     typedef struct Node
    {
    char code_name[15];
    int total_points;
    struct Node *next;
    }listNode;
    
    listNode *HashTable[HASHSIZE]; /*to * antiprosopevi dieythinsi domis gia na perasti se sinartisi*/
    
    
    int hash(char *key); 
    int hash_insert(char *key,int key_value); 
    char* hash_find(char* key);
    void hash_destroy();
    
    
    }
    Last edited by kopros; 12-14-2006 at 07:01 AM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Perhaps you depend of the sizeof that structure being a hard-coded value? Perhaps your memory allocation calls are wrong somehow?
    Code:
    int main(int argc, char *argv[])
    {
      printf("\n<<<<Welcome to Super Market Program>>>\n");
      arxi:
      
      menu(); 
      menu_choice();
    
      goto arxi;
      
      system("PAUSE");	
      return 0;
    }
    *Ahem*
    Code:
    int main(int argc, char *argv[])
    {
      printf("\n<<<<Welcome to Super Market Program>>>\n");
    
      for(;;) {
        menu(); 
        menu_choice();
      }
      
      system("PAUSE");	
      return 0;
    }
    This is a little excessive, don't you think?
    Code:
    array2 = calloc(1000000, sizeof(item2));
    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.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    I fixed the prob you mentioned but My serious problem is with hashing .Thnx for the notice anyway

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    while(! feof(File2))
    Why it's bad to use feof() to control a loop

    Code:
    		printf("Not Enough Memory!\n"); 
    		exit(EXIT_SUCCESS);
    Success? . . .
    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.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    1. Code formatting and layout.
    It's a lot easier to find things if code is layed out in a consistent manner, not randomly distributed.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    /* #defines */
    /* ======== */
    #define HASHSIZE 997
    
    /* Types */
    /* ===== */
    
    typedef struct Node {
        char code_name[15];
        int total_points;
        struct Node *next;
    } listNode;
    
    typedef struct {
        int low_limit;
        int hi_limit;
        int points;
    } item1;
    
    typedef struct {
        int money;
        char codename[15];
        int earned_points;
    } item2;
    
    /* Prototypes */
    /* ========== */
    void menu(void);
    void insert_points();
    void menu_choice();
    void insert_clients();
    
    int hash(char *key);
    int hash_insert(char *key, int key_value);
    char *hash_find(char *key);
    void hash_destroy();
    
    /* Global variables */
    /* ================ */
    item1 *array1;
    item2 *array2;
    listNode *HashTable[HASHSIZE];  /*to * antiprosopevi dieythinsi domis gia na perasti se sinartisi */
    
    /* Functions */
    /* ========= */
    int hash(char *key)
    {
        unsigned int hash_val = 0;
    
        while (*key != '\0')
            hash_val += *(key++);
    
        return (hash_val % HASHSIZE);
    }
    
    int hash_insert(char *key, int key_value)
    {
        listNode *newptr, *curr;
        int pos;
    
        pos = hash(key);
    
        /* make sure it's not already in the hash table */
        curr = HashTable[pos];
        while (curr != NULL) {
            if (!strcmp(curr->code_name, key))
                curr->total_points = curr->total_points + key_value;
            return 1;
            curr = curr->next;
        }
    
        newptr = malloc(sizeof(listNode));
        if (newptr == NULL) {
            printf("Not Enough Memory!\n");
            exit(EXIT_SUCCESS);
        }
        strcpy(newptr->code_name, key);
        newptr->total_points = key_value;
    
        newptr->next = NULL;
    
        if (HashTable[pos] == NULL)
            HashTable[pos] = newptr;
        else {
            listNode *headptr = HashTable[pos];
            HashTable[pos] = newptr;
            HashTable[pos]->next = headptr;
        }
        return 1;
    }
    
    char *hash_find(char *key)
    {
        listNode *curr;
        int pos;
    
        pos = hash(key);
        curr = HashTable[pos];
        while (curr != NULL) {
            if (!strcmp(curr->code_name, key))
                printf("%s %d", curr->code_name, curr->total_points);
            return key;
            curr = curr->next;
        }
    
        return NULL;
    }
    
    int main(int argc, char *argv[])
    {
        printf("\n<<<<Welcome to Super Market Program>>>\n");
      arxi:
    
        menu();
        menu_choice();
    
        goto arxi;
    
        system("PAUSE");
        return 0;
    }
    
    
    void menu(void)
    {
        printf("\nPlease choose one of the following:\n\n");
        printf("1. Insert Points File\n");
        printf("2. Insert Shopping File\n");
        printf("3. Best Clients\n");
        printf("4. Client's Data\n\n");
        printf(">");
    }
    
    void menu_choice()
    {
        char kodikos[15] = "ABCDEFGHIGK";
        int choice;
        scanf("%d", &choice);
        switch (choice) {
        case 1:
            insert_points();
            break;
    
        case 2:
            insert_clients();
            break;
    
        case 3:
            hash_find(kodikos);
            break;
    
        case 4:
            break;
    
        default:
            printf("\nERROR: The number must be between 1-4\n\n");
            break;
        }
    }
    
    
    void insert_clients()
    {
        int s, n;
        int i = 0;
        FILE *File2;
    
        File2 = fopen("clients.txt", "r");
        array2 = calloc(1000000, sizeof(item2));
    
        while (!feof(File2)) {
    
            fscanf(File2, "%[^;];%d", array2[i].codename, &array2[i].money);
            if ((array2[i].money > array1[0].low_limit)
                && (array2[i].money < array1[0].hi_limit)) {
                array2[i].earned_points = array1[0].points;
            }
            if ((array2[i].money > array1[1].low_limit)
                && (array2[i].money < array1[1].hi_limit)) {
                array2[i].earned_points = array1[1].points;
            }
            if ((array2[i].money > array1[2].low_limit)
                && (array2[i].money < array1[2].hi_limit)) {
                array2[i].earned_points = array1[2].points;
            }
            if ((array2[i].money > array1[3].low_limit)
                && (array2[i].money < array1[3].hi_limit)) {
                array2[i].earned_points = array1[3].points;
            }
            if ((array2[i].money > array1[4].low_limit)
                && (array2[i].money < array1[4].hi_limit)) {
                array2[i].earned_points = array1[4].points;
            }
            printf("%s %d %d", array2[i].codename, array2[i].money,
                   array2[i].earned_points);
    
            hash_insert(array2[i].codename, array2[i].earned_points);
        }
    
        fclose(File2);
    }
    
    void insert_points()
    {
        int s, n;
        int i = 0;
        FILE *File1;
    
        File1 = fopen("points.txt", "r");
    
        fscanf(File1, "%d", &n);
        printf("%d\n", n);          /*This is for the first line that shows the line number */
        array1 = calloc(n, sizeof(item1));  /*Don't cast the return value of malloc or calloc */
    
        for (i = 0; i < n; i++) {   /*You're reading n values, not n+1, so the condition shouldn't be i<=n */
            fscanf(File1, "%d;%d;%d", &array1[i].low_limit,
                   &array1[i].hi_limit, &array1[i].points);
            printf("%d %d %d\n", array1[i].low_limit, array1[i].hi_limit,
                   array1[i].points);
        }
        free(array1);
        fclose(File1);
    }
    2. None of the malloc or file reading functions bother to check for errors.

    3. Is this a typo?
    char kodikos[15]="ABCDEFGHIGK";

    4. Reduce the size
    #define HASHSIZE 997
    Debug the program with a 7 entry hash table and a 2 line input file.
    Then go through your code one statement at a time to make sure it's doing the right thing.
    When all seems well, then you can scale up.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  3. typedef problem
    By anarchypower in forum C Programming
    Replies: 8
    Last Post: 12-31-2006, 04:47 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM

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