Thread: return function

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    return function

    Hi how would i return the value of word in the count words function to words in the main function im returning the wrong value. Am i returning it correctly

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    int countWords(FILE *fp, char filech);
    int main(void)
    {
    	
    	FILE *fp;
    	int i;
    	char cnt;
    	char ch;
    	int words=0, line=0, characters=0;
    	
    	/*open in binary*/
    	if((fp = fopen("count.txt", "r")) ==NULL)
    	{
    	   printf("cannot open\n");
    	   exit(1);	
    	}
    	while( (ch = fgetc(fp)) != EOF)
    	{
    		/*counts no characters in file*/
            cnt++;
            
            /*counts the no of lines in file*/
            if(ch == '\n')
            {
            line = line +1;
            
            }
            characters = characters +1;
             
           /*counts the number of words in a file*/
           /*
           if(ch == '\n' || ch == ' ' || ch == '.')
           {
          	 
          	 words = words +1;
           }
           */
           words = countWords(fp, ch);
            
    	}
       printf("no chars is:%d\n",cnt);
       
       printf("No of Lines %d\n", line);
    	printf("no of words are%d\n", words); 
    	   return 0;
    		
    	}
    	
    int countWords(FILE *fp, char filech)
    {
    	char word;
    	if(filech == '\n' || filech== ' ' || filech == '.')
        {
          	  word = word +1;
          	 
        }
    	return (word);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    in countWords you have a local var word (not initialized)
    you increment it - getting new garbage value and returning to the main...

    1. If you want to return int - declare it as int
    2. initialize it
    3. add in someway to the previous value stored in the var declared in main (or do addition in main - or pass the previous value as a parameter)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM