Thread: K&R Example not working here

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    [Solved]K&R Example not working here

    I've just copied this example from K&R from page 23 but it doesnt works properly:
    Code:
    #include <stdio.h>
    
    int main(){
    	int c, i, nwhite, nother;
    
    	int ndigit[10];
    
    	nwhite = nother = 0;
    	
    	for (i = 0; i < 10; ++i)
    		ndigit[i] = 0;
    
    	while ((c = getchar()) != EOF)) //I had a missing ) here;
    		if (c >= '0' && c <= '9')
    			++ndigit[c-'0'];
    		else if (c == ' ' || c== '\n' || c == '\t' )
    			++nwhite;
    		else 
    			nother++;
    	printf("digits =");
    		for (i = 0; i < 10; ++i)
    			printf("%d", ndigit[i]);
    		printf(", white space %d, other = %d\n", nwhite, nother);
    	
    	return 0;
    
    
    }
    Ive replaced EOF for 'q' because I dont know how to "type" EOF, also ive noticed that spaces and other counters seem wrong. Im testing it with linux Gentoo, Ive compiled with gcc -Wall but no messages of warning were shown. Any ideas? Thanks in advance guys.
    Last edited by Maragato; 04-29-2005 at 10:36 PM.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    What do you mean when you say it doesn't work?

    This line
    Code:
    while ((c = getchar() != EOF))
    is going to cause the program to run until it recieves an EOF, so it won't terminate if you just give it text. Is that the problem you are having?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by trippeer
    What do you mean when you say it doesn't work?

    This line
    Code:
    while ((c = getchar() != EOF))
    is going to cause the program to run until it recieves an EOF, so it won't terminate if you just give it text. Is that the problem you are having?
    Sure but now I know eof is crtl + d

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    I thought EOF was ctrl+z

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Just compare it with '\n' instead of EOF. That's what I did when I encountered the same problem back in the day.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by trippeer
    What do you mean when you say it doesn't work?

    This line
    Code:
    while ((c = getchar() != EOF))
    is going to cause the program to run until it recieves an EOF, so it won't terminate if you just give it text. Is that the problem you are having?
    It's not copied correctly. It should be:
    Code:
    while ((c = getchar()) != EOF )
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Just compare it with '\n' instead of EOF.
    Except you're always sure to get an EOF at some point
    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.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by sand_man
    I thought EOF was ctrl+z
    Microsoft OSes use ctrl+z while *NIX OSes use ctrl+d.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM