Thread: Problem with loop stopping at wrong time

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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