#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Smile Problems getting an output.


    Hey! I was hoping you brainiacs could tell me about this issue I'm having with my dumb C program.

    I'm on the first chapter of "The C Programming Language" by Kernighan and Ritchie. They're hitting me with some input output code. NO BIG DEAL.

    I copied their example code line by line and whenever I compile and run it in Vstudio or minGW I get no output and only input.

    Here is the secret code:

    Code:
    #include <stdio.h>
    
    /* count the characters in input V1.0 */
    
    main()
    {
         long nc;
         
         nc = 0;
         while (getchar() != EOF)
              ++nc;
         printf("%ld\n", nc);
    }
    Running the .exe opens a command window and lets me type stuff. Pressing enter just lets me type on a new line. I never get to see how many characters my lines have. The printf line just doesn't seem to want to do anything. Maybe VS has a bad definition of EOF, so I'm stuck in the loop or something.

    I wouldn't ask for help, but I've been at this all day and my problem doesn't seem to show up in the faq. I really appreciate your guys' help.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    Add another getchar() after the print to keep the window open.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    The program compiles and runs perfectly under Linux. But I am guessing you aren't using linux. I don't know what VS is, so how are you inputting characters to the executable when you run it ?
  6. #4
  7. Who set my Title?
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Posts
    716
    Rep Power
    258
    You need to input End of File character. Since you are using visual studio, you can press ctrl+z when you are done entering the text.
    Nobody is perfect. I am Nobody.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Works fine under Visual Studio 2008. That even takes care of the standard problem of closing consoles when launching from the IDE.

    What exactly are you doing? Describe your actions keystroke-by-keystroke if need be.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    When a program running in its own window terminates, the OS closes the window. That is normal behaviour yet seems to astound and confuse people when it happens to their own code!

    In Visual studio you can either:
    1. Set a breakpoint on the closing brace or return statment of main()
    2. Use the "Run Without Debugging" menu selection (it then runs as a child process of a wrapper process that waits for user confirmation)


    Alternatively you can write your code so that it does not terminate without any user interaction, or if you run your code from a command shell prompt because it no longer runs in its own window, the window will not close.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by clifford
    When a program running in its own window terminates, the OS closes the window. That is normal behaviour yet seems to astound and confuse people when it happens to their own code!

    In Visual studio you can either:
    1. Set a breakpoint on the closing brace or return statment of main()
    2. Use the "Run Without Debugging" menu selection (it then runs as a child process of a wrapper process that waits for user confirmation)


    Alternatively you can write your code so that it does not terminate without any user interaction, or if you run your code from a command shell prompt because it no longer runs in its own window, the window will not close.
    Yes, that is indeed the usual problem. But not here. Here, it appears to be the EOD problem, Ctrl-Z in Windows/DOS and Ctrl-D in UNIX/Linux.

    As it stands, with Microsoft Visual Studio, the program runs with no problem whatsoever, and even will run with no problem whatsoever in regard with the usual you-gotta-keep-that-console-window-from-closing-upon-termination problem when clueless beginners fail to run a command-line app from the command line.

    When I was a US Air Force technician, we had a Technical Order that provided us codes to enter into the requisite forms to document our maintenance and corrective actions. One block was "How Mal", meaning "how did this equipment malfunction?" There was a standard letter we were to enter to indicate that this was due to normal maintenance. However, there was one HOWMAL code that we would have liked to have entered at times, but which we were not allowed to use: Y -- User Error.

    I personally ran the OP's code without any modification under Microsoft Visual Studio 2008. Without any error whatsoever. I would therefore submit that the HOWMAL code for this particular evolution (to mix my earlier USAF military experience with my later USN-RC experience) would be Yankee -- the OP clearly had no fracking clue how to execute the program in question.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Subhasgram,Kolkata,WB,IND
    Posts
    13
    Rep Power
    0
    Try this, it might be work...
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    /* count the characters in input V1.0 */
    
    main()
    {
         long nc;
         
         nc = 0;
         while (getchar() != EOF)
              ++nc;
         printf("%ld\n", nc);
    getch();
    }

IMN logo majestic logo threadwatch logo seochat tools logo