I'm developing a videogame themed hangman game right now for class and I was wondering how to put spaces in the word they were supposed to guess without making it where the player has to input a space (Makes the game impossible to win by the way.)

EX.
string SecretWord;
int R = rand()%10+1;
if (R == 1) SecretWord = "grand theft auto";
if (R == 2) SecretWord = "heavenly sword";
if (R == 3) SecretWord = "rock band";
if (R == 4) SecretWord = "the orange box";
if (R == 5) SecretWord = "crackdown";
if (R == 6) SecretWord = "god of war";
if (R == 7) SecretWord = "guitar hero";
if (R == 8) SecretWord = "halo";
if (R == 9) SecretWord = "world of warcraft";
if (R == 10) SecretWord = "knights of the old republic";

This is what results. Never ends. (The period represents a blank space where there is supposed to be a letter. The period is where a space would be.

You have guessed ghieavnlysword
Word to guess heavenly.sword
Enter a Letter:

Thanks for helping a newb.

I'm developing a videogame themed hangman game right now for class and I was wondering how to put spaces in the word they were supposed to guess without making it where the player has to input a space (Makes the game impossible to win by the way.)

EX.
string SecretWord;
int R = rand()%10+1;
if (R == 1) SecretWord = "grand theft auto";
if (R == 2) SecretWord = "heavenly sword";
if (R == 3) SecretWord = "rock band";
if (R == 4) SecretWord = "the orange box";
if (R == 5) SecretWord = "crackdown";
if (R == 6) SecretWord = "god of war";
if (R == 7) SecretWord = "guitar hero";
if (R == 8) SecretWord = "halo";
if (R == 9) SecretWord = "world of warcraft";
if (R == 10) SecretWord = "knights of the old republic";

This is what results. Never ends. (The period represents a blank space where there is supposed to be a letter. The period is where a space would be.

You have guessed ghieavnlysword
Word to guess heavenly.sword
Enter a Letter:

Thanks for helping a newb.

Not sure if this is what you are getting at or not. Your secret word is "guitar hero". The user guesses all of the letters correctly and you want that to be a win, but you have some function comparing letters guessed to letters in the string and the user never "guessed" a space so the game isn't over yet, even though it should be? It's not obvious to me what the problem is since you haven't posted all the code, but this would be my approach to the problem (certainly not the only one). Keep the string as you have it, including the space. In other words, SecretWord = "guitar hero". That's 11 characters, including the space. Set up a boolean array called guessedRight[11]. Initialize guessedRight to false for all characters except for the space, which is index 6. Initialize that to true. The player wins when all indexes of guessedRight are true. A player guesses 'r'. That's indexes 5 and 9, so change guessedRight to true for those indexes. When you display the guessed letter, display all of the indexes of SecretWord where guessedRight is true. Where guessedRight is false, display some impossible character like # to signify that that spot hasn't been guessed yet. Anything with a space is "right" before the user guesses anything, so the user never has to guess for that spot.

You can use isalpha() to check if it's a letter, or explicitly check for a non-space character. Provided you already have a system for checking guessed letters in general, this will be easy to adjust.
Here is info: http://www.cplusplus.com/reference/clibrary/cctype/isalpha.html

There are loads of other ways of doing it too..

Anyway I'm going to be really vague since you didn't post any code..

``````bool IsWordCompleted (std::string secretWord, std::string gameWord)
{

for (eachletter in secret word)
{
if (isalpha(eachletter in secret word) && eachletter in secret word != the corresponding letter in the game word)
return false;
}

return true;
}``````

Here is the code. I didn't post it all before because it has gigantic ascii images in it. (They are removed for your eyes' comfort.)

``````#include <iostream>
#include <float.h>
#include <string>
#include <time.h>
using namespace std;

main()
{
srand(time(NULL));
string Letter;
int Location;
string lettersguessed;
cout <<" It's time to play \"Game Man\" " << endl;
//step two
string SecretWord;
int R = rand()%10+1;
if (R == 1) SecretWord = "grand theft auto";
if (R == 2) SecretWord = "heavenly sword";
if (R == 3) SecretWord = "rock band";
if (R == 4) SecretWord = "the orange box";
if (R == 5) SecretWord = "crackdown";
if (R == 6) SecretWord = "god of war";
if (R == 7) SecretWord = "guitar hero";
if (R == 8) SecretWord = "halo";
if (R == 9) SecretWord = "world of warcraft";
if (R == 10) SecretWord = "knights of the old republic";

//step three
string GuessWord = SecretWord;
for (int x = 0; x < SecretWord.size(); x++)
GuessWord[x] = '.';
//step four
while (BadGuesses <6 && GuessWord != SecretWord)
{
//step five
//step six

//step seven
//step eight
cout <<"           You have guessed ";
cout << lettersguessed << endl;
cout << "Word to guess " << GuessWord << endl;

//step nine
cout << "Enter a Letter: ";
cin >> Letter;
cout << endl << endl;
lettersguessed = lettersguessed + Letter;
//step ten
Location = SecretWord.find(Letter,0);
if (Location > SecretWord.size())
else
while (Location < SecretWord.size())
{
GuessWord.replace(Location,1,Letter);
Location = SecretWord.find(Letter, Location + 1);
}
//step eleven
{
cout << "Hell no";
}
{
cout << "Filler";
}
{
cout << "The secret word was " << SecretWord << " You lose \n";
}
if (SecretWord == GuessWord)
{  cout << SecretWord << endl;
cout << "I'ma tha wiener! \n";
}
}
}``````

So try replacing the second condition in the while loop from "guessWord != secretWord" to "!isWordCompleted"

where isWordCompleted is a bool evaluated using my previous suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.