Toby Cannon
Toby Cannon

Reputation: 735

C Program calling array containing strings within a loop

I am fairly new to C, and am having problems with my array. The application is an anagram game, and should compare the users guess to the correct word. I am trying to log all of the incorrect answers, as well as the correct versions of the answers in two seperate arrays. However, at the end of the program when I try and print all of the incorrect guesses and correct versions, it only prints the first term in the array. I think it may be a problem caused by the arrays, as they are holding strings, so in C it is essentially an array within an array I think? Why does my code just print the first term from the arrays?

(There is more code, however this is just the key part, I think this is the only section that needs looking at)

char correctWord[20];
char anagramWord[20];
int  guesses, score;
char* incorrectGuess[20];
char* correctVersion[20];

void userGuess(){
char userWordGuess[20];

printf("Anagram: ");
printf(anagramWord);
printf("\n Your Guess: ");
scanf("%s",userWordGuess); //Reads in user input
strtok(correctWord, "\n");
guesses++;

if(strcmp(userWordGuess, correctWord) == 0){
    printf("Congratulations, you guessed correctly! (Please wait for the 

next question...)\n");
        score++;
        Sleep(1600);
        system("cls");
    }else{
        printf("Incorrect, try harder!(Please wait for the next question...) \n");
        Sleep(1600);
        system("cls");
        int i = 0;
        incorrectGuess[i]=(userWordGuess);
        correctVersion[i]=(correctWord);
        i++;
    }
}

void finalScore(){
    int i;
    system("cls");
    int percentage = ((score/guesses) * 100);
    printf("Congratulations - Game Complete!");
    printf("\n Guesses: %d", guesses);
    printf("\n Score: %d", score);
    printf("\n Percentage Correct: %d", percentage);
    int numberOfIncorrect = (guesses-score);
    for(i=0;i<=numberOfIncorrect;i++){
        printf(incorrectGuess[i]);
        printf(correctVersion[i]);
    }
    getch();
}

Upvotes: 0

Views: 71

Answers (3)

Arun A S
Arun A S

Reputation: 7006

In this code, you are not initializing guesses and score with any values, and you try to increment them. You should have

int  guesses = 0 , score = 0 ;

and then, in your else block

else
{
    printf("Incorrect, try harder!(Please wait for the next question...) \n");
    Sleep(1600);
    system("cls");
    int i = 0;                             // you initialize i to 0 every time
    incorrectGuess[i]=(userWordGuess);
    correctVersion[i]=(correctWord);
    i++;
}

in the commented line, you initialize i to 0 each time.

Upvotes: 1

unwind
unwind

Reputation: 399753

This:

incorrectGuess[i]=(userWordGuess);

is wrong, the string is not copied. All you're doing is storing a pointer to the array holding the current guess, and that array's the same all the time.

You need to copy the actual characters into newly allocated memory to remember them. You can also make it a 2D-array:

char incorrectGuess[20][100];

and just strcpy() the current guess in there. Beware so you don't overwrite, use snprintf() if you have it.

Upvotes: 0

kvorobiev
kvorobiev

Reputation: 5070

At first, variables guesses and score doesn't init. You need something like

int  guesses=0, score=0;

And in this lines

incorrectGuess[i]=(userWordGuess);
correctVersion[i]=(correctWord);

you do not copy the string. You should use

incorrectGuess[i]=strdup(userWordGuess);
correctVersion[i]=strdup(correctWord);

instead. Or you could use malloc and strcpy like this

incorrectGuess[i] = malloc(strlen(userWordGuess)+1);
strcpy(incorrectGuess[i], userWordGuess);
correctVersion[i] = malloc(strlen(correctWord)+1);
strcpy(correctVersion[i], correctWord);

In this cases, at the end you need to free the allocated memory like

free(incorrectGuess[i]);
free(correctVersion[i]);

Upvotes: 0

Related Questions