Search code examples
cmsys2

Little heart problem with my MSYS2 compiler


I am working on a homework again and my homework is a hangman game with turkish cities. The code is not completed yet because there is a funny problem. When I run the code with MSYS2 compiler my output suppose to look like this:

_______ //the random cities letter numbers determines how many "_" in here
Sehri tahmin et! // "Guess the city!"
Bir harf soyle //pls tell me a letter
b //user input 
Tahmin ettigin harf: b // the letter u guessed
_ _ _ _ _ //?
Bir harf soyle //pls tell me a letter
u //user input
Tahmin ettigin harf: u // the letter u guessed
_ _ _ _ _ //?
Bir harf soyle //pls tell me a letter
a //user input
Tahmin ettigin harf: a // the letter u guessed
a _ _ _ _ //user input
Bir harf soyle //pls tell me a letter


But it looks like this:

_ _ _ _ _ 
Sehri tahmin et!// "Guess the city!"
Bir harf soyle //pls tell me a letter
b //user input 
Tahmin ettigin harf: b // the letter u guessed
♥ _ _ _ _ //?
Bir harf soyle //pls tell me a letter
u //user input
Tahmin ettigin harf: u // the letter u guessed
♥ _ _ _ _ //?
Bir harf soyle //pls tell me a letter
a //user input
Tahmin ettigin harf: a // the letter u guessed
a _ _ _ _ //user input
Bir harf soyle //pls tell me a letter

There is an heart appears until I guess the first letter correctly

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct correctGuess{
    int letterNumber;
    char letter;
};

int main() {
    srand(time(NULL));
    char cities[][10] = { "antalya","ankara","istanbul","izmir","kutahya","bursa","konya","afyon","balikesir","mugla" };
    

    char guessedLetter[30];
    struct correctGuess correctGuess[10];
    
    

    char indexLetter;


    int i = 0, guessCounter = 0;
    int letter = 0;
    int random = (int)(rand() % 10);

    while (cities[random][i] != '\0') {
        letter++;
        i++;
        correctGuess[i].letterNumber = i;
        correctGuess[i].letter = '\0';
    }
    
    
   

    for (int i = 0; i < letter; i++) {
        printf("_ ");
        if (i == (letter - 1)) {
            printf("\n");
        }
    }

    printf("Sehri tahmin et!\n"); //en: "Guess the city!"
    
    for (;;) {
        printf("Bir harf soyle\n"); //en: "pls tell me a letter"
        scanf(" %c", &indexLetter);
        while (getchar() != '\n'); 
        printf("Tahmin ettigin harf: %c\n", indexLetter); //en: "The letter u guessed
        guessCounter++;
        guessedLetter[guessCounter] = indexLetter;
        int correctGuessIndex = 0;
        for (int i = 0; i < letter; i++)
        {
            if(cities[random][i] == indexLetter){
                
                correctGuess[correctGuessIndex].letter = indexLetter;
                correctGuess[correctGuessIndex].letterNumber = i;
                
            }
            correctGuessIndex++;
        }
        
        for(int i = 0; i < letter; i++){
            if(correctGuess[i].letter == '\0'){
                printf("_ "); //the problem probably here!!
            }else{
                printf("%c ",correctGuess[i].letter);
            }

            if(i == (letter - 1)){
                printf("\n");
            }
        }
       
    }

 
    return 0;
}

So my problem only appears when I use msys2 compiler but when I use an online compiler like this it works fine. How do I fix it? And why is it happening?


Solution

  • This "heart" character is probably a control character. ASCII control characters do some control, but can also be displayed literally; ♥ is one of these, having the code 3.

    Note that it's displayed instead of underscore _. The code prints correctGuess[i].letter. So you can be reasonably sure that at that place in code, it has this weird value 3.

    Why does it have that value there? It's uninitialized; it can have any value. Uninitialized variables often have the value 0; this can appear as if your code works properly.


    To initialize all of the correctGuess array to zeros, you can use the "generic" zero-initialization idiom:

    struct correctGuess correctGuess[10] = {0};
    

    or detailed initialization (if you want to change it to nonzeros later, and need to specify clearly which values go where):

    struct correctGuess correctGuess[10] = {
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'},
        {0, '\0'}
    };