the value of length is seven, word in file is hello, chosen_letter can be any that user guesses. chosen_letter is char ofcourse.

my problem is when we type in character 'l' for chosen_letter and it only counts it once. also, the if-loop in the second while loop doesnt work. please help, my assignment is due tommorow :/

void check_letter(char chosen_letter, FILE *ip_file, int length){
int count_let[20], i=0, x=0;

length = length-2;
while((d1[i] = fgetc(ip_file)) != EOF ){

printf("%c %d\n", d1[i], i);

    if(d1[i] == chosen_letter){
        printf("* %d\n", length);
    count_let[x] = count_let[x] + i;    /* records i in count_let[x] */
    x++;
    }
i++;
}
printf("%d, %d \n", x, length);
while(length >= 0){

    if(length == count_let[x]){
     printf("%c", chosen_letter);
     } /* checks if length value equals count_let[x] value, if so prints the character. */
    else printf("*");                /* checks if length value not equals count_let[x] value, if so prints '*' */
    length--;
x--;
}
}

You need to initialize the array count_let to all 0's before calling that loop. You can do that when it is declared, like this:
int count_let[20] = {0};

Since you are reading the file one character at a time it is not necessary to read the characters into an array. Notice fgetc returns an integer, not char.

int c;
while( (c = fgetc(ip_file)) != EOF)
{


}

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.