[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey yall, I was wondering if I could get some help with a program.
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 28
Thread images: 3
File: CPlusPlus.jpg (36 KB, 433x455) Image search: [Google]
CPlusPlus.jpg
36 KB, 433x455
Hey yall, I was wondering if I could get some help with a program. I know it probably looks horrendous but anyway, I'm trying to make a game of hangman but when I run it it won't display the word that's been guessed so far and even if you guess it the game keeps going til you lose.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
//maximum allowed wrong guesses
const int MAX_WRONG = 8;
//variable for difficulty
char difficulty;
int d = 0;
//vectors for hangman words for different difficulties
vector<string> eWords;
vector<string> mWords;
vector<string> hWords;
eWords.push_back("SHARK");
eWords.push_back("BOTTLE");
eWords.push_back("GRAPE");
eWords.push_back("STAR");
eWords.push_back("SAMPLE");
mWords.push_back("SQUASH");
mWords.push_back("JUPITER");
mWords.push_back("SWORD");
mWords.push_back("GINGER");
mWords.push_back("PERSON");
hWords.push_back("LAVENDER");
hWords.push_back("MEDIOCRE");
hWords.push_back("DANDELION");
hWords.push_back("KANGAROO");
hWords.push_back("ANTIDISESTABLISHMENTARIANISM");

//shuffle the words to generate random word
srand(static_cast<unsigned int>(time(0)));
random_shuffle(eWords.begin(), eWords.end());
random_shuffle(mWords.begin(), mWords.end());
random_shuffle(hWords.begin(), hWords.end());
//variable for the chosen word
string THE_WORD;
//display dashes for unguessed letters
string userGuesses(THE_WORD.size(), '-');
string used = "";
//variable for wrong guesses
int wrong = 0;
//while loop to select difficulty
while(d == 0)
{
cout << "Welcome to hangman. Choose a difficulty to begin" << endl;
cout << "E for Easy" << endl << "M for Medium" << endl << "H for Hard" << endl;
cin >> difficulty;
difficulty = toupper(difficulty);
>>
cout << difficulty << endl;

if(difficulty == 'E')
{
THE_WORD = eWords[0];
d = 1;
}
else if(difficulty == 'M')
{
THE_WORD = mWords[0];
d = 1;
}
else if(difficulty == 'H')
{
THE_WORD = hWords[0];
d = 1;
}
else
{
cout << "Sorry, that is not a valid entry." << endl;
}
}
//while loop for game play
while ((wrong < MAX_WRONG) && (userGuesses != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left.\n";
cout << "\nHere are the letters you've used so far:\n" << used << endl;
cout << "\nHere is your word so far:\n" << userGuesses << endl;
//get user's guessed letters
char guess;
cout << "\n\nWhat is your guess: ";
cin >> guess;
guess = toupper(guess);
//while loop if user guesses already guessed letter
while (used.find(guess) != string::npos)
{
cout << "\nThe letter " << guess << " has already been guessed." << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
//if statement if user guesses a letter
if (THE_WORD.find(guess) != string::npos)
{
cout << "Excellent! " << guess << " is in the word.\n";
//for loop to see if user's letter is in the word
for (int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
userGuesses[i] = guess;
}
}
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
//if statement if user loses
if (wrong == MAX_WRONG)
{
cout << "\nSorry, you lose!";
}
//else statement if user wins
else
{
cout << "\nCongratulations! You guessed the word!";
}
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
>>
any c++ gurus out there that wanna help a noob
>>
is that a no
>>
Hold on I'm no C++ guru but I'm having a read
>>
>>51750821
awesome thank you
>>
Screenshot of input/output?
>>
File: hangman.png (10 KB, 490x475) Image search: [Google]
hangman.png
10 KB, 490x475
>>51750893
Here ya go. When the game starts it should show

---------
for the length of the word. Then when you guess a correct letter(lets say the word is shark and I guess an A) it should look like this

--A--
>>
>>51750893
If it helps, the program was working fine until I added the levels of difficulty. Mainly the while loop at the beginning.
>>
You're aware that you can initialize a vector all at once, right?

This is also just a formatting thing, but it's less confusing when you break up cout statements like this:
      cout << "\n\nYou have " << (MAX_WRONG - wrong);
<< " incorrect guesses left.\n";


There's a cheap and easy algorithm to solving hangman involving hash sets, which are converted to arrays, and then sorted.

Anyway, the reason your loop isn't breaking is because you're comparing two string pointers against each other, and not the value of the string.

If you ever perform (string == string) in any sane programming language, what's going to happen is that it will compare the pointer against the pointer (or the object ID against the object ID), which will never be equal unless it is literally the same object/pointer.

So, every sane language has a specific function to compare strings against one another.
>>
>>51751030
Also, all of your variable names are fucking shit.
>>
>>51751030
Why was it working before then? Also all of this code is straight from a c++ book except the parts I added for difficulty levels.
>>
>>51751068
Most of them aren't mine. They came from my book. I just have to add 3 difficulties to the program.
>>
File: 1385628875448.png (5 KB, 239x258) Image search: [Google]
1385628875448.png
5 KB, 239x258
>>51751075
not the guy you replied to, but,
use git in the future and learn to use it properly, so if you mess your code up, you can go back and look
you don't have to use git remotely, you can use it on your own machine
then start from the sample code again, use versioning and see
but I can't imagine a book would have such terrible sample code honestly
>>
>>51751075
name and shame, which book is it?
>>
I've got the problem. Look at how you initialize userGuesses and where you declare THE_WORD
>>
>>51751112
Yeah the book seems very stupid and confusing. I'm usually good at computer stuff but I'm barely passing this class. I have an A in Chemistry and I don't even know what the fuck I'm doing in that class. The book is called Beginning c++ Through Game Programming by Michael Dawson
>>
>>51751140
Before when I initialized the word it was

const string THE_WORD = words[0];

but I need it to change for the difficulties.
Could you give me a hint as in how to fix what I've done?
>>
Be right back. gonna have a smoke break. Please don't leave me you guys are a life saver.
>>
>>51751175
Take a piece of paper, go line by line starting at "string THE_WORD;". Write down what your variables are. You won't get far before finding your problem.
>>
>>51751195
I'm still not getting it. Maybe I'm dense. All I know is it stopped working when I got rid of

const string THE_WORD = words[0]

and started the first while loop.
>>
>>51751195
Have you turned the program into an exe fixing the bugs and tested by chance?
>>
>>51751195
There's only 2 variables after THE_WORD and I'm not seeing a problem.

I can post the original code if you like.
>>
still there
>>
Can anyone else elaborate?
>>
>>51751030
How is that sane behaviour? Have you heard of operator overloading?
>>
>>51751886
many operators are overloaded in std::string including operator==
http://www.cplusplus.com/reference/string/string/operators/
Anon is fullof dhit
>>
Try to do what
>>51751195
suggested.

Take a piece of paper and try to run your program "manually":

Execute each statement taking into account the state you got by far on the paper.
When you encounter a variable declaration you write its name on an empty line with a box on the side: inside the box goes the value of the variable.
On assignments strike out the old value and write the new one.
Note that for complex objects you may need to store more than one value, for example vectors , strings and so on also have a 'size'. Also check on the docs what their constructors actually do with their parameters.
Thread replies: 28
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.