Thread: Questions to Structure - Beginner needs help!

  1. #16
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    @Adak: I just tried your program. It runs nearly perfect. Thank you. I tested it with a txt-file like this:
    olo olo olo
    olo olo olo ...
    olo olo olo
    And it counts 10 words, but there are just 9 ...
    I don't understand this line/command:
    Code:
    while((fscanf(fp, "%s", list[i].word)) > 0)

    The other lines are clear.
    I have also a little question, that doens't remains (could I say it in this way?) with c-programming: What is the long form of "fp"... something like file...???

    @Quzah: I changed this lines and I get the same solution. 10 words, even though there are just 9 words. Like the version of Adak it counts the "..." as one word. Now I/we have to find out how we can resolve this problem. So we have to take account of all special signs and maybe linebreaks: [ ][.][,][-][_][;][!][?]

    I hope you know, that I am very happy with this great approach!

    Yesterday I was working for a while on my own solution and my programm just counts the special signs in the line with the most special signs... crazy... okay. So this was rubbish. But now I try to understand your while-loops and to get it in my program and do the next steps. Maybe someone can explain me this line and you can be sure... the next question will be follow
    But I am amazed... the solution could be so easy/short! My code had some lines more

    Kind regards,
    Fresa


    EDIT: After testing: sure, it looks just after the blank space. And is this the meaning of the "0"??
    Last edited by Fresa; 02-29-2012 at 01:48 AM.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know what .... is, all about. Can you make up an actual input phrase that is *VERY SIMILAR* to the one you need? The details are important.

    Your solution was OK, but would take more work to get all the little bits and pieces of logic that it needs, working together.

    fp is just a common variable name abbreviation in C for "file pointer", a FILE * data type.

    What Quzah was commenting on would be like you went to the store and bought one loaf of bread. When you get home, he says you put ONE loaf of bread away in the cabinet. I say you put away the bread.

    Anyway, post up something with all the odd stuff that will be in your actual assignment, and let's see what logic is needed to handle it.

  3. #18
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Oh ok, I understand the thing with the "0" or "1". Thank you... ah ok, file pointer. It make sense


    Here is the txt-file and a picture how the solution can be.
    The number 590 discribes how many words you can find in the file (also if they are the same, in the first little example of me with the "olo"s the solution is 9)
    236 is the quantity of the DIFFERENT words. In my example it was always "olo", accordingly: 1. This should be list on the screen (I think I have just to compare the different strings in the next step). The right column shows the frequency of the words. I hope you can understand what I want.


    Questions to Structure - Beginner needs help!-unbenannt-jpg

    My actual input phrase is your solution transformed into German or my old solution, which is just rubbish .

    Now I want to attend to the other special signs and then to differ if there is a word, that is a duplex or not.


    -------------------------------------------------
    Code:
    #include <stdio.h>
    
    struct Liste {
        char Wort[26];
        int Anzahl;
    };
    
    int main(void) 
    {
        FILE * datei;
        int i;
        struct Liste Eintrag[600];
    
        if((datei=fopen("olo.txt","r+")) == NULL) 
        {
            printf("Fehler: Die Datei konnte nicht geoeffnet werden.\n");
        }
    
        i = 1;
        while((fscanf(datei, "%s", Eintrag[i].Wort)) > 0 ) 
        {
        printf("%2d. Wort: %s\n",i,Eintrag[i].Wort);
        i++;
        }
    
        printf("\nGesamtanzahl aller W\x94rter: %d",i-1);
    
        fclose(datei);
        printf("\n");
    
        fflush(stdin);
        getchar();
        return 0;
    }
    Attached Files Attached Files
    Last edited by Fresa; 02-29-2012 at 02:55 AM.

  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Sorry, it wsa the false text-file!!! Here is the correct one:

    Demodatei.txt

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand this sentence:
    "My actual input phrase is your solution transformed into German...". Can you re-word it?

    The Demodatei.txt file seems to be the assignment, or a list of assignments. Is this the input file? I used Google Translate to put it into English.

    I'd like to work in English, because my family hasn't spoken German in about 400 years, so I'm *really* rusty at it!

  6. #21
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    I'm not sure what you meant with an inpu phrase? I thought you want to see my code. Not correct?
    If you wanted to see my code I can explain in the next post

    Sure I can write it in English!! About 400 years? Where are you from?

    Hm... "assignment" has a lot of different possibilities to translate, but first here is the code in English:
    Code:
    #include <stdio.h>  
    struct list{     
    char word[26];
    int count; 
    };  
    
    int main(void)  {     
    FILE * fp;     
    int i;     
    struct list entry[600];      
    
    if((fp=fopen("Demodatei.txt","r+")) == NULL) {printf("ERROR: ...\n");}   
       
    i = 1;     
    while((fscanf(fp, "%s", entry[i].word)) > 0 )      
    {
    printf("%2d. word: %s\n",i,entry[i].word);     
    i++;
    }     
     
    printf("\ntotal count: %d",i-1);      
    
    fclose(fp);     
    printf("\n");      
    
    fflush(stdin);    
    getchar();     
    return 0; 
    }
    Last edited by Fresa; 02-29-2012 at 03:52 AM.

  7. #22
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Another part of the task is, that the User could define how many lines should be analyzed. So I tried to program the first steps for it. But there is the following problem: Anyway it analyzes the whole file, because the while-command from you does this in one step. So what should/could I change?
    Maybe I have to insert a line like this(?):
    Code:
    ret=fgets(Zeile,100,datei);
    Code:
    #include <stdio.h>
    
    struct list{
        char word[26];
        int count;
    };
    
    int main(void) 
    {
        FILE * fp;
        int i, CountOfLines, x=1;
        char line[101], *ret;
        struct Liste entry[600];
    
        if((fp=fopen("Demodatei.txt","r+")) == NULL) 
        {
            printf("ERROR: ...\n");
        }
        else
        {
            printf("\nHow many lines should be analyzed?\n");
            scanf("%d",&CountOfLines);
    
            if (CountOfLines== 0) printf("The whole file will be analyzed"); // this will be edited later...
            else
            {
                CountOfLines++;
                    i = 1;
                while(x<CountOfLines)
                {
                    while((fscanf(fp, "%s", entry[i].word)) > 0 ) 
                    {
                    printf("%2d. word: %s\n",i,entry[i].word);
                    i++;
                    x++;
                    }
                }
            }
    
            printf("\ntotal count: %d",i-1);
    
        fclose(fp);
        printf("\n");
    
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }
    Last edited by Fresa; 02-29-2012 at 04:25 AM.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Input phrase = a test phrase for input to the program. Like Edelweiss.txt was - but now something more focused on the same type of input your program should handle.

    My family has been kicking it in America for about 350 years. Before that, half of the family was in England, and half was in western Germany.

    Two items:
    1) fflush(stdin) doesn't work. You can't flush a kitchen faucet - you can only flush the bathroom toilet. fflush() works on outgoing streams, only.

    2) Please indent 2-5 spaces when you have subordinate lines of code, like a loop. Your eye will become trained to pick up lots of errors if you use this habit. It helps everyone studying your code, as well.
    Code:
    for(i=0;i<10;i++) {
       //some code here, but note it's indented by 3 spaces
    }
    
    //Instead of:
    for(i=0;i<10;i++) {
    //some code here, but note it's NOT indented at all
    }
    So, how do you want to count up the frequency of each word?

    The number of lines to be read and analyzed is a detail. Smart thing to do is to NOT WORK WITH UNNECESSARY details, at this time. Get the "core" of the program working first - add in the details later. Otherwise, you may have to work in major parts of the program, and fit them in around the details you already have in place - which is backward to what you want. Like packing a box - always put in the biggest items first, then fit in the smaller items, around the big items.

    A while loop we are using can easily be adjusted, for only a certain number of lines to read in.
    Last edited by Adak; 02-29-2012 at 04:33 AM.

  9. #24
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Your past is very interesting, but we should talk and discuss about C

    The test-file I hosted here in post #19. This is also the file from my teacher.
    It is common in our lessons, that we always add it to the end of the main. Does it annoy?


    Sorry, but I can't see any difference in your two for-loops.

    I try to sum it up:

    How can I or the user define how many lines should be analyzed.
    And there is also the problem with the special signs! It lists also the commas and punktuation (for example "Word 591: Kopie!") and if there would be this: " ... " (with two spaces) it will count it as a word, too.

    INFO: After lunch I answer to the whole post from/of(?) you!
    Last edited by Fresa; 02-29-2012 at 04:54 AM.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C it is.

    No, it doesn't annoy.

    The difference in the two for loops is not apparent to beginners. It will become VERY apparent, as your brain trains itself to recognize the common patterns in C code. It is a helpful tool to use.

    Don't worry about the number of lines to be analyzed - that's trivial. Concentrate on the BIG stuff - counting the word frequency in the text.

    Don't worry about weird words yet - that's another detail. Don't worry right now about other details unless they're a big deal, bigger than counting the frequency of the words. Put off the details, for now.

    Welcome to Top Down Design! XD

  11. #26
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Okay, so I concentrate on the frequency counting. But don't forget the other things. Later it also must be ordered by frequency count or alphabetically.

    Now I put off the lines with this line-counting or "numberofline"...
    I have the same code from post 21 again.

    So how can I get the frequency count... I have to compare the words to find duplexes.

    I want to use
    Code:
    (strcmp(list[i].word, list[i-1].word)) == 0
    , but it doesn't work... it is breaking off...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct alist {
        char word[26];
        int count;
    };
     
    int main(void) 
    {
        FILE * fp;
        int i, d, k;
        struct alist entry[600];
    
        if((fp=fopen("Demodatei.txt","r+")) == NULL) 
        {
            printf("Error! File did not open - closing program.\n");
            return 1;
        }
        else
        {
            d = 0;
            i = 1;
            while((fscanf(fp, "%s", entry[i].word)) > 0 ) 
                {
                    if(i>1)
                    {
                        k = i;
                        for(i=1;i++;i<k)
                        {
                            if((strcmp(entry[k].word, entry[i].word)) == 0) {d++;}
                        }
                    }
    
                    printf("count: %d   word: %s\n",i,entry[i].word);
                    i++;
                }
    
        fclose(fp);
        printf("\ntotal count: %d",i-1);
        printf("\ntest: total duplexes: %d",d);
        printf("\n");
    
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }
    Last edited by Fresa; 02-29-2012 at 06:49 AM.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I changed a few things.

    1) included ctype.h and string.h

    2) changed part of the while loop to this:

    Code:
       while((fscanf(fp, "%s", list[i].word)) > 0) {
          len = strlen(list[i].word);
          while(!isalpha(list[i].word[len]))    //trim the word of punctuation
             list[i].word[len--]= '\0';
    
          for(j=0;j<=i;j++) {
             if(!strcmp(list[j].word, list[i].word)) { //words match
               ++list[i].count;                        //add to the tally
             }
          }
    
          printf("%4d. %26s %4d\n",i+1,list[i].word, list[i].count);
          ++i; 
       }
    Edit: Just saw your please wait - a bit too late though.

  13. #28
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Edit: Just saw your please wait - a bit too late though.
    It's okay, but you should look at post 26 again. Thanks.

    Now I try to transpose yout ideas with mine. Line 6 to 10 are similar to my changes! *happy*

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you getting the full list of words, and the frequency count, OK?

    Don't worry about the words being repeated, for now - that's just another detail to be put off until later. Verify that the words are all in the list[], and that the frequency is being handled OK. That's the major thing.

    I'm using Edelweiss.txt for the input, so I can verify the above.

  15. #30
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Where can I find the Edelweiss.txt?

    No, it doesn't pu out the frequency count.
    Now, i changed my code and it shows mit the list to word 336 and then it puts out the following error-message
    "Debug Assertion Failed!
    Lin56,
    Expression: (unsigned)(c + 1) <= 256
    For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts..."

    And this is my current 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, d, k, len;
        struct alist list[600];
        
        if((fp=fopen("Demodatei.txt","r+")) == NULL)
        {
            printf("Error! File did not open - closing program.\n");
            return 1;
        }
        else
        {
            d = 0;
            i = 1;
            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(i=1;i<k;i++)
                        {
                            if((strcmp(list[k].word, list[i].word)) == 0) {d++;}
                        }
    
                    printf("count: %d   word: %s\n",i,list[i].word);
                    i++;
                }
    
        fclose(fp);
        printf("\ntotal count: %d",i-1);
        printf("\ntest: total duplexes: %d",d);
        printf("\n%s",list[1].word);
        printf("\n");
    
        }
    
        fflush(stdin);
        getchar();
        return 0;
    }
    I am so deeply deeply grateful, that you take so much time for me! I hope I could return the favor one day... :-/

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