C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2012, 06:15 PM
ungraylessness ungraylessness is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 1 ungraylessness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 42 sec
Reputation 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.

Reply With Quote
  #2  
Old August 11th, 2012, 06:19 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,475 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 11 m 38 sec
Reputation Power: 2194
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!

Reply With Quote
  #3  
Old August 11th, 2012, 09:00 PM
EEmaestro EEmaestro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 58 EEmaestro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ?

Reply With Quote
  #4  
Old August 11th, 2012, 09:24 PM
LaughingBelly's Avatar
LaughingBelly LaughingBelly is offline
Who set my Title?
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Posts: 716 LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level)LaughingBelly User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Weeks 6 h 21 m 7 sec
Reputation Power: 256
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.

Reply With Quote
  #5  
Old August 12th, 2012, 12:58 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
  #6  
Old August 12th, 2012, 01:58 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 51 m 23 sec
Reputation Power: 1774
__________________
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

Reply With Quote
  #7  
Old August 12th, 2012, 03:31 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,804 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 16 h 42 m 47 sec
Reputation Power: 1800
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.

Reply With Quote
  #8  
Old August 12th, 2012, 05:56 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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:
  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.

Reply With Quote
  #9  
Old August 25th, 2012, 10:45 AM
sandi.guha sandi.guha is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Subhasgram,Kolkata,WB,IND
Posts: 13 sandi.guha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 4 sec
Reputation Power: 0
Send a message via Google Talk to sandi.guha
Facebook
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();
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problems getting an output.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap