Thread: Need help with displaying the output

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

    Question Need help with displaying the output

    Code:
    #include <stdio.h>
    
    main()
    {
        int c, n1;
    
        n1 = 0;
        while ((c = getchar()) != EOF)
            if (c == '\n')
                ++n1;
        printf("%d\n", n1);
    }
    I have a more complicated program I'm wishing to have display the output, however, to save some time I'm using an example of a shorter version. Please help count the lines in input and display the output in terminal when ./program is executed after compilation.
    To help count and compute lines, words and within arrays.
    Thanks,
    yoda

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    It's not showing the output on Fedora 18's terminal

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like it should work (though you should declare main as returning an int). Did you trigger EOF at the terminal?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Wow..
    I got everything to run from the code I didn't paste here. Long day.. I never triggered EOF..
    Also, sorry for the sloppy code haha I went back and declared main. Thanks, laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help displaying output from function
    By c++prog in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2008, 10:01 PM
  2. Help with displaying output
    By tallguy in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2007, 06:38 PM
  3. Output Displaying Incorrectly
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 11-21-2003, 10:59 AM
  4. displaying binary output
    By clancyPC in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 07:32 AM
  5. Trouble displaying output
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2003, 08:39 PM