dimanche 29 novembre 2015

How to add a char into a string

So I am creating an hangman game and want to add a char into a string. I want to add a char of guess to the gatherguess string until the gatherguess matches hangman. Feel free to add some helpful tips to my code that will make me better. Also if it would be more then nice if you can also give me some sample code with dynamic memory allocation.

    #include <stdio.h>
    #include <string.h>
    #include <iostream>     // std::cout

          #include <stdio.h>
#include <string.h>
#include <iostream>     // std::cout
#include <algorithm>    // std::for_each
#include <vector>
#include <set>
#include <string>
bool isitdone(std::string foo, std::string hang){
return foo == hang ? true : false ;
}
int main(){
std::string hangman;
char guess;
std::string gatherguess; //Get the letters guessed.
std::cin >> hangman; //Player enter the word to guess.
bool checkstatement; // Check to see if game is over.



for(int i =0; i < 10; ++i)
    {

std::cin >> guess; //Individual characters to guess
std::string doo;
int wordsin;
doo = hangman;

int y;
if(doo.rfind(guess) != std::string::npos)
    {


std::cout << "Right " << guess << " Is in the word"  << std::endl;


std::cout << std::endl;

checkstatement = isitdone(gatherguess,doo);// I want to add guess char into gatherguess 
//then check to see if gatherguess is equal to the word then the game will be complete
if(checkstatement == true)
    {
    return 0;
    }
    } else
    {
    std::cout << "Wrong" << std::endl;

    }



}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire