The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Problems getting an output.
Discuss Problems getting an output. in the C Programming forum on Dev Shed. Problems getting an output. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 11th, 2012, 06:15 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 1
Time spent in forums: 9 m 42 sec
Reputation Power: 0
|
|
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.
|

August 11th, 2012, 06:19 PM
|
|
|
|
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!
|

August 11th, 2012, 09:00 PM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 58
Time spent in forums: 1 Day 17 h 52 m 2 sec
Reputation Power: 2
|
|
|
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 ?
|

August 11th, 2012, 09:24 PM
|
 |
Who set my Title?
|
|
|
|
|
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.
|

August 12th, 2012, 12:58 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

August 12th, 2012, 01:58 AM
|
 |
Contributed User
|
|
|
|
|

August 12th, 2012, 03:31 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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: - Set a breakpoint on the closing brace or return statment of main()
- 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.
|

August 12th, 2012, 05:56 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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: - Set a breakpoint on the closing brace or return statment of main()
- 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.
|

August 25th, 2012, 10:45 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Subhasgram,Kolkata,WB,IND
Posts: 13
Time spent in forums: 4 h 20 m 4 sec
Reputation 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();
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|