| | |
Hangman, How do I make spaces?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,815
Reputation:
Solved Threads: 501
•
•
•
•
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.
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/c...e/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..
Here is info: http://www.cplusplus.com/reference/c...e/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..
C++ Syntax (Toggle Plain Text)
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; }
Last edited by phalaris_trip; May 1st, 2008 at 9:42 pm.
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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.)
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <float.h> #include <string> #include <time.h> using namespace std; main() { srand(time(NULL)); int BadGuesses = 0; 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()) BadGuesses++; else while (Location < SecretWord.size()) { GuessWord.replace(Location,1,Letter); Location = SecretWord.find(Letter, Location + 1); } //step eleven if (BadGuesses == 4) { cout << "Hell no"; } if (BadGuesses == 5) { cout << "Filler"; } if (BadGuesses == 6) { cout << "The secret word was " << SecretWord << " You lose \n"; } if (SecretWord == GuessWord) { cout << SecretWord << endl; cout << "I'ma tha wiener! \n"; } } }
Last edited by Narue; May 2nd, 2008 at 9:29 am. Reason: Added code tags
![]() |
Similar Threads
- QBasic character counter (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: simple linked list problem
- Next Thread: cod help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






