Thread: Words Counter

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Post Words Counter


    Hi there.

    I'm noob programming c and I'm studying with the Programming Language C book by Kernighan and Ritchie.

    There is an example to count words and the code is the next:

    #include <stdio.h>
    #include <stdlib.h>

    #define IN 1 // In one word
    #define OUT 0 //out of word

    main()
    {

    int c, nl, nw, nc, state;

    state = OUT;

    nl = nw = nc = 0;
    while((c = getchar())!= EOF){
    ++nc;
    if (c == '\n')
    ++nl;
    if (c == ' ' || c == '\n' || c == '\t')
    state = OUT;
    else if (state == OUT){
    state = IN;
    ++nw;
    }
    }
    printf("%d %d %d\n", nl,nw,nc);

    }

    Anyway, my problem is that there are not mistakes in the code, but when I compile the program (in code blocks, by the way), I cannot watch the output, I mean, I cannot get the count of the variables.

    I hope can you help me.

    Thank very much.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Read the sticky thread, Commonly Asked C/C++ Questions.

    In Message #2 refer to 0.3 I cannot see my C compiler output. The window is closing too quickly for me.

    Also, use code tags to preserve your code's indentation.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define IN 1 // In one word
    #define OUT 0 //out of word
    
     main()
    {
    
       int c, nl, nw, nc, state;
    
       state = OUT;
    
       nl = nw =  nc = 0;
       while((c = getchar())!= EOF){
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT){
            state = IN;
             ++nw;
        }
       }
        printf("%d %d %d\n", nl,nw,nc);
    
    }
    I retrieved your original indentation via the Reply button. You can do the same to his message to view code tags.

    You should have gotten something like the following warnings:
    C:\otros\dcw>gcc -Wall newb.c
    newb.c:8: warning: return-type defaults to `int'
    newb.c: In function `main':
    newb.c:29: warning: control reaches end of non-void function

    C:\otros\dcw>
    The problem is that K&R still use some old practices that newer compilers will not tolerate. Here is how the skeleton of the main function should look (changes in red):
    Code:
    int main()
    {
    
    
        return 0;
    }
    And some very good advice for you: do not ignore warnings. Warnings are more important than error messages. Never ignore warnings.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    Hi dwise1_aol.

    Thank very much by your advice and answer.

    I have added the tags as you indicated me, but the program continues without show me the output.

    I have started to analyze the code, to check if there is something that misses in the program. Just it executes and allows me to write in the cmd prompt but when I type enter the program gets stuck.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Did you even bother to read Commonly Asked C/C++ Questions? I pointed you directly to the answer.

    The program runs in a shell (AKA "cmd prompt" window). If you already have a shell open and you execute it there, then it uses that shell. If you launch it from a GUI, then the OS opens a shell for the program to run in and as soon as the program terminates normally it closes the shell immediately. This requires that you either design your program to not close immediately or else use a kludge to postpone program termination.

    Follow the link. Then follow the link in 03.

    PS

    On my drive in to work, I realized that I may have misunderstood what your problem is. I will get back to you in a short while.
    Last edited by dwise1_aol; August 16th, 2013 at 11:07 AM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    OK, if I may make a suggestion. Since you are learning from a book that a great many programmers have, it might help us if you were to identify which problem or which example program from the book you are working from. Problem number and page number would be enough. It's just that there might be something in the problem statement or in the preceding pages that would help us.

    Also, since the first edition of the book was effectively the only documentation available on C, it became extremely well-known and both the book, that version of C, the book's style of formatting and indenting the code, and the authors themselves, Kernighan and Ritchie, came to be referred to as "K&R". So whenever you see us refer to K&R, such as I do in this message, you will know what we mean.


    This could be an operator error situation. K&R were both involved with the design of the UNIX operating system; C and UNIX were pretty much co-designed together. Using this program requires that you know how standard C library input functions work and especially how to enter an end-of-file (EOF) from the keyboard.

    C and UNIX were designed around 1970, a decade before personal computers. Most main-frame computers and minicomputers had users logging in through terminals of varying sophistication. The most basic terminals would send a character each time you pressed a key, including the backspace character (^H). The computer would store these characters into a buffer until it got the signal to give it to the program. That signal was the Enter key.

    As a result, the standard library input functions will not start to process your keyboard input until the user has pressed the Enter key. There are ways to get around this, but they all depend on the operating system. The point here is that your program will not start to process the input until you've hit the Enter key.

    And there's also the EOF. The program is designed to allow the user to input any amount of text over any number of lines, from which the program will count the white space characters (newline, tab, space). The user will indicate that he is done entering that text by entering the end-of-file (EOF) character, which is also known in UNIX as end-of-data (EOD).

    That means that you need to know how to enter that character, which I assume is your problem. It is possible that K&R explained that in the book, but not knowing where to look I cannot check. Even if they did, that wouldn't help you if you're running on a Windows machine, because it's a different input.

    On UNIX and Linux: mark end-of-data with Ctrl-D ("control D"), which should display the Ctrl-D sequence, ^D (look at my avatar, which references "the poor man's editor"). Then press Enter and the program should display the statistics and exit.

    On Windows/DOS: end-of-file is ^Z, so press Ctrl-Z and then press Enter. This I did test.

    If that's still not the problem, then please explain your problem by describing how you start the program, what exactly you input, and what exactly you see happening.

    BTW, I ran it from the command line on a Win XP box and it worked just fine.

    PS
    "The poor man's editor".

    It's a command-line trick to create a text file without using an editor. In the early days of MS-DOS and UNIX, where were some line-oriented editors that came with the system, but to get a functional screen editor like we are used to now you'd have to buy one. So if you couldn't afford to buy an editor, you could use this trick instead.

    To create a file, test.txt, in MS-DOS, you would enter this command:
    COPY CON test.txt
    CON is the console device with input from the keyboard and output to the monitor. CON input is known in C as stdin, the standard input file. COPY is the command to copy the first file to the second file. So what this command does is that it copies input from the keyboard to the file, test.txt. Which means that whatever you type will be copied to test.txt until you enter the EOF character, ^Z.

    To create that file in Linux, you use the cat command, which is similar to the TYPE command in MS-DOS, it displays what's in that file. It comes from concatenate, since it can be used to concatenate files together. I believe (since I don't work with Linux much) that you would enter this command:
    cat >test.txt
    Since you do not give cat a file, it assumes stdin, the keyboard. And you tell it to redirect the output to the target file, test.txt, so it won't output to the screen but rather to that file. Then when you're done, ^D marks the end-of-data.

    And that explains my avatar, which I found once on a website.
    The "Editors are for wimps" is because you have to type everything in correctly; you can't go back and change the text file except with an actual editor. I have played with using the backspace key, but that only inserts the backspaces into the file; it will display as corrected, but a hex dump of the file shows what's really happening.
    Last edited by dwise1_aol; August 16th, 2013 at 11:51 AM.

IMN logo majestic logo threadwatch logo seochat tools logo