vendredi 4 décembre 2015

If statement and and else if statement bouth execute

hey guys I'm relatively new to programing, I'm doing the hangman exercise. my problem is after an if statement is true and activates the subsequent code the else if statement also executes. this is the problematic code:

    for (int i = 0; i < word.size(); i++) {     //cheks the letters against the control word
        if (p1_input == word[i]) {
            guess[i] = p1_input;
            cout << "TEST 1!!!!" << endl;
        }
        else if (wrong_letters.find(p1_input) == std::string::npos) {
            wrong_letters += " ";
            wrong_letters += p1_input;
            guess_num++;
            cout << "TEST 2!!!!" << endl;
        }
    }
}

iv added the cout<<"test1"; and cout<<"test2" to make sure both code blocks execute.

cant for the life of me figure out why both the if and the else if work together.

full code:

#include<iostream>
#include<string>
#include<ctime>
#include<random>
using namespace std;

string Word_box();
//@keeps an asortment of words and picks a random one from the directory
//@return string- a random word to be used in the game

string Hangman(int);
//stores the hangman pics
//@param int- the amount of wrong guesses
//return string- the coresponding pic

int main() {
string word = Word_box();   //word to be guessed
string guess;               //user gessed word
int guess_num = 0;          //number of guesses
string wrong_letters;       //wrong letters inserted
char p1_input;              //user input

//test1
cout << word << " TEST" << endl;

//creating the guess word
for (int i = 0; i < word.size(); i++) {
    guess += "-";
}

//test 2
cout << guess << endl;

//insructions
cout << "\t\tHangman v1.0" << endl << endl;
cout << "Lets play a game...\nIm thinking of a word...\nTry and guess it..." << endl << endl;

//game loop
while ((word.compare(guess) != 0) && (guess_num != 8)) {
    cout << Hangman(guess_num) << endl << endl;
    cout << "\tYour wrong guesses:" << endl;
    cout << "\t[" << wrong_letters << " ]" << endl << endl;
    cout << "\t" << guess << endl << endl;
    cout << "Guess please: ";
    cin >> p1_input;
    cout << "\n\n\n\n\n\n\n";


    for (int i = 0; i < word.size(); i++) {     //cheks the letters against the control word
        if (p1_input == word[i]) {
            guess[i] = p1_input;
            cout << "TEST 1!!!!" << endl;
        }
        else if (wrong_letters.find(p1_input) == std::string::npos) {
            wrong_letters += " ";
            wrong_letters += p1_input;
            guess_num++;
            cout << "TEST 2!!!!" << endl;
        }
    }
}

//game over
cout << "\n\n\n\n\n\n\n";
cout << "The word was: " << word << endl;
if (word.compare(guess) != 0) {
    cout << Hangman(7) << endl << endl;
    cout << "GAME OVER!" << endl;
}
else {
    cout << Hangman(guess_num) << endl << endl;
    cout << "you win!" << endl;
}

cout << endl;
return 0;
}   //End

string Word_box() {
string game_word[10];                       //the word used in the game
srand(time(0));                             //random number generator
unsigned int rand_word = rand() % 10;       //number between 0-9

game_word[0] = "loot";
game_word[1] = "rnjesus";
game_word[2] = "dps";
game_word[3] = "troll";
game_word[4] = "skill";
game_word[5] = "mushroom";
game_word[6] = "noob";
game_word[7] = "derp";
game_word[8] = "pcmasterrace";
game_word[9] = "headshot";

return game_word[rand_word];
}

string Hangman(int guess_num) {
string hang_pic[8];         //stores the hngman pics

hang_pic[0] = "    -----\n    |   |\n    |\n    |\n    |\n    |\n    |\n    |\n    |\n----------\n";;
hang_pic[1] = "    -----\n    |   |\n    |   0\n    |\n    |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[2] = "    -----\n    |   |\n    |   0\n    |  -+-\n    |\n    |\n   |\n    |\n    |\n----------\n";
hang_pic[3] = "    -----\n    |   |\n    |   0\n    | /-+-\n    | |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[4] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | |   |\n    |\n    |\n    |\n    |\n----------\n";
hang_pic[5] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |\n    |\n    |\n----------\n";
hang_pic[6] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |  |\n    |  |\n    |\n----------\n";
hang_pic[7] = "    -----\n    |   |\n    |   0\n    | /-+-\\\n    | | | |\n    |   |\n    |  | |\n    |  | |\n    |\n----------\n";

return hang_pic[guess_num];
}

Aucun commentaire:

Enregistrer un commentaire