Like Tree3Likes
  • 1 Post By JohnGraham
  • 1 Post By std10093
  • 1 Post By std10093

cant understand my mistake

This is a discussion on cant understand my mistake within the C Programming forums, part of the General Programming Boards category; hello, this program is an exercise from ritchie and kernighan book..the question says- Write a program to count blanks, tabs, ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    cant understand my mistake

    hello,

    this program is an exercise from ritchie and kernighan book..the question says-

    Write a program to count blanks, tabs, and newlines.

    i made the following program but the output is coming zero always.

    https://www.dropbox.com/s/doky8ev9pdisxr0/h.c

    pls help i am new to programming..

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    First, welcome to the forum.

    Second, always post your code in code tags, like this: [code]/*your program*/[/code]
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    okay will do so next time onwards..did u check out the program??? pls help..

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Your problem is here:

    Code:
    while ( (c=getchar()) !=EOF);
    Do not put the semicolon after the "while()" - if you do so, it constitutes the statement you want to perform, which does nothing. I.e. what you have written is actually:

    Code:
    while ( (c=getchar()) !=EOF)
        /* do nothing */;
    
    /* c is now EOF */
    nilot27 likes this.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Οκ, since it's your first post I clicked on the link, but next time use the code tags

    You have
    Code:
    while ( (c=getchar()) !=EOF); /* A semicolon!!*/
    As I wrote in a comment, you have a semicolon there. What does this mean to a compiler?
    It means, that you have a while loop, with a body of doing nothing!
    Equivalently, you have this
    Code:
    while ( (c=getchar()) !=EOF)
    ;
    which can be written like this
    Code:
    while ( (c=getchar()) !=EOF)
    /*Do nothing*/;
    So, you press a key, getchar brings it to the variable c, you check if it is not EOF and then you do nothing and go on to the next input, while you give EOF. So, when you do give EOF, you will say goodbye to this logically error loop. Then you will meet the if statements. But you won't enter none of them!
    Why?
    Because c is now EOF.
    As a result, when you reach the printing part, you have the variables with their initial values.

    So, how to fix it? Remove this semicolon.. But... Will this really work? My guess is no! Why?
    Because you should use brackets (I really recommend using them, especially now that you are a beginner!!!!!!), or you should use if else instead of if.

    Let's see what I mean:
    ( I have removed the semicolon )
    Code:
    while ( (c=getchar()) !=EOF)
        if (c=='\n')
            ++new;
        if (c=='\t')
            ++tabs;
        if (c==' ')
            ++blanks;
    Which is the body of the loop? Since, when we do not place brackets only the very next line of code is believed to be the body, the body of while is the very next if statement, which in turn, because it has no brackets, has as its body the ++new.

    So, this code is equivalent to this code
    Code:
    while ( (c=getchar()) !=EOF)
    {
        if (c=='\n')
            ++new;
    }
        if (c=='\t')
            ++tabs;
        if (c==' ')
            ++blanks;
    which, as you can see, it's not what you want

    But, if you had if else statements, then it would be ok.
    What I say is this:
    Code:
    while ( (c=getchar()) !=EOF)
        if (c=='\n')
            ++new;
        else if (c=='\t')
            ++tabs;
        else if (c==' ')
            ++blanks;
    because the else is going together with the previous if.

    But, what you should do at this stage of yours is this
    Code:
    while ( (c=getchar()) !=EOF)
    {
        if (c=='\n')
            ++new;
        if (c=='\t')
            ++tabs;
        if (c==' ')
            ++blanks;
    }
    Also, a good idea would be to use brackets at the if statements too.

    Take home message:
    Use brackets in your first steps! It's not something clever you need to practice much. As you gain experience, you can easily start omitting brackets. Omitting brackets, doesn't make you a better programmer. Just producing code with less lines (which in turn produces cleaner code). But first you have to learn how to code, in order to produce correct code and then you can improve the readability of it

    Hope this helps
    nilot27 likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Quote Originally Posted by JohnGraham View Post
    Your problem is here:
    Partially true. Read my post, because there is another logical error too
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    thank you..thanks a lot..though the bracket part was a bit confusing ..i think the bracket is necessary to mark out the body of the while loop, hope i got the point..but thanks again for such a comprehensive post..

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Glad I helped. I believe that beginners should always use brackets ( yes, that may be "boring" some times ), but being pedantic saves you from logical errors
    nilot27 likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Ok, now you understood this, I think it's time for my question: Have you read the chapter about functions, or not yet? Of course, if not yet, don't hesitate to tell it
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    no i have not yet reached it..i am doing character input output now...will start word counting and arrays tomorrow...this exercise was a part of line counting...

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Ok, good studying then! And despite the fact that the code had logical errors, you did a good try. Well done!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is my mistake
    By Dave Couture in forum C Programming
    Replies: 8
    Last Post: 08-14-2012, 10:28 PM
  2. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  3. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 08:31 AM

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