Thread: Problem with loop stopping at wrong time

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Problem with loop stopping at wrong time

    Hello again.
    I have created the following program to count the number of times each letter of the alphabet appears in a textfile. It works properly, unless there are no occerences of a certain character. Instead of just returning the value 0, it stops and any other letter in the alphabet after it that is in the file dosnt get counted\printed and I can't figure out why. Any ideas??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char inputArray[1024], inFile[128];
    FILE *in;
    int charCount, countArray[26] = {0}, aRep, *ptr;
    float lettotal, percentage;
    
    void main()
    {
    
    	printf("\nInput name of plaintext file, including full path:\n");
    	gets(inFile);
    
    	if ((in = fopen(inFile, "r")) == NULL)
    	{
    		printf("\nCould not open file to be converted\n");
    		exit(8);
    	}
    
    	while (aRep != EOF)
    	{
    		aRep = fgetc(in);
    		
    		if (aRep >= 97 && aRep <= 122)
    			{
    			aRep = aRep - 32;
    			}
    		
    		if (aRep >= 65 && aRep <= 90)
    		{
    		inputArray[charCount] = aRep;
    		charCount++;
    		}
    	}
    
    	inputArray[charCount] = '\0';
    	charCount = 0;
    	
    	for (charCount = 0; charCount != '\0'; charCount++)
    	{
    		aRep = inputArray[charCount] - 65;
    		countArray[aRep] = countArray[aRep] + 1;
    	}
    	
    	countArray[charCount] = '\0';
    	charCount = 0;
    	printf("Number of each letter in file:\n");
    
    	for (charCount = 0; countArray[charCount] != '\0';  charCount++)
    	{
    		aRep = charCount + 65;
    		printf("%c:%d\t", aRep, countArray[charCount]);
    	}
    }
    Thanks
    -Chris

    P.S. I know there are a couple of unused variables and a pointer, they are for somthing else it needs to do later once this part works =)
    Last edited by Baron; 12-12-2005 at 09:08 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It stops because you tell it to stop. They're all counted. However, you should be doing something like:
    Code:
    for( charCount = 0; charcount < lettersinthealphabet; charCount++ )
    {
        if( countArray[ charCount ] )
        {
            ...do fun stuff...
        }
    }
    Also, if you mean 'A', say 'A'. Don't force some arbitrary integer value in all of your comparisons. The language cares little about implementation specific character sets. If you want to check from 'A' to 'Z' and subtract 'A' from it, then do so.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You might tinker with this and add perhaps an isalpha from <ctype.h>.

    [the usual]
    Don't use void main().
    Don't use gets.
    Check for EOF after each input.
    FAQ.
    [/the usual]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (aRep >= 97 && aRep <= 122)
    Try something like
    if ( islower(aRep) ) aRep = toupper(aRep);

    And
    #include <ctype.h>

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you can just use
    Code:
    aRep = toupper(aRep);
    since toupper() doesn't do anything to non-lowercase letters.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The isalpha bit I mentioned was for counting letters only, by the way.
    Code:
          static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
          int     ch, count [ sizeof alphabet / sizeof *alphabet - 1 ] = {0};
          size_t  i;
          while ( (ch = fgetc(file)) != EOF )
          {
             if ( isalpha(ch) )
             {
                const char *found = strchr(alphabet, toupper(ch));
                if ( found != NULL )
                {
                   ++count [ found - alphabet ];
                }
             }
          }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by quzah
    The language cares little about implementation specific character sets. If you want to check from 'A' to 'Z' and subtract 'A' from it, then do so.
    I wouldn't do that either, because you are relying on implementation specific character sets (ASCII has a contiguous 'A' to 'Z', others might not). Dave Sinkula's strchr plus alphabet array solution looks more solid.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, A to Z is guarinteed to be consecutive.
    Code:
    5.2.1 Character sets
    1 Two sets of characters and their associated collating sequences shall be defined: the set in
    which source files are written (the source character set), and the set interpreted in the
    execution environment (the execution character set). Each set is further divided into a
    basic character set, whose contents are given by this subclause, and a set of zero or more
    locale-specific members (which are not members of the basic character set) called
    extended characters. The combined set is also called the extended character set. The
    values of the members of the execution character set are implementation-defined.
    2 In a character constant or string literal, members of the execution character set shall be
    represented by corresponding members of the source character set or by escape
    sequences consisting of the backslash \ followed by one or more characters. A byte with
    all bits set to 0, called the null character, shall exist in the basic execution character set; it
    is used to terminate a character string.
    3 Both the basic source and basic execution character sets shall have the following
    members: the 26 uppercase letters of the Latin alphabet
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    the 26 lowercase letters of the Latin alphabet
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    the 10 decimal digits
    0 1 2 3 4 5 6 7 8 9
    the following 29 graphic characters
    ! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~
    the space character, and control characters representing horizontal tab, vertical tab, and form feed. The representation of each member of the source and execution basic character sets shall fit in a byte. In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
    Read the last line.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
    That's just the digits 0-9.

    [edit]Thus allowing EBCDIC in C.
    Last edited by Dave_Sinkula; 12-14-2005 at 08:28 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yup, that's just for 0-9. Look up EBCDIC.

    Edit: Dave's edit makes my post redundant
    Last edited by cwr; 12-14-2005 at 08:32 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, I read it as:

    "Each character's value, after zero (since that's set aside from NULL), will be 1 less then the one after it."

    Thus, if 'C' is given 23, 'D' will be 24. I was thinking it pertained to every list above, and when it said "zero", it meant "the character whose value is zero", not the digit zero.

    [edit]
    My eyes apparently glazed over when they hit the phrase "decimal digits" in the last sentence. :P Although, come to think of it, I'm not sure what I was thinking, since I've heard of EBDIC, and knew it was a different setup. Just not thinking clearly today. Must need more beer.
    [/edit]

    Quzah.
    Last edited by quzah; 12-14-2005 at 08:38 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
    It specifically mentions "the above list of decimal digits" as the criterion for the "one greater than the value of the previous". The only list of decimal digits above are 0,1,2,3,4,5,6,7,8,9.

    Edit: and if it pertained to all the sets above, it would render ASCII non-conformant, since the graphic characters are not all contiguous
    Last edited by cwr; 12-14-2005 at 08:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. for loop problem What's Wrong???
    By benjamin923 in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2007, 12:36 AM
  5. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM