Thread: Questions to Structure - Beginner needs help!

  1. #76
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    I tried your solution, but in the debug-mode I got this:
    Questions to Structure - Beginner needs help!-unbenannt-jpg

    In the release-modus it just shows instead of the "empty" words this 3 points.
    It is the same solution I had with this loop, which is green:

    Sorry! The right code in English will follow!
    Last edited by Fresa; 03-05-2012 at 09:45 AM.

  2. #77
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Handling hyphens, I can help you with, but debug mode, German code, and umlauts - no way.

    If you have code to handle the ... as a word already, good deal.

  3. #78
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Sorry again. Here is it in English:

    Code:
    // if(list[i-1].word[i]!='0'){
    while((list[i].word[len]==' ') || (list[i].word[len]=='.') || (list[i].word[len]=='!') || (list[i].word[len]=='?') || (list[i].word[len]==',') || (list[i].word[len]==';') || (list[i].word[len]=='-') || (list[i].word[len]=='_') || (list[i].word[len]==':') || (list[i].word[len]=='\0') || (list[i].word[len]=='\n') || (list[i].word[len]=='\x0A') || (list[i].word[len]=='\x0D'))
                        {
                            list[i].word[len--]='\0';
                        }
    // }

  4. #79
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Fresa View Post
    Sorry again. Here is it in English:
    If it works, that's all that matters. There are always more than one way to do any specific thing, in programming.

    If ... is a word - and you want all the words to be shown, don't you want the ... to be shown?

    It will look odd if you want to number the ... as a word, but then don't show it in the word list, won't it? It will look like the program skipped a number, in the word count.

    For example, with the ... as the third word, Edelweiss.txt listing would become:

    1. Edelweiss
    2. every
    ( not shown: 3. ... )
    4. morning
    5. you

  5. #80
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    New info: my teacher told me, that it has to work in the debug-mode. Yesterday my program works, but now it works JUST in the release-mode. I can't find the mistake. But I will restart at the last step, where it runs.

    The three points must not been accepted as a word! So they sholdn't been shown. What did you change? Just this thing?:
    Code:
    while(!isalpha(list[i].word[len]) && list[i].word[len-2] != '.') {          list[i].word[len--]= '\0';          ++j;       }
    Okay, I want to try it again, when my program runs again!
    Do you have a solution, that "two,words" is shown as two words and not list as one word? Try this in a text-file:

    two,words and-three-words

  6. #81
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "two,words", is not a word. And it is not two words. It's just letters, a comma, and more letters, forming junk. Words are ended in English with either:

    1) a space
    2) a period, followed by a space or an end of the text (an EOF in our case).
    3) a punctuation mark and a newline
    4) a newline.

    You can delimit junk like "two,words", if you like, but it's not English. That gets troublesome though with the logic we have already.

    What;else:will,we%need&^to@accept!As=English?

  7. #82
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Yes of course. Normally there is a space behind a comma. But I have to attend to typing errors. So if there is something like this (two,words) in the text it has to been shown as
    two 1
    words 1

    and not as

    two,words 1

    The same for and-three-words.

    I tried your proposition with "&& list[i].word[len-2] != '.'" again and I got this:
    ... 1
    a 1
    abcde 1

    (and so on)

    Without your addition it looks like this:
    (here is a gap) 1
    a 1
    abcd 1

    (and so on)

    And now, there are just the points shown, but they must not been in the list!! This applys for all following special sings: . , - : ; _ ! ? (and blank space).

    Are you shure, that you made blank spaces around this three points? Why doesn't work it with my program? Phew...!!

  8. #83
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Perhaps it depends on "fscanf"? I would try another method, like this:
    Code:
    while((ret=fgets(buffer, 100, fp)) != NULL);
    But it doesn't work. Probably I have to change the following loops, but I haven't any idea. Maybe you can give me a hint? But at this time I haven't spend much time on it... maybe...

    my first try (that doesn't work):
    Code:
    i=j=0;
            while((ret=fgets(buffer, 100, fp)) != NULL);
            {
                strcpy(list[i].word, buffer);
                len = strlen(buffer);
                j++;
    
                while(((list[i].word[len]==' ') || (list[i].word[len]=='.') || (list[i].word[len]=='!') || (list[i].word[len]=='?') || (list[i].word[len]==',') || (list[i].word[len]==';') || (list[i].word[len]=='-') || (list[i].word[len]=='_') || (list[i].word[len]==':') || (list[i].word[len]=='\0') || (list[i].word[len]=='\n') || (list[i].word[len]=='\x0A') || (list[i].word[len]=='\x0D')) && (list[i].word[len-2] != '.'))
                {
                    list[i].word[len--]='\0';
                }
    
            }
                
            for(i=0;i<=j;i++) printf("%s",list[i].word);
    Last edited by Fresa; 03-06-2012 at 04:56 AM.

  9. #84
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Another try (the program reads one line and then it goes through the line: sign by sign). But it prints out just rubbish:
    Code:
    #include <stdio.h>
    #include <string.h>
      
    struct alist{
        char word[26];
        int count;
    };
    
    int main(void)
    {
        FILE *fp;
        int i=1, j=1, p=1, w=1, len, total;
        char puffer[100], *ret, c='\0';
        struct alist list[600];
    
        if((fp=fopen("test.txt","r+")) == NULL)
        {
            printf("ERROR\n");
        }
        else
        {
            while((ret=fgets(buffer, 100, fp)) != NULL)
            {
                while((c=fgetc(fp)) != EOF)
                {
                    c=list[i].word[w];
                    w++;
    
                    if(c=' ') i++;
                }
            }
                
            for(i=0;i<=w;i++) printf("%s",list[i].word);
    
            fclose(fp);
        }
      
        fflush(stdin);
        getchar();
        return 0;
    }

  10. #85
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Fresa
    (the program reads one line and then it goes through the line: sign by sign)
    This does not do that:
    Code:
    while((c=fgetc(fp)) != EOF)
    To go through the line, you should be accessing the contents of buffer instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #86
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    About the extended, German characters - ü, ä, ß, etc... - You need to call setlocale(LC_ALL, "") at the beginning of you program if you want character classification, collation, and casing to work according the user's locale.

    If you don't call that, then the only valid characters are a-z, A-Z, 0-9, etc... That is why isctype.c is is asserting in debug mode.

    By calling setlocale(LC_ALL, ""), you are saying (among other things): "I want to use the character encoding of the user's locale/language". If you are on a German OS, and the file is encoded using the German ANSI codepage, then you're good. For me however, my system's locale is "English_United States.1252" (Win7 + VC2010). So running your code on a non-German system, but with a German file, may still not work.

    Here's how you can determine what the user's locale string is:
    Code:
    #include <stdio.h>
    #include <locale.h>
    #include <errno.h>
    
    int main()
    {
        const char *loc = setlocale(LC_ALL, "");
        if (loc)
            printf("%s\n", loc);
        else
            printf("setlocale() failed, %d\n", errno);
    
        return 0;
    }
    Has your teacher discussed this at all? If not, I find it very strange that you've been given a task to process German text.

    In the meantime, just add that code to the top of your main(). Then when posting program output, we will all know what locale is being used to process the German text. For any of us running this code on non-German OS's, you will need to use an appropriate locale string to process the German text. Calling setlocale(LC_ALL, "german") with the MS CRT should be sufficient for character classification/collation.

    gg

  12. #87
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    Thank's for your answers!

    @Codeplug: I'm from Germany, so my teacher gave me a German text. Didn't you notice that in cause of my grammar and expressions?

    But I found another way to resolve the umlauts-problem (this was tooo easy):
    Code:
    if(c=='Ä') c=142;
                if(c=='ä') c=132;
                if(c=='Ö') c=153;
                if(c=='ö') c=148;
                if(c=='Ü') c=154;
                if(c=='ü') c=129;
                if(c=='ß') c=225;

  13. #88
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> @Codeplug: I'm from Germany, so my teacher gave me a German text. Didn't you notice that in cause of my grammar and expressions?
    I noticed. The fact that you were given German text to process just tells me that your teacher is as lost as you are on the subject of locales - or perhaps the teacher has lectured on locales, but you missed class those days.

    >> But I found another way ...
    What you are doing in post #87 is as-wrong and incorrect-as trying to process, categorize, and collate German text using the C locale.

    There is no need to look for another way - the correct way was given in post #86:
    >> just add that code to the top of your main()

    gg

  14. #89
    Registered User
    Join Date
    Feb 2012
    Posts
    47
    I don't understand the problem with the German text. What do you mean with "subject of locaes"?

    I changed my program completely. Now it reads the document sign by sign.

    Thank you all for helping me! You helped me a lot to learn and understand c-programming better. If I get more questions in the next parts of my task I will write again. Sorry, that I wasn't online regulary last week!

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