Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,496 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,364 people online right now. Registration is fast and FREE... Join Now!




Hangman Issue

 
Reply to this topicStart new topic

Hangman Issue, Trying to get hangman to work, using a string.

SeCTRiX
21 Mar, 2007 - 10:32 AM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 8


My Contributions
This is my entire Hangman code. Starts from generation a random number 1-5 using time to select 1 of 5 words with 5 letters each. Which this works using "string". When i get down to the next part under "Loop and Counter" I am having trouble converting my string pAnswer which contains my 5letter word into 5 chars for guessing. I dont know too much about strings however i tried looking in my text book, and several websites to find a solution. The rest of this code seems to work in my Counter Results & Generation Sections.

What I started was using g1 - g5 as my variables for each letter of the pAnswer word. If you guys have any tips or possibly help fix my issue please let me know.






CODE


#include <iostream> // Input-Output
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand(
#include <string.h> //use string

using namespace std;


// *****************RANDOM**NUMBER**AND**WORD**GENERATION**************************
*
int main()
{
    string pAnswer;
    int rNumber;
    srand(time(0));                // Initialize random number generator using time
    rNumber = (rand() % 5) + 1;    // rNumber = random# 0-5 + 1 from 0

    //cout << rNumber << endl;//random number output

            if    (rNumber == 1)
            {
                  pAnswer = "house";
                  //cout << pAnswer.c_str();
            }
            else if (rNumber == 2)
            {
                  pAnswer = "grass";
                  //cout << pAnswer.c_str();
            }        
            else if (rNumber == 3)
            {
                  pAnswer = "cloud";
                // cout << pAnswer.c_str(); Displays word ( Was a test to make sure it worked )            
            }
            else if (rNumber == 4)
            {
                  pAnswer = "river";
                //cout << pAnswer.c_str();    
            }
            else if (rNumber == 5)
            {
                  pAnswer = "fence";
                // cout << pAnswer.c_str() << endl;
            }

// ********************LOOP**AND**COUNTER******************************************
********
    // pAnswer = puzzle word


    cout << "\n  HANGMAN PROJECT - \n\n\n ";
    cout << "    Welcome to the hangman game. This game was created by Aaron.\n ";
    cout << " You will be tested in a series of guesses by letter to complete the five   \n ";
    cout << " letter word. For each guess given you step close to being hung. You have a \n ";
    cout << " total of 10 guesses. GOOD LUCK!!!\n\n ";

    char guess;
    char c1='_',c2='_',c3='_',c4='_',c5='_';

    int counter = 1;
    while ( counter <= 10) //loop continuation up to 10 times
        {
            cout << "\nPuzzle: "<< c1 << " " << c2 << " " << c3 << " " << c4 << " " << c5 << " ";
            cout <<"\n" << counter << " Enter a letter guess: ";
            counter++;        //increment control variable by 1
            cin >> guess;     // put user input into 'guess'
            system("cls");    // Clear screen
            
            
        //    if (guess ==  c1)
        //        g1 = guess;
        //    cout << "\nGreat Guess \n\n";
        //    if (guess ==  c2)
        //        g2 = guess;
        //    cout << "\nGreat Guess \n\n";
        //    if (guess ==  c3)
        //        g3 = guess;
        //    cout << "\nGreat Guess \n\n";
        //    if (guess ==  c4)
            //        g4 = guess;
        //    cout << "\nGreat Guess \n\n";
        //    if (guess ==  c5)
        //        g5 = guess;
        //    cout << "\nGreat Guess \n\n";
        //    if (guess !=  c1,c2,c3,c4,c5; )
            
        //        cout << "\nThis word doesn't contain the letter\n"<< endl;
        
            

//****************Counter***Results*********************************

    if (counter > 1) cout <<"   +----+     "<<endl;
    if (counter > 2) cout <<"   |    |     "<<endl;
    if (counter > 3) cout <<"   |    O     "<<endl;
    if (counter > 4) cout <<"   |   /|\\   "<<endl;
    if (counter > 5) cout <<"   |  / | \\  "<<endl;
    if (counter > 6) cout <<"   |   / \\   "<<endl;
    if (counter > 7) cout <<"   |   | |    "<<endl;
    if (counter > 8) cout <<"   |=========="<<endl;
    if (counter > 9) cout <<"   |Last Breath"<<endl;
    if (counter > 10) cout<<"   YOU ARE DEAD\n"<<endl<<endl;
    if (counter > 10) cout<<"GAME OVER You have ran out of guesses!"<<endl<<endl;
        }

    return 0;
}



User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hangman Issue
21 Mar, 2007 - 11:04 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
you didn't really as a question... what problem are you having.

It would seem that you were compairing the guess with the character "_" which makes little or no sence. Maybe you mean something like:

CODE

if (guess == pAnswer[0] )
{
c1 = pAnswer[0];
cout << "\nGreat Guess \n\n";
} else if(guess == pAnswer[1] )
{
c2=pAnswer[1];
cout << "\nGreat Guess\n\n";
} etc etc

of course saving the answers in upper-case and then converting the guess to uppercase would make things work a little more smoothly.
User is offlineProfile CardPM
+Quote Post

SeCTRiX
RE: Hangman Issue
21 Mar, 2007 - 11:23 AM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 8


My Contributions
QUOTE(NickDMax @ 21 Mar, 2007 - 12:04 PM) *

you didn't really as a question... what problem are you having.

It would seem that you were compairing the guess with the character "_" which makes little or no sence. Maybe you mean something like:

CODE

if (guess == pAnswer[0] )
{
c1 = pAnswer[0];
cout << "\nGreat Guess \n\n";
} else if(guess == pAnswer[1] )
{
c2=pAnswer[1];
cout << "\nGreat Guess\n\n";
} etc etc

of course saving the answers in upper-case and then converting the guess to uppercase would make things work a little more smoothly.


User is offlineProfile CardPM
+Quote Post

SeCTRiX
RE: Hangman Issue
21 Mar, 2007 - 11:32 AM
Post #4

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 8


My Contributions
Actually, that solved my problem believe it or not. i am gonna post only that part of my code. I got rid of else, and used only if, because any word with 2 of the same letters wasnt being displayed. Now it works. Thank you for your help I really appreciate it. Now just for me to end the loop when i get all 5 answers correct.



CODE


while ( counter <= 10) //loop continuation up to 10 times
        {
            cout << "\nPuzzle: "<< c1 << " " << c2 << " " << c3 << " " << c4 << " " << c5 << " ";
            cout <<"\n" << counter << " Enter a letter guess: ";
            counter++;        //increment control variable by 1
            cin >> guess;     // put user input into 'guess'
            system("cls");    // Clear screen
            
            
        if (guess == pAnswer[0] )
        {
            c1 = pAnswer[0];
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[1] )
        {
            c2 = pAnswer[1];
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[2] )
        {
            c3 = pAnswer[2];
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[3] )
        {
            c4 = pAnswer[3];
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[4] )
        {
            c5 = pAnswer[4];
            cout << "\nGreat Guess \n\n";
        }
        else
        {
        cout << "\nThis word doesn't contain the letter\n"<< endl;
        }



User is offlineProfile CardPM
+Quote Post

SeCTRiX
RE: Hangman Issue
22 Mar, 2007 - 09:05 AM
Post #5

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 8


My Contributions
I have one more question about this hangman situation. How do i get the guessing loop to break after i have guessed all 5 letters in the puzzle? I tried 2 ways to try to get the loop to break after all the letters have been guess. I have // out the code that i tried to attempt using. I used bool in which if all the letters were true, that the if statement would break; I think the If statement in which states if they are all true only works if one is true.
I also used the counter inwhich if 5 letters are guess correctly it would break, however that issue is that i can use the same letter thats already correctly guessed, and have it count as a second time correct. If this method is part correct, how do i disable that if statement (if possible) after its been used once the corrected letter is used.
If anyone possibly knows the solution to my prob, or a better way of exiting this loop after all 5 letters are revieled. Feel free to share your knowledge. - Thanks

CODE

//    bool letter1 = false;
//    bool letter2 = false;
//    bool letter3 = false;
//    bool letter4 = false;
//    bool letter5 = false;

    char guess;
    char c1='_',c2='_',c3='_',c4='_',c5='_';
    int counter = 1;    
//    int rGuess = 0;       //correct letters guessed
    while ( counter <= 10) //loop continuation up to 10 times
        {
            cout << "\nPuzzle: "<< c1 << " " << c2 << " " << c3 << " " << c4 << " " << c5 << " ";
            cout <<"\n" << counter << " Enter a letter guess: ";
            counter++;        //increment control variable by 1
            cin >> guess;     // put user input into 'guess'
            system("cls");    // Clear screen
            
        
        if (guess == pAnswer[0] )
        {
            c1 = pAnswer[0];
//            rGuess++;
//            letter1 = true;
            counter--;
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[1] )
        {
            c2 = pAnswer[1];
//            rGuess++;
//            letter2 = true;
            counter--;
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[2] )
        {
            c3 = pAnswer[2];
//            rGuess++;
//            letter3 = true;
            counter--;
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[3] )
        {
            c4 = pAnswer[3];
//            rGuess++;
//            letter4 = true;
            counter--;
            cout << "\nGreat Guess \n\n";
        }
        if (guess == pAnswer[4] )
        {
            c5 = pAnswer[4];
//            rGuess++;
//            letter5 = true;
            counter--;
            cout << "\nGreat Guess \n\n";
        }
//        if ( letter1 == true, letter2 == true, letter3 == true, letter4 == true
//        {
//            cout << "The word is "<<pAnswer.c_str()<<"   GREAT JOB!!!\n\n";
//            break;
//        if ( rGuess == 5 )
//        {
//            cout << "The word is "<<pAnswer.c_str()<<"   GREAT JOB!!!\n\n";
//            break;
//        }
        else
        {
        cout << "\nThis word doesn't contain the letter\n"<< endl;
        }


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month