Thread: Problem's here!! Please help!!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    Unhappy Problem's here!! Please help!!

    I am working on a program that can count the capital letters of the text file inputted.
    However, sometimes it works and sometimes it doesn't. So i want to ask wt is the problem with my program.

    Code:
     
       void capital(FILE*fp)   
    {    
       char ch;    
       int count=0;          
       while(ch!=EOF)   
    {    
        ch=fgetc(fp);    
        if(ch>='A' && ch<='Z')    
        count++;   
    }     
        printf("\nThe total number of capital letters is %d.\n",count);    
        printf("\n");    
        fclose(fp);   
    }
    Please help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first problem is with your code indentation. I suggest writing it as:
    Code:
    void capital(FILE *fp)
    {
        char ch;
        int count = 0;
        while (ch != EOF)
        {
            ch = fgetc(fp);
            if (ch >= 'A' && ch <= 'Z')
                count++;
        }
        printf("\nThe total number of capital letters is &#37;d.\n", count);
        printf("\n");
        fclose(fp);
    }
    Now, since you have not given any sample data as to when it does not work, I can only guess: perhaps this is due to the fact that fgetc() returns an int, but you are storing its return value in a char. Along this line, fgetc() returns EOF when an attempt is made to read at the end of file, so on the last iteration you would well be comparing with EOF.

    A far less likely reason is that the comparison ch >= 'A' && ch <= 'Z' does not work in determining uppercase characters, and theoretically it is not guaranteed to work, so you should use isupper() from <ctype.h> instead.

    Consequently, you could try changing to:
    Code:
        int ch;
        int count = 0;
        while ((ch = fgetc(fp)) != EOF)
        {
            if (isupper(ch))
                count++;
        }
    One last note: it may be a better idea to let the caller close the file. After all, the caller opened the file, so the caller would expect to close it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Code:
        char ch;
        int count = 0;
        while (ch != EOF)
    You are reading ch without initializing. This could also be one of the reasons for your code not working.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    EOF also doesn't fit in a char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM