EOF trouble

This is a discussion on EOF trouble within the C Programming forums, part of the General Programming Boards category; im working on an assignment for school, and i need to count the number of words, lines, and charcters that ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    EOF trouble

    im working on an assignment for school, and i need to count the number of words, lines, and charcters that the user inputs untel they enter a EOF, but my program wont work for some reason, and i cant tell whats wrong.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define NULL 0
    main()
    {
    	char buff[256];
    	int len, line_count=0, char_count=0, last_char_flag, index, word_count;
    	printf("Please Type some stuff, and hit ctrl+z to end\n");
    	do
    	{
    		if(gets(buff)==EOF)
    			break;
    			line_count++;
    			char_count=len;
    				len=strlen(buff);
    			
    	}
    	while(1);
    		
    	for(index=0; index<len; index++)
    	{
    		if(last_char_flag==0) 
    		word_count++;
    		if(buff[index]!=' ')
    			word_count++;
    		if(buff[index]==' ')
    			last_char_flag=0;
    		else
    			last_char_flag=1;
    	}
    	printf("there are %d charcters\n %d lines\n and %n words\n", len, line_count, word_count);
    }
    
    int strlen(char able[])
    {
    	int index=0;
    		while(able[index]!=NULL)
    			index++;
    		return(index);
    }

  2. #2
    Unregistered
    Guest

    Smile

    instaed of
    if(gets(buff)==EOF)
    break;

    use

    gets(buff);
    if(feof(stdin))
    break;


    hope that helps you.

    -ravi

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    now i get a "this program has proformed an illeagle operation" when i press ctrl+z

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    is the syntex for "#define NULL 0" correct?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    gets is evil. use fgets. evil buffer overflows.

    Code:
    /* this code is lifted from one of my programs
        it basically just gets a line from stdin */
    
    int getSentance(char *sentance, int maxlen)
    {
     char sentance2[maxlen];
     int i=0;
     fgets(sentance2,maxlen,stdin);
     while(sentance2[i] != '\n')
     {
      *sentance = sentance2[i];
      *sentance++;
      i++;
     }
     *sentance = '\0';
     return 0;
    }
    
    /* usage: */
    getSentance(charArray, 30);  /* where 30, put the length of the 
                                                       character array */

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Originally posted by Saiyanman
    is the syntex for "#define NULL 0" correct?
    NULL is already defined in stdio.h [or is it stdlib.h?]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    NULL is defined in stdio.h

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 03:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21