Krystian Dobkowski
Krystian Dobkowski

Reputation: 16583

Can't access the If statement

I have written code for a hangman game which worked normally on Linux (where I wrote it) but now when I compile it on OS X (also using g++) it behaves very weirdly.

I have this function called getCharacter() which basically request a single character from the user, then (because I personally found it easier) I saved the character as a type string instead of char. But when I need to compare if the letter selected exists in the word, I need to extract each character from the chosen word then compare it with the string character typed in by the user. So I do this pretty awkward conversion of char which I got from the .at(i) function of string to the a temp string which then I compare with the chosen letter. If it matches then put the letter into a vector at the i position. It worked grand on Linux, but here weirdly enough, when the user types in a letter even if it is in the chosen word, unless it is the 1st letter, the if() statement gets omitted.

So here is my function, I'm still learning so please excuse my lack of professionalism:

void Game::getCharacter(){ 

    string character;

    cin >> character;

    while((character.length() > 1) || (checkIfUsed(character))){
        cout << "Not a character or already used, try again:\n";
        cin >> character;
        cout << endl;
    }

    if(RETRIES_LEFT>0){ 
         for(int i = 0; i < DIFFICULTY; i++){
            char TEMP_CHAR = CHOESENWORD.at(i); 
            char *TEMP_CHAR_PTR = &TEMP_CHAR;   
            string TEMP_STRING_LETTER(TEMP_CHAR_PTR);

            if(TEMP_STRING_LETTER == character){

                FRAME.at(i) = character;
                GUESSED+=1;     
            }
        }

        RETRIES_LEFT-= 1;           
        NUM_LETTERS_USED+=1;            
        LETTERS_USED.push_back(character);  
    }   


}

Upvotes: 0

Views: 93

Answers (1)

Jorma Rebane
Jorma Rebane

Reputation: 626

Getting straight to the problem:

char TEMP_CHAR = CHOESENWORD.at(i);         // grab copy of a char at [i]
char *TEMP_CHAR_PTR = &TEMP_CHAR;           // take the address of the char
string TEMP_STRING_LETTER(TEMP_CHAR_PTR);   // call string(const char* str) 
        // constructor, which is incorrect because strlen(&TEMP_CHAR) will
        // give a pretty random result. this is due to how data is laid out
        // in memory. To sum this up, you're having a buffer overrun issue,
        // where the buffer is TEMP_CHAR (1 byte)

This is probably what you want to achieve:

if (CHOSENWORD[i] == character[0]) {

In either case, there is much to improve in your code, since it's obvious that you're just starting with C/C++. Rest assured, it's actually not that complicated.

How to do it easier? Well, first of all, you can start with only asking for one char:

char ch;
std::cin >> ch;     // get a single char
std::cin.sync();    // flush all extra input

This should make your code easier to handle.

Upvotes: 3

Related Questions