I'm a student and taking an intro to C class and would appreciate some help!

I've got a file with 10 words that I've loaded into a 2D array. I've written code that will generate a random number between 0 and 9 to select a string from the array. I'm using random_word for this variable

I ask the user to guess a single letter which they think may be in the string selected. I'm using guess for that variable

I'm trying to write a function that takes the randomly selected string in the array and checks the guess that the user input.

My code compiles but when I get to the part where it asks the user to input the character they would like guess, the program just hangs after I've typed a single character and hit return.

int findchar(char words[][ARRSIZE], char guess, int random_word){

    int i;

        while(words[random_word] != '\0'){
            if(words[random_word] == guess)
                return 1;
        return -1;

Once the function is run I'm expecting it to return a 1 or a -1 which I check with these if statements.

is_letter = findchar(words, guess, random_word);
        if(is_letter == -1){
            printf("%c, is not there!\n", guess);
        else if(is_letter == 1){
            printf("There is a %c in this word!\n", guess);
6 Years
Discussion Span
Last Post by atechnikality

You wind up comparing a char, to a string in words - that won't work.

In your call to findchar(), I'd just pass the one word that you will be checking, rather than the array. You seem to be confused about that, so:

words[random_word][] should do.

ARRSIZE sounds wrong, also. You have ROWS and COLS in a 2D array, and the ROWS * COLS equals your array size.

Then you need to step through the word, checking words[random_number]== guess, one char at a time. (incrementing i)

And welcome to the forum! ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.