Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-21-2003, 04:51 PM   #1 (permalink)
DemosthenesB
Registered User
 
Join Date: Mar 2003
Posts: 7
DemosthenesB is on a distinguished road
Character Counting Program

I just started to learn C with the book "The C Programming Language." I am only in the first chapter and am already experiencing problems

There is an section on Character Input and Output. The first example is a "Character Counting" program. The script is a s follows:

Code:
#include <stdio.h>

/*count chars in input, 1st version*/

main ()
{
	long nc;

	nc = 0;
	while (getchar() != EOF)
		++nc;
	printf("%ld\n", nc);
}
What am I supposed to get when I run the program? So far all I can do it type and the text will show, nothing else.

P.S. Just to let you know I am using Slackware Linux, maybe that has something to do with it...
DemosthenesB is offline   Reply With Quote
Old 03-21-2003, 05:16 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i think the program is executing and exiting so quick that you can't see the output .. try this:
Code:
#include <stdio.h>

/*count chars in input, 1st version*/

main ()
{
	long nc;

	nc = 0;
	while (getchar() != EOF)
		++nc;
	printf("%ld\n", nc);

	printf("\n\n Press any key to exit.");
	
	getchar(); 
}
i'm sorta a n00b myself, but i think this will do it.
sde is offline   Reply With Quote
Old 03-21-2003, 06:15 PM   #3 (permalink)
DemosthenesB
Registered User
 
Join Date: Mar 2003
Posts: 7
DemosthenesB is on a distinguished road
Thank you for your help sde, but it still does not work, or at least doesn't output something. Maybe you could explain what it actually does(what the output is supposed to look like), or if it is even suposed to have an output.

Thank you very much.
DemosthenesB is offline   Reply With Quote
Old 03-21-2003, 09:00 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
the program does exactly what is intended, and upon you ending input (EOF) it will display the number of characters you pressed..

EOF is probably crtl-Z, so type your characters then press crtl-z and it will display no. of chars and exit.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-22-2003, 06:04 AM   #5 (permalink)
JJoSA
Registered User
 
Join Date: Mar 2003
Location: Between a rock and a hard place
Posts: 7
JJoSA is on a distinguished road
Yup, it's waiting for the EOF character from your input. If you're running on a Unix box, it's CTRL-D
JJoSA is offline   Reply With Quote
Old 03-23-2003, 02:49 AM   #6 (permalink)
peted
Recruit
 
peted's Avatar
 
Join Date: Mar 2003
Location: Norfolk, UK
Posts: 2
peted is on a distinguished road
yep, JJoSA has it. The program you are compiling reads each character you enter from the keyboard in a loop until it reaches a special character called an End Of File (EOF) character. Your standard keyboard doesn't include this special character so you have to send it using a control sequence. Under linux the control sequence for an EOF is to hold the control key and press 'd' (Commonly presented as Ctrl-D).

So, run the program, type your message that you want character counted and when you've finished press Ctrl-D.

The control sequence that abc123 stated (Ctrl-Z) suspends a process (puts it in the background). To explain this i'd have to go into kernel process schedulers and i'll be here for hours. The best book in my opinion to describe kernel processes is Operating Systems by Andrew Tanenbaum.

Hope this helps.
peted is offline   Reply With Quote
Old 03-23-2003, 10:26 AM   #7 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
Originally posted by peted
The control sequence that abc123 stated (Ctrl-Z) suspends a process (puts it in the background).
you're both right. in dos/windows ctrl-z is the EOF combo.
in unix, like you said, it is ctrl-d, and ctrl-z suspends a process.
joe_bruin is offline   Reply With Quote
Old 03-23-2003, 03:13 PM   #8 (permalink)
DemosthenesB
Registered User
 
Join Date: Mar 2003
Posts: 7
DemosthenesB is on a distinguished road
YAY!~ Thank you guys SO much . I tried it with ctl+D and it works, thanks again.
DemosthenesB is offline   Reply With Quote
Old 04-03-2003, 05:10 PM   #9 (permalink)
TheDreamless
Registered User
 
TheDreamless's Avatar
 
Join Date: Apr 2003
Posts: 10
TheDreamless is on a distinguished road
Send a message via AIM to TheDreamless
Something we did in my intro C class that helped me to understand the way input from files and out to files worked was take a text file in unix and count all the lines, characters (dont include spaces tabs or end lines), and remove the vowels and then print them into an output file. Once you can do this you will pretty much have the hang of these types of programs. I know that your example code was reading from stdin but it seems the book is working towards redirected in and out since it used EOF.
TheDreamless is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM
Need help on program B00tleg Standard C, C++ 1 10-12-2004 12:02 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 04:01 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting