Thread: I'm New...=(

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    I'm New...=(

    Ok so, i'm currently trying to make a application that performs similar to a game of hangman.

    i currently have it set up to have the leader of the game choose a word, and when he/she types it in it stores it as a variable, and also stores the length to then tell the players later.

    this is what i have right now:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
     int strikes; // to tell the plyers how many wrong guesses
     int wordlength; //to tell plyers how long the word is
     char guess; //what the players will be defining to play the game
     char gameword[30]; //the designated word from the leader
    
     cout<<"For Game Leader:\nEnter Game Word Here:"; 
     cin.getline(gameword, 25); //leader of game enters the word of the game here
     wordlength=strlen ( gameword ); //stores the length of the word into "wordlength"
     if( wordlength>25){
         cout<<"The Word You Entered Was Too Long For This Game\n";
         cout<<"Please Try Another Word."; //checks to see if word will fit in the string allowed
     }
     else {
         cout<<"CONGRATULATIONS! You Have Chosen A Valid Word\n";
         cout<<"Now You Are Ready To Play The Game\n\n\n"; //tells the leader he/she has chosen a valid word, and it's time to play
     }
     cout<<"For Players:\nThe Game Word Has A Length Of "<<wordlength; //reveals the length of the game word to the players
     
    cin.get();
    }
    what i have works just as i want it, but now i need to get to a point to where i can look directly to the string defined and find individual characters.

    as the title says, i'm fairly new to this. i understand the logic, of most things. SO am i able to analyze the string made by the leader of the game and have, when the player types a single character as their guess to tell the player tht yes or no the letter is there, and there if the letter is there where as in:

    leader chooses hangman as the gameword
    player chooses a
    say yes, and position 2
    then
    player chooses x
    i say no
    and give them 1 strike(u have certain number left)

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    To find if the letter is in the word, you want to use std::string::find(). You're probally going to end up stuffing it in a loop so you can return every occurance of the letter in the word, unless you're only using words where the letter occurs once.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    18
    well i can't guarantee that the game leader will always use words with completely unique letters.

    i figure i might have to use the find function, but how do i implicate it into the code i have right now?

    because i have tried using the find thing(with little knowledge of the syntax) and it always produces an error.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok, let's psuedocode a function for you and see if it makes sense.

    Code:
    ContainsLetter(Word, Letter)
    {
        int Positions[Word.Length];
        int SearchIndex = 0, PositionsArrayPositionCounter = 0;
    
        while ((SearchIndex = Word.find(Letter, SearchIndex)) != npos)
        {
            Positions[PositionsArrayPositionCounter] = SearchIndex;
    
            SearchIndex++; // So find() isn't finding the same letter over and over.
            PositionsArrayPositionCounter++; // So we don't overwrite our previous result.
        }
    
        return(Positions);
    }
    Does that make a little bit more sense?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    18
    ummm unfortunately no...

    the ContainsLetter(Word, Letter)... is that part of the code, or just a title of what is going to be going on.

    i see that the the variable positions is going to be a string?
    then ur defining the SearchIndex as 0, but what is the next thing on that line?
    then the loop... how do u know what to put b4 "find". also what is npos?
    after that i think if all my other ?'s are answered to my understanding should be fine.

    i am as we talk here, trying to read a tutorial on the find function.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    Whats up... am I mis-understanding something? Are you just trying to analyze the word and find the user specified letter and where it exists? Why not just go with a simple loop?

    Code:
    for(int i = 0; i < wordlength; i++){
      if(gameword[i] == guess){
        // Letter found.  Output [cout] or store. variable i = character position.
      }
    }  // Done.  Entire word analyzed.
    There is also a function called strchr that locates a specific char within a string which could be used in a loop to find all occurring letters in a string and their locations.

    I have a feeling I'm misunderstanding your goal..

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    I think it's a good thing to use the stl functions, instead of more C-like functions as 'strchr'? Here is a good reference about the stl:

    http://www.cplusplus.com/reference/string/string/

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    18
    ok so here's what i have(again)
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
     HangMan:
     int strikes=0; // to tell the plyers how many wrong guesses
     int wordlength; //to tell plyers how long the word is
     char guess; //what the players will be defining to play the game
     string gameword; //the designated word from the leader
     string decision; //string for type of guessing
     string answer; //reference to the answer for the player's guess
     string lost; //choice by player to play again
     int menuchoice; //what type of meanu they want
     string success; //to decide if guessed letter is present
     string name; //Players Name
     string players; //If theres more players
     int p; //number of players
     int lives=10; //number of lives player has
     char astring[30]="-";
    
     cout<<"Choose A Number.\n"; //Displays Menu Text
     cout<<"1. Play Game\n";
     cout<<"2. Help and Guide\n";
     cout<<"3. Exit\n";
     cout<<"Choice:";
     cin>>menuchoice;
    switch ( menuchoice ) //Allows For Menu Option Selection
      {
      case 1:
        goto GameStart;
        break;
      case 2:
        goto HelpAndGuide;
        break;
      case 3:
        exit(0);
        break;
      default:
        goto GameStart;
        break;
      }
    
    GameStart: //Labels The Game's Start
    cin.get();
     system("cls");
    p=1;
    PlayerNames: //Labels Area For Player Identification
    cout<<"Enter Your Name Here:";
    cin>>name;
    cout<<"Welcome "<<name<<"!!\n";
    cout<<"More Players?'y/n'"; //Allows For Multiple Player Notification
    cin>>players;
    if (players=="y")
    {
        p=p+1;
        goto PlayerNames;
    }
    else if(players=="n")
    {
        system("cls");
        cout<<"The Number Of Players Is "<<p<<"\n";
        cout<<"Designate A Leader Of This Game Now.\n"; //Tells Players To Choose A Leader
    }
    cin.get();
       cout<<"For Game Leader:\nEnter Game Word Here:";
       getline(cin,gameword,'\n'); //leader of game enters the word of the game here
       system("cls");
       wordlength=gameword.length(); //stores the length of the word into "wordlength"
        if( wordlength>25)
        {
         cout<<"The Word You Entered Was Too Long For This Game\n";
         cout<<"Please Try Another Word."; //checks to see if word will fit in the string allowed
        }
        else
        {
         cout<<"CONGRATULATIONS! You Have Chosen A Valid Word\n";
         cout<<"Now You Are Ready To Play The Game\n\n\n"; //tells the leader he/she has chosen a valid word, and it's time to play
        }
       cout<<"\nFor Players:\nThe Game Word Has A Length Of "<<wordlength<<" Letters\n\n"; //reveals the length of the game word to the players
    
       for (char w=0;w<wordlength;w++)
       {
           cout<<astring;
       }
     PlayerInput: //Designates Time Of Game For Player Interaction
       cout<<"\n\nWhat Would You Like To Do?\n";
       cout<<"Type 'Answer' To Try And Guess The Word Or Type 'Turn' To Guess A Letter.";//Asks Player To Try Choose Between Guessing A Letter Or The Word
       cin>>decision;
        if (decision=="turn")//Goes Here If Player Wants To Guess Letters
        {
           system("cls");
           cout<<"Type Your Single Letter Guess Here:";
           cin>>guess;
           success="n";
           for(int i = 0; i < wordlength; i++)//Checks For Players Guess Validity
           {
             if(gameword[i] == guess)
             {
                 success="y";
               cout<<"Letter "<<guess<<" Is In Position "<<i+1<<" In The Game Word\n\n";//Displays Location Of Valid Guess
                   astring[i]=guess;
                   for(int i = 0; i < wordlength; i++)
                   {
                        if (astring[i]==guess)
                        {
                            cout<<astring;
                            if (astring==gameword)
                            {
                                goto Win;
                            }
    
                        }
                        /*if(astring[i]==guess)
                        {
                        cout<<astring;
                        }
                        if (astring[i]!=guess)
                        {
                            cout<<" ";
                        }*/
                   }
    
             }
           }
    
        if (success=="n")
        {
               cout<<"Sorry, The Letter You Have Guessed Was Incorrect.\n";//Tells Player Their Guess Was Not In The Word
               strikes=strikes+1;
               cout<<"Lives Remaining "<<lives-strikes<<"\n";//Takes Away Srikes From Total Initial Lives
               if(strikes==lives)//Check For Loss
               {
                  cout<<"YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
                  cin>>lost;
                  if (lost=="y")
                  {
                    goto HangMan;//Goes To Beginning of Game, If Chosen
                  }
                else
                {
                    exit(0);
                }
               }
        }
        }
        else if (decision=="answer")//Goes Here If Player Wants To Guess The Word
        {
           system("cls");
           cout<<"What Do You Think The Answer Is?";//Asks Player For The Guess Of What The Word Is
           cin>>answer;
           if( answer==gameword)
           {
               Win:
               system("cls");
               cout<<"CONGRATULATIONS! You Have Found The Word!\nYou Are Now The Game Leader!\n\n\n";//Reveals Victory
               cout<<"Hit Enter To Start New Game";
               cin.get();
               goto GameStart;
           }
           else
           {
               cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
               strikes=strikes+2;
               cout<<"Lives Remaining "<<lives-strikes<<"\n";
               if(strikes==lives)
               {
                  cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
                  cin>>lost;
                  if (lost=="y")
                  {
                    goto GameStart;
                  }
                  else
                  {
                    exit(0);
                  }
               }
           }
        }
    
    
    goto PlayerInput;
    HelpAndGuide://Goes Here From 2nd Menu Selection And Provides Game Information
    system("cls");
    cout<<"This Is The Consol Version Of HangMan!\n";
    cout<<"You Have A Space Of 25 Letters For The Game Word.\n";
    cout<<"When Playing This Game And Taking Guesses, A Wrong Guess Will Result\nIn A Strike.\n";
    cout<<"Also When Guessing Letters, The Letter You Type As Your Guess Will.\n";
    cout<<"Result In A Number Or Numbers Of Position(s) That The Letter Is.\n";
    cout<<"That Number Is A Reference To The Location Inside The Game Word.\n";
    cout<<"You Have 10 Chances, Including Both Letter Guesses And Full Word Guesses.\n";
    cout<<"Letter Guesses Are Worth 1 Life\n While Word Guesses Are Worth 2 Lives.\n";
    cout<<"After 10 Chances Are Gone, You Will Lose.\n";
    cout<<"When Typeing Anything In This Game, Type Exclusively In Lowercase.\n\n\n\n\n";
    cout<<"Hit Enter";
    cin.get();
    goto GameStart;
    
    cin.get();
    }
    here, i have it displaying a string array to show the player a visual of the word(given the letters they've guessed)
    the only problem is, it only works if the letters are guessed in order. for example, if the game word is happy, and the first guess is y, it WILL store the letter into the array, but it WILL NOT display it until h,a, and p are guessed.

    ne ideas?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Instead of using goto, pull out parts of your code into functions, then call those functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    18
    ok?
    1. how exactly do i do that
    2. how do i call them
    3. how do i fix my string problem

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    1) you have defined 'main' as a function which returns 'int' (and this is correct), so return it. For the main function, return a '0'.

    2) make functions instead of codeblocks, call the functions instead of 'goto'.

    3) make functions instead of codeblocks, call the functions instead of 'goto'.

    4) p = p+1 is equivalent to p += 1 is equivalent to p++

    5) consider using "#include<string>" instead of "#include<cstring>"

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    18
    my beginner brain was just blown...

    so instead of having everything under 'main' just make a new block of code? and can u provide and example of a call? do u just use 'cout'?

    ur response #4... why did u put that in there?

    and 5, what exactly does that mean?

  13. #13
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    void function1( void)
    {
      cout<< "this is function 1"<< endl;
    }
    
    double function2( double arg)
    {
      double result = arg / 2;
      return result;
    }
    
    int main( void)
    {
      
      cout<< "let's call function1 !"<< endl;
    
      function1(); // empty argument list since function argument list is declared 'void' (empty)
    
      double justanumber = 10;
    
      double result = function2( justanumber);
    
      return 0; // the return value of main is of an 'int' type
    
    }

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    ur response #4... why did u put that in there?
    just to show you the equivalence... replace p by c and you see what the language's name means ;-)

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    18
    ok so for say, my code, int 'function name', then later just type 'function name()'?? and i understand the arguments, thank you for the reference tho.

Popular pages Recent additions subscribe to a feed

Tags for this Thread