Home > database >  why only the index 0 changes him value to ""
why only the index 0 changes him value to ""

Time:04-02

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;

}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related