Thread: Questions to Structure - Beginner needs help!

  1. #31
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Two little questions:

    What means "len"? Length? Is it an "int"?
    What function has "len = strlen(list[i].word);"? PS: I know the meaning of strlen, but why do I use it here? Just to put an '\0' to the end of the string?
    Last edited by Fresa; 02-29-2012 at 07:37 AM.

  2. #32
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    It stopped by word 336.
    I tried an other textfile. It consists just 50 times the same word.
    However, now, there is no error-message (in contrast to Demodatei.txt). It print out 50 times this word and then the total count:50 and then total duplexes: 1225... strange...

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Several changes are needed. Study this:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct alist {
       char word[26];
       int count;
    };
    
    int main(void) {
       FILE * fp;
       int i,j,len,totalWords, wasDup;
       struct alist list[600];
    
       
       if((fp=fopen("Edelweiss.txt","r")) == NULL) {
             printf("Error! File did not open - closing program.\n");
             return 1;
       }
       printf("\n Word Analysis:\n\n                            Word    Frequency\n");
       printf("  ============================================\n");
       i = totalWords = wasDup = 0;
       while((fscanf(fp, "%s", list[i].word)) > 0) {
          len = strlen(list[i].word);
          while(!isalpha(list[i].word[len])) 
             list[i].word[len--]= '\0';
    
          for(j=0;j<i;j++) {
             if(!strcmp(list[j].word, list[i].word)) { //words match
                list[j].count++;                        //add to the tally
                --i;
                wasDup=1;                               //it was a duplicate word
             }
          }
          if(!wasDup)                                  //it was not a duplicate word
             list[i].count++;
          wasDup=0;
    
          //printf("%4d. %26s %4d\n",i+1,list[i].word, list[i].count);
          ++i; 
          ++totalWords;
       }
       fclose(fp);
       
       for(j=0;j<i;j++) 
          printf("%4d. %26s %4d\n",j+1,list[j].word, list[j].count);
    
       printf("\n Total Words: %d\n", totalWords);
       return 0;
    }
    Edelweiss.txt is this:
    Edelweiss, edelweiss, every morning you greet me.
    Small and white, clean and bright, you look happy to meet me.
    Blossom of snow may you bloom and grow,
    bloom and grow forever.
    Edelweiss, edelweiss, bless my homeland forever.

  4. #34
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Wow thanks. I don't understand it 100%, but before I will bombard you with lots of questions I want to brood over your code some minutes more.

    But looks it on your computer like this, too?
    Questions to Structure - Beginner needs help!-unbenannt-jpg

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, but that may have not been the version I meant to post.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct alist {
       char word[26];
       int count;
    };
    
    int main(void) {
       FILE * fp;
       int i,j,len,totalWords, totalDupes, wasDup;
       struct alist list[600];
    
       
       if((fp=fopen("Edelweiss.txt","r")) == NULL) {
             printf("Error! File did not open - closing program.\n");
             return 1;
       }
       printf("\n Word Analysis:\n\n                            Word    Frequency\n");
       printf("  ============================================\n");
       i = totalWords = wasDup = totalDupes = 0;
       while((fscanf(fp, "%s", list[i].word)) > 0) {
          len = strlen(list[i].word);
          while(!isalpha(list[i].word[len])) 
             list[i].word[len--]= '\0';
    
          for(j=0;j<i;j++) {
             if(!strcmp(list[j].word, list[i].word)) { //words match
                list[j].count++;                        //add to the tally
                --i;
                wasDup=1;                               //it was a duplicate word
                ++totalDupes;
             }
          }
          if(!wasDup)                                  //it was not a duplicate word
             list[i].count++;
          wasDup=0;
    
          ++i; 
          ++totalWords;
       }
       fclose(fp);
       
       for(j=0;j<i;j++) 
          printf("%4d. %26s %4d\n",j+1,list[j].word, list[j].count);
       
       j = (totalWords - totalDupes); 
       printf("\nTotal Words: %d  Duplicate Words: %d  Unique Words: %d\n",totalWords,totalDupes,j);
       return 0;
    }
    Output:
    Code:
     Word Analysis:
    
                                Word    Frequency
      ============================================
       1.                  Edelweiss    2
       2.                  edelweiss    2
       3.                      every    1
       4.                    morning    1
       5.                        you    3
       6.                      greet    1
       7.                         me    2
       8.                      Small    1
       9.                        and    4
      10.                      white    1
      11.                      clean    1
      12.                     bright    1
      13.                       look    1
      14.                      happy    1
      15.                         to    1
      16.                       meet    1
      17.                    Blossom    1
      18.                         of    1
      19.                       snow    1
      20.                        may    1
      21.                      bloom    2
      22.                       grow    2
      23.                    forever    2
      24.                      bless    1
      25.                         my    1
      26.                   homeland    1
    
    Total Words: 37  Duplicate Words: 11  Unique Words: 26
    Last edited by Adak; 02-29-2012 at 09:01 AM.

  6. #36
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Sorry I compared my code several times with yours and I can't find my mistake. But I have a similar output like I postet some minutes ago.
    If I try to open my Demodatei.txt the program "crashed".

    No I just copied your code (into microsoft visual studio) I get this. It looks better, but the frequency ist not correct:
    Questions to Structure - Beginner needs help!-unbenannt-jpg

    This is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct alist {
        char word[26];
        int count;
    };
     
    int main(void) 
    {
        FILE * fp;
        int i, wasduplex, j, len, total, totalduplex;
        struct alist list[600];
        
        if((fp=fopen("Edelweiss.txt","r+")) == NULL) 
        {
            printf("Error! File did not open - closing program.\n");
            return 1;
        }
        else
        {
            printf("\n Word Analysis:\n\n                          Word    Frequency\n");
            printf("  ==========================================\n");
            i = total = wasduplex = totalduplex = 0;
            while((fscanf(fp, "%s", list[i].word)) > 0 ) 
                {
                    len = strlen(list[i].word);
    
                    while(!isalpha(list[i].word[len]))
                        {
                            list[i].word[len--]='\0';
                        }
    
                    for(j=0;j<i;j++)
                        {
                            if((strcmp(list[j].word, list[i].word)) /* == 0*/) 
                                {
                                    list[j].count++;
                                    i--;
                                    wasduplex=1;
                                    totalduplex++;
                                }
                        }
                    
                    if(!wasduplex) {list[i].count++;}
                    wasduplex=0;
                    i++;
                    total++;
                }
    
        fclose(fp);
    
        for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);}
    
        j = (total - totalduplex);
        printf("\nTotal Words: %d  Duplicate Words: %d  Unique Words: %d\n",total,totalduplex,j);
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }
    Where is the critical and unseen mistake???

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You removed the ! from the strcmp() line of code, is the first thing I see.

  8. #38
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Oh no!!! That's not true!
    Okay, now it is the same like if I copy your code... oh noooo... I hate such things! This little sign destroys the whole program!
    Okay, but the frequency is still not shown correct(ly).

    Does your program work with the "Demodatei.txt", which I have uploaded last page?
    Do you sleep sometimes? It's crazy, that you can help me the whole day. But I didn't want to deplore it !!
    Last edited by Fresa; 02-29-2012 at 09:38 AM.

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You put the ! back into the strcmp() line of code, right?

  10. #40
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Yes. And now it doesn't look like post #34 anymore, but it looks like post #36. The frequency seems to have the wrong type, but there is no difference to your code...

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course there is a difference!

    You're messing with me, right?

    Post the code as it is right now, and I'll see what I can find, in more detail.

  12. #42
    Registered User
    Join Date
    Feb 2012
    Posts
    47


    Okay, here is the code, but I compared it lots of times... ok, I haven't also seen the missing "!"... but... look:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct alist {
        char word[26];
        int count;
    };
     
    int main(void) 
    {
        FILE *fp;
        int i, wasduplex, j, len, total, totalduplex;
        struct alist list[600];
        
        if((fp=fopen("Edelweiss.txt","r+")) == NULL) 
        {
            printf("Error! File did not open - closing program.\n");
            return 1;
        }
        else
        {
            printf("\n Word Analysis:\n\n                          Word    Frequency\n");
            printf("  ==========================================\n");
            i = total = wasduplex = totalduplex = 0;
            while((fscanf(fp, "%s", list[i].word)) > 0 ) 
                {
                    len = strlen(list[i].word);
    
                    while(!isalpha(list[i].word[len]))
                        {
                            list[i].word[len--]='\0';
                        }
    
    //                k = i;
    
                    for(j=0;j<i;j++)
                        {
                            if((!strcmp(list[j].word, list[i].word)) /* == 0*/) 
                                {
                                    list[j].count++;
                                    i--;
                                    wasduplex=1;
                                    totalduplex++;
                                }
                        }
                    
                    if(!wasduplex) {list[i].count++;}
                    wasduplex=0;
                    i++;
                    total++;
                }
    
        fclose(fp);
    
        for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);}
    
    //    printf("\ntotal count: %d",i);
    //    printf("\ntest: total duplexes: %d",d);
        j = (total - totalduplex);
        printf("\nTotal Words: %d  Duplicate Words: %d  Unique Words: %d\n",total,totalduplex,j);
    //    printf("\n");
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }

  13. #43
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Can you find the mistake/difference or not?

    I have found out two new problems.
    - If you write " ... " in your file, the program doesn't work (three points included of two blank spaces).
    - If there will be missing a blank space after a point, so program does not identify the word correct. Example: "The dog is black.The cat is white." The computer doesn't list "black" and "The", but "black.The" ...

    I hope I don't needle you

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make your line 39, the same as mine -- exactly. I think it's goofed on that one line, even though they look almost the same, your compiler is treating it differently.

    That's part of the definition of a word - one or more letters, terminating in a punctuation mark, an end of line (newline to us), or a space.

    There is no such word as "black.The", so naturally I didn't include such a provision in my example program. . . . will be handled. I did expect that detail - just putting it off for now.

    Get this right, and then on to sorting the names, and sorting by frequency of the words.
    Last edited by Adak; 02-29-2012 at 10:29 AM.

  15. #45
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    no success:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    struct alist {
        char word[26];
        int count;
    };
     
    int main(void) 
    {
        FILE *fp;
        int i, wasduplex, j, len, total, totalduplex;
        struct alist list[600];
        
        if((fp=fopen("Edelweiss.txt","r+")) == NULL) 
        {
            printf("Error! File did not open - closing program.\n");
            return 1;
        }
        else
        {
            printf("\n Word Analysis:\n\n                          Word    Frequency\n");
            printf("  ==========================================\n");
            i = total = wasduplex = totalduplex = 0;
    
            while((fscanf(fp, "%s", list[i].word)) > 0 ) 
                {
                    len = strlen(list[i].word);
    
                    while(!isalpha(list[i].word[len]))
                        {
                            list[i].word[len--]='\0';
                        }
    
    //                k = i;
    
                    for(j=0;j<i;j++)
                        {
                           if(!strcmp(list[j].word, list[i].word))
                                {
                                    list[j].count++;
                                    i--;
                                    wasduplex=1;
                                    totalduplex++;
                                }
                        }
                    
                    if(!wasduplex) {list[i].count++;}
                    wasduplex=0;
                    i++;
                    total++;
                }
    
        fclose(fp);
    
        for(j=0;j<i;j++) {printf("%4d %26s %4d\n",j+1,list[j].word, list[j].count);}
    
    //    printf("\ntotal count: %d",i);
    //    printf("\ntest: total duplexes: %d",d);
        j = (total - totalduplex);
        printf("\nTotal Words: %d  Duplicate Words: %d  Unique Words: %d\n",total,totalduplex,j);
    //    printf("\n");
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }
    EDIT: I can copy your whole code: same problem
    Last edited by Fresa; 02-29-2012 at 10:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some beginner questions.
    By Kitt3n in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2010, 04:18 PM
  2. A few beginner's questions
    By Megidolaon in forum C++ Programming
    Replies: 33
    Last Post: 10-24-2008, 09:21 AM
  3. Beginner's Questions
    By bjl in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2008, 06:56 AM
  4. beginner questions.
    By Jaken Veina in forum C Programming
    Replies: 5
    Last Post: 03-16-2005, 09:38 PM
  5. several beginner questions...
    By Taikon in forum C Programming
    Replies: 2
    Last Post: 02-09-2005, 09:53 AM