为什么只有索引0将他的价值变成“ quot”。

发布于 2025-01-18 12:11:30 字数 2444 浏览 0 评论 0原文

我将直接

上下文:我正在制作一个刽子手游戏

脚本“bug”区域:

while(lifes > 0){

    cout << "word: ";
    for(int i=0; i<size; i++){
    cout << secret[i];
    }

cout << endl;
cout << "lifes: " << lifes << endl;

cout << endl << "choose a letter..." << endl;
cin >> letter;

check=false;
for(int i=0; i<size; i++){

    if(key[i] == letter[0]){
    secret[i] = letter[0];
    check=true;
    } 

}

if(check == false){
  lifes--;
}
}

问题:

我将模拟发生的情况:

让我们将秘密单词视为“熊”,好吗?

第一个循环=

word: ----
lifes: 5

cin >> 'b'

第二个循环=

word: b---
lifes: 5

cin >> 'a'

第三个循环=

word: -a-
lifes: 5

cin >> 'b'

第四个循环=

word: b-a-
lifes: 5

看???当我输入一个新字母时,第一个字母变成空格,但是如果我再次输入该字母,它就会出现!

我真的不知道如何解决这个问题。

向您寻求所有帮助,并对糟糕的英语表示歉意,哈哈。

如果你想要完整的代码:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

    //definição de variáveis
    
    char chave[50], palavra[50], letra[1];
    int tam=0, pos=0, vidas=5;
    bool acerto=false;
    
    //estabelecendo palavra-chave
    cout << "Qual a palavra?" << endl;
    cin >> chave;
    
    system("cls");
    
    //descobrindo o tamanho da palavra-chave
    while(chave[pos] != '\0'){
        tam++;
        pos++;
    }
    
    //escondendo a palavra secreta
    for(int i=0; i<tam; i++){
        palavra[i] = '-';
    }
    
    /*
    .
    . etapa 1
    .
    */
    
    while(vidas > 0){
        
        //criar painel
        cout << "Palavra: ";
        for(int i=0; i<tam; i++){
            cout << palavra[i];
        }
        cout << endl;
        cout << "Vidas: " << vidas << endl;
        
        cout << endl << "Escolha uma letra..." << endl;
        cin >> letra;
        
        //verificar se tem algum caracter identico à letra
        acerto=false;
        for(int i=0; i<tam; i++){
            
            if(chave[i] == letra[0]){
                palavra[i] = letra[0];
                acerto=true;
            } 
        }
        
        if(acerto == false){
            vidas--;
        }
        
        //fim do loop
        system("pause");
        system("cls");
    }
    
    
    
    return 0;

}

I'll be directly

Context: I'm making a hangman game

Script "bug" area:

while(lifes > 0){

    cout << "word: ";
    for(int i=0; i<size; i++){
    cout << secret[i];
    }

cout << endl;
cout << "lifes: " << lifes << endl;

cout << endl << "choose a letter..." << endl;
cin >> letter;

check=false;
for(int i=0; i<size; i++){

    if(key[i] == letter[0]){
    secret[i] = letter[0];
    check=true;
    } 

}

if(check == false){
  lifes--;
}
}

THE PROBLEM:

I'll simulate what happens:

lets take the secret-word as "bear", ok?

first loop =

word: ----
lifes: 5

cin >> 'b'

second loop =

word: b---
lifes: 5

cin >> 'a'

third loop =

word: -a-
lifes: 5

cin >> 'b'

fourth loop =

word: b-a-
lifes: 5

see???? When I input a new letter, the first letter turn a blank space, but if I input the letter again, it appears!!

I really dont know how to solve this.

Tank you for all help and sorry for the bad english haha.

if you want the full code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

    //definição de variáveis
    
    char chave[50], palavra[50], letra[1];
    int tam=0, pos=0, vidas=5;
    bool acerto=false;
    
    //estabelecendo palavra-chave
    cout << "Qual a palavra?" << endl;
    cin >> chave;
    
    system("cls");
    
    //descobrindo o tamanho da palavra-chave
    while(chave[pos] != '\0'){
        tam++;
        pos++;
    }
    
    //escondendo a palavra secreta
    for(int i=0; i<tam; i++){
        palavra[i] = '-';
    }
    
    /*
    .
    . etapa 1
    .
    */
    
    while(vidas > 0){
        
        //criar painel
        cout << "Palavra: ";
        for(int i=0; i<tam; i++){
            cout << palavra[i];
        }
        cout << endl;
        cout << "Vidas: " << vidas << endl;
        
        cout << endl << "Escolha uma letra..." << endl;
        cin >> letra;
        
        //verificar se tem algum caracter identico à letra
        acerto=false;
        for(int i=0; i<tam; i++){
            
            if(chave[i] == letra[0]){
                palavra[i] = letra[0];
                acerto=true;
            } 
        }
        
        if(acerto == false){
            vidas--;
        }
        
        //fim do loop
        system("pause");
        system("cls");
    }
    
    
    
    return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2025-01-25 12:11:30
char chave[50], palavra[50], letra[1];

定义letra为1个字符的数组。这是一个问题,因为当cin&gt;&gt; letra;读取到letra中,它读取为无效终止字符串而不是一个字符。不幸的是,一个字符的1个字符阵列中没有空间和一个null终结器,因此letra [0]保留该字符,不存在letra [1]保持零终端。这是界限的文字,并调用所谓的不确定的行为。从技术上讲,任何事情都可能发生。

似乎发生的事情是letra [1]恰好与palavra [0]的位置相同,因此'b'被无效的角色覆盖。

解决方案:

char chave[50], palavra[50], letra;

然后

if(chave[i] == letra){
    palavra[i] = letra;
    acerto=true;
}

让角色成为一个字符。

char chave[50], palavra[50], letra[1];

defines letra as an array of 1 character. This is a problem because when cin >> letra; reads into letra, it reads as a null terminated string and NOT a single character. Unfortunately there is no room in a 1 character array for one character and a null terminator, so letra[0] holds the character and the non-existent letra[1] holds the null terminator. This is a write out of bounds and invokes what's called Undefined Behaviour. Technically anything can happen.

What appears to be happening is letra[1] happens to be the same place in memory as palavra[0], so the 'b' is overwritten with a null character.

Solution:

char chave[50], palavra[50], letra;

and later

if(chave[i] == letra){
    palavra[i] = letra;
    acerto=true;
}

Let the character be a single character.

皓月长歌 2025-01-25 12:11:30

好的,鉴于您正在尝试学习,并且您不受任何典型的家庭作业规则的限制,因此您可以按照以下第一步正确执行操作。

第一的:
std :: String s替换这些char []
在C ++中,您已经构建了为了做各种工作的类,管理字符串就是其中之一,std :: String是必经之路。

第二:
字母实际上是一个字母,因此不需要是char [1],它只能是char

第三:尝试用英语编写变量,当您要共享代码时,任何人都更容易。

从这些简单的步骤开始,您将编写更好的代码,从而偶然地帮助您避免错误。

PS,我不会放置您的代码的工作和改进版本,因为我认为您会自己学习更多,而不仅仅是从这里复制一些代码。

ok, given you are trying to learn, and you are not limited by rule of any sort typical of homeworks, you can follow these first steps to do things correctly.

first:
replace those char[] with std::strings.
In c++ you have classes already built to do various stuff, managing strings is one of those, and std::string are the way to go.

second:
letter is literally a single letter, so is does not need to be a char[1] and it can be just a char.

third: try to write variables in english, it's easier for anyone when you are going to share code.

starting from these simple steps you'll write better code that will incidentally help you to avoid errors.

P.s. I'm not going to put a working and improved version of your code 'cause I think you will learn more doing it by yourself instead of just copying some code from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文