英文:
Little heart problem with my MSYS2 compiler
问题
你的问题似乎是在使用MSYS2编译器时出现了显示错误字符(例如心形符号)的问题。这可能是编码或终端设置的问题。要解决这个问题,你可以尝试以下几种方法:
-
字符编码问题:确保你的源代码文件以UTF-8编码保存。这可以在文本编辑器中设置。MSYS2可能对UTF-8支持较好。
-
终端设置:MSYS2的终端可能需要特定的字体或字符集设置。请检查终端的设置,确保它使用了合适的字符集,如UTF-8。
-
编译器选项:确保你在编译时没有使用任何奇怪的编译器选项,可能会影响输出的字符集。
-
库版本:MSYS2和在线编译器可能使用不同版本的标准C库,这可能会导致输出差异。确保你的环境中使用的库版本是正确的。
如果这些方法都无法解决问题,考虑使用其他编译器或集成开发环境(IDE)来编译和运行你的程序,以查看是否在特定于MSYS2的设置中出现了问题。
英文:
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] != '#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;
}
') {
letter++;
i++;
correctGuess[i].letterNumber = i;
correctGuess[i].letter = '#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;
}
';
}
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 == '#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;
}
'){
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?
答案1
得分: 1
这个“心”字符可能是一个控制字符。ASCII控制字符执行一些控制功能,但也可以直接显示;♥ 就是其中之一,它的代码是3。
注意它显示为下划线 _
的代替。这段代码打印 correctGuess[i].letter
。所以你可以合理地确定在代码的那个位置,它具有奇怪的值3。
为什么它在那里有这个值?因为它是未初始化的;它可以有任何值。未初始化的变量通常具有值0;这可能会使你的代码看起来正常工作。
要将 correctGuess
数组全部初始化为零,你可以使用“通用”零初始化习惯用法:
struct correctGuess correctGuess[10] = {0};
或者详细初始化(如果以后要更改为非零值,并需要明确指定哪些值放在哪里):
struct correctGuess correctGuess[10] = {
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'}
};
英文:
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, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'},
{0, 'struct correctGuess correctGuess[10] = {
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'},
{0, '\0'}
};
'}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论