Thread: simple word count program not quite working out (noob question)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    simple word count program not quite working out (noob question)

    I have a very simple program that I am not sure why it works. It should just count the number of lines, words, and characters that are typed. It counts the lines and characters properly but not the words.
    Code:
    #include <stdio.h>
    
    #define IN 1 	//inside a word
    #define OUT 0	//outisde a 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);
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    state==OUT;
    You want this to be a single = for assignment; as it is you're doing a comparison. It's possible that your compiler has a setting to warn you on something like this (gcc warns by default); check its documentation.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    == is a relational operator. = is an assignment operator. Getting them mixed up is a classic beginner's mistake, even for elite programming legends like myself. Want to see my first post ever?

    Random Numbers
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    duh, ok thank you. I knew it was something dumb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc compile errors, HELP!
    By FalconGK81 in forum C Programming
    Replies: 11
    Last Post: 02-14-2011, 04:17 AM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  3. noob help on simple program
    By halfpint101 in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2007, 07:29 PM
  4. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM