Thread: Add elements into an array in C

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Add elements into an array in C

    Hello everyone! I have to make a program - french-english dictionary controled by a menu with three functions: search, add and remove words. I have a problem with the add function but i don't now what is the problem. I'm posting the entire code here just in case:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char words[][2][40]={
          {"adorer","adore"},
          {"gras","greasy"},
          {"outil","tool"},
          {"radis","radish"},
          {"tracer","trace"},
          {"",""}
    };
    
    
    void Search(void);
    int menu(void);
    void Save(void);
    void Load(void);
    void Display(void);   
    void Add(void);
    
    int main(void){
        int choice;
    
        //Load();
    
        do{
            choice = menu();
    
            switch(choice){
                case 1: Load();
                    break;
    
                case 2: Save();
                    break;
    
                case 3: Search();
                    break;
    
                case 4: Display();
                    break;
    
                case 5: Add();
                    break;
          }
        }while(choice!=6);
    
        return 0;
    }
    
    
    int menu(void){
        char str[80];
        int i;
    
        printf("1. Load\n");
        printf("2. Save\n");
        printf("3. Search\n");   
        printf("4. Display\n");
        printf("5. Add new words\n");
    
    
        do{
            printf("Enter your choice:(1-5) \n");
            gets(str);
            i=atoi(str);
            printf("\n");
        }while(i<1 || i>5);
    
        return i;
    }
    
    
    void Search(void){
    
        char word_fr[40];
        int i, size;
    
        size=sizeof(words)/sizeof(words[0]);
    
        printf("Enter french word: ");
        gets(word_fr);
    
        for(i=0; i < size; i++)
            if(!strcmp( word_fr, words[i][0]))
                printf("%s %s\n", words[i][0], words[i][1]);
    
    
    }
    
    void Save(void){
         FILE *fp;
         int i,size;
    
         size=sizeof(words)/sizeof(words[0]);
    
         if(( fp=fopen( "diction.txt", "w" )) == NULL) {
              printf("Save says: Cannot open file.\n");
              exit(1);
         }
        
         for(i=0; i < size; i++) {
             fprintf( fp, "%s %s ", words[i][0], words[i][1]);
         }
         fclose(fp);
    }
    
    void Load(void)
    {
    
        FILE *fp;
        int i,size;
    
        size=sizeof(words)/sizeof(words[0]);
    
        if(( fp=fopen( "diction.txt", "w+" )) == NULL ) {
            printf("Load says: Cannot open file.\n");
            exit(1);
        }
    
        for(i=0; i < size; i++) {
            fscanf( fp, "%s %s", words[i][0], words[i][1]);
        }
        fclose(fp);
        printf("The file was loaded successfully.\n");
    }
    
    void Display(void)
    {
        int i,size;
    
        size=sizeof(words)/sizeof(words[0]);
    
        for(i=0; i < size; i++) {
            printf("%s %s\n", words[i][0], words[i][1]);
        }
    }
    
    void Add(void)
    {
    
    
        char str[40];
        int size;
    
        size=sizeof(words)/sizeof(words[0]);
    
        printf("Enter french word:\n ");
        gets(str);
    
    
        strcpy(words[size][0], str);
    
        printf("Enter english word:\n ");
        gets(str);
    
        strcpy(words[size][1], str);
    
        Save();
    }
    I'm using Turbo C/C++ because i'm obliged to present my project on this compiler. I'll be thankful if someone helps me.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Marria View Post
    I have a problem with the add function but i don't now what is the problem.
    We don't either since you are the only one that has seen wha happens (or does not happen).
    You need to describe the problem.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    When i choose "5" from the menu, i enter the french and the english word and press "ENTER" it gives me this:

    Code:
    Enter french word: musique
    Enter english word: music
    musique2. Save
    3. Search
    4. Display
    musicusicEnter your choice:(1-5)
    And it's not adding the words in the array.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe engage in the discussion on your other thread over here -> Writing, Reading And Searching In A File In C - C And C++ | Dream.In.Code

    Because it seems like you're just wandering from one forum to another hoping someone will fix all your issues without you having to understand anything, or do anything.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Salem all fryday i'm searching in the net and the forums for people with the same problems, i changed tons of things in my program and i'm not stopping doing it. But i'm sorry that i dont understand where the problem is. Maybe i really should do something more...
    Sorry for the inconvinience.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    I don't think size=sizeof(words)/sizeof(words[0]); is doing what you want.

    When you add an entry, simply count it.
    This count tells you exactly how many entries you have at all times. New entry, increase the count.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The flaw is in your thread title. You can't add elements to an array in C. You can only copy elements into an array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2011, 11:22 PM
  2. Sum of all elements in an array
    By SilentPirate007 in forum C Programming
    Replies: 6
    Last Post: 05-02-2010, 08:32 AM
  3. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 10:47 PM
  4. array of zero elements
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2008, 07:10 AM
  5. # of Elements in Array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 01:43 PM