Output Problem, Very simple program

This is a discussion on Output Problem, Very simple program within the C Programming forums, part of the General Programming Boards category; I am new to programming in C and I am learning from "The C Programming Language", I am trying to ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Output Problem, Very simple program

    I am new to programming in C and I am learning from "The C Programming Language", I am trying to write a program that will count input lines words, and characters, I've checked multiple times and I'm fairly certain this is the exact code in the book, but i am not getting any output from it, any suggestions?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define IN 1
    #define OUT 0
    
     main()
    {
    int c, nl, nw, nc, state;
    
    state = OUT;
    
    nw = nc = nl = 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", nw, nl, nc);
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    line 16 is an error. it should be \n not n (checking for newlines).
    line 21 also. it should be = not == (assignment operator, not equality test operator).

    you should be getting some kind of output. what is your input?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Thank you for the help, but im still not getting anything, I have been typing "this is a test" and after hitting enter im not getting any sort of result

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you're entering data from the terminal then you need to tell windows it's the end of your input by entering a ctrl-z at the beginning of a line. But more normally, that code is meant to be run with file redirection:
    Code:
    count < testfile.txt

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    Quote Originally Posted by MrAlt View Post
    Thank you for the help, but im still not getting anything, I have been typing "this is a test" and after hitting enter im not getting any sort of result
    How are you entering EOF?

    I think Linux uses this
    CONTROL-D
    or
    I think Windows uses this
    CONTROL-Z

    Note: I always had to press enter after doing the EOF.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't all modern compilers warn when you don't specify what type a function returns? (it should be int main, not just main)

    On line 21, you have an extra = sign, and that might be causing some of the issue.

    Also, your if statements are messed up, what's the point of setting the state and then re-evaluating the state?

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    CTRL - Z was the answer, thank you all very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting wrong output from simple program
    By d387420489 in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 07:21 PM
  2. Replies: 3
    Last Post: 04-27-2011, 10:46 PM
  3. Replies: 5
    Last Post: 04-25-2011, 02:12 PM
  4. Seemingly simple array program yielding bizarre output
    By alter.ego in forum C Programming
    Replies: 7
    Last Post: 04-07-2011, 12:15 PM
  5. Weird output simple program.
    By omnificient in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2005, 01:53 PM

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