Thread: Blank, newline, tabs counting wierd error

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Blank, newline, tabs counting wierd error

    Hi

    This is a very wierd problem. It's a program for an exercise in The C Programming Language 2nd Edition (K&R), but using CTRL + D doesn't work for this problem. I've this workable version, compiled it and it worked fine, this program is 99% of that program with one exception: the variable "nn" for "new blank" is named as "nb" (makes more sense). Aside from that, there's no major difference between these two files and I've no idea why this is not working.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
            int c, nn, nt, nl;
            nn = nt = nl = 0;
            while ((c == getchar()) != EOF) {
                if (c == ' ')
                    ++nn;
                else if (c == '\t')
                    ++nt;
                else if (c == '\n')
                    ++nl;
    }
            printf("%d %d %d\n", nn, nt, nl);
            return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You have a double = sign in (c == getchar()); you just want one.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by cas View Post
    You have a double = sign in (c == getchar()); you just want one.
    I noticed that in the code a few minutes ago. I'm sorry for that, but I've actually checked line by line several times but missed that =.

    Thanks for helping

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM