Struct list

This is a discussion on Struct list within the C Programming forums, part of the General Programming Boards category; ooh i was creating an program for dictionary and words....and the problem is that it ships over the loop for ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    Struct list

    ooh i was creating an program for dictionary and words....and
    the problem is that it ships over the loop for putting a word for container and goes to definition.

    Code:
    #include <stdio.h>
    
    
    struct entry
    {
    	char *word;
    	char *definition;
    };
    
    
    
    int main(void)
    {
        //char *word = "Justin";
        //printf("%s",word);
    
    char letter,answer,array[80];
    
    
    
    int num,temp=0;
    printf("How many entries to define?\n");
    scanf("%d",&num);
    
    
    
    struct entry list[num];
    
    
    
    do
    {
    
    printf("Enter words\n");
    
    int i=0;
    
    
    do
    {
                 letter = getchar();
                 array[i] = letter;
                 ++i;
    }while(letter != '\n');
    
    array[i-1] = '\0';
    
    list[temp].word = array;
    
    printf("%s\n",list->word);
    
    
    printf("---------------------------------------\n");
    
    printf("Enter definitions\n");
    
    i=0;
    
    
    do //inputing sentences into definition
    {
                 letter = getchar();
                 array[i] = letter;
                 ++i;
    }while(letter != '\n');
    
    array[i-1] = '\0';
    
               list[temp].definition= array;
    
    ++temp;
    
    printf("%s\n",list -> definition);
    
    printf("Do you wish to  make another entry(y/n)?");
    
    scanf("%c",&answer);
    
    
    
    }  while(answer != 'y'); //end entire loop
    
    
    }
    oooh by the way if u see the while weird is because im was going to change the struct entry list[num];, and make it soo that every time it loops there would be 1 additional more memory location for list but i'll do that later seen the problem is with the word loop...

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
       printf("How many entries to define?\n");
        scanf("%d",&num);
    
        struct entry list[num];
    First of all your code need a proper indentation. And in the above code while declaring an array your array size need to be const. something like this

    Code:
        struct entry list[10];
    it cant depend on a variable. C standard.

    Code:
    void clear_buffer(void)
    {
        int ch;
        
        while((ch=getchar()) != '\n' && ch != EOF);
    }
    use this function here
    Code:
     printf("Enter words\n");
            j =0;
            clear_buffer();
            do
            {
                 letter = getchar();
                 array[j] = letter;
                 ++j;
    This clears the input buffer. That u got avoid using scanf funtion. And the problemw which u where getting likely because of scanf.

    Code:
            printf("%s\n",list -> definition);
            printf("Do you wish to  make another entry(y/n)?");
            scanf("%c",&answer);
        }while(answer != 'n' && i < num); //end entire loop
    and u got tocheck for boundry of the array. becuase C dosn't check for array boundries

    ssharish2005
    Last edited by ssharish2005; 12-31-2006 at 04:46 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are you using some language extension for C90 or C++, or C99?

    I'll assume you want C, and recommend looking into malloc to dynamically allocate space into which you can write strings. A pointer to unallocated memory should not be written to.

    Also, look in the FAQ for improved methods for user input. My short version: always read user input as a string, convert to this string to numeric if necessary (fgets and sscanf to begin).
    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.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Perhaps some application of the fine art of indenting code as well Darkinyuasha1
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    thanks but can u explain the clear_buffer func..a bit more soo i can get a better understand of it...sorry im a bit slow ^_^ heh. oooh and this is C without no extension but i was thinking about using link list and malloc...


    p.s const struct entry list[10]; wouldn't work or const would work in most of this program because your inputing variables into the container
    Last edited by Darkinyuasha1; 12-31-2006 at 08:03 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Quote Originally Posted by Salem
    Perhaps some application of the fine art of indenting code as well Darkinyuasha1
    Yes the art of indention is powerful lol....

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    > thanks but can u explain the clear_buffer func
    Looking a little different it's
    Code:
    int junk;
    do 
    {
       junk = getc(stdin);
    } 
    while(junk != EOF && junk != '\n');
    Now you can clearly see that we're just looping a call to one of the getc* functions in an attempt to clean up the input stream, because we were supplied with more data than we needed.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Quote Originally Posted by citizen
    > thanks but can u explain the clear_buffer func
    Looking a little different it's
    Code:
    int junk;
    do 
    {
       junk = getc(stdin);
    } 
    while(junk != EOF && junk != '\n');
    Now you can clearly see that we're just looping a call to one of the getc* functions in an attempt to clean up the input stream, because we were supplied with more data than we needed.

    thanks now thats clear up and my program works..i can go back to do my other side programs ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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