Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Newbie

Name: Anonymous 2009-08-12 20:43

http://pastebin.com/m4319da6a
Basic hangman game, I got it from Beginning C++ Through Game Programming. I was doing one of the exercises at the end of a chapter and it said to take the original hangman game and make a function for getting the player's guess and make a function for checking if the letter is in the secret word. I'm not sure what the problem is, the code compiles fine. I wasn't sure where to create the variables at because leaving them where they were in main wasn't working because the function that uses them is declared before main. Any help would be appreciated, sorry for the WALL OF TEXT

Name: Anonymous 2009-08-12 20:52

>>1
C++
I found the problem.

Name: Anonymous 2009-08-12 20:53

>>2
0/10

Name: !3LrT5NRVks 2009-08-12 21:04

const string THE_WORD = words[0];            // word to guess

string soFar(THE_WORD.size(), '-');

You are trying to access words[0] before you push the words on to the the vector in main.  I'm surprised this compiles.  Just toss the const, leave the declaration for THE_WORD and soFar there, and do your actual initializing of those variables in the main.

Name: !3LrT5NRVks 2009-08-12 21:10

>>1
Also you are repeating a lot of code with the getGuess stuff, there is a better way to structure that.

When you check the guess, you first do string.find() then loop through the string looking for that character.  The nested looping is redundant, just use the inner loop and set a boolean flag if you find it and then check that after it.

Name: Anonymous 2009-08-12 21:13

>>4
http://pastebin.com/m45b0eaac
Is that what my code should look like now?
It still crashes.

Name: !3LrT5NRVks 2009-08-12 21:23

>>6
>You are trying to access words[0] before you push the words on to the the vector in main.
IHBT

Name: Anonymous 2009-08-12 21:46

>>3
It's easy to think replies like >>2 are trolls, considering that this is obviously your first programming language, but they're actually the best advice you could possibly get.
Use C or a decently high-level language instead.

Name: Anonymous 2009-08-12 22:04

>>8
Ok. Any other recommendations besides C?

Name: Anonymous 2009-08-12 22:16

>>9
For a first language, Python is probably your best bet. It's easy to get started in, and it has a lot of advanced features more experienced programmers will enjoy too, with a bare minimum of counterproductive shit. It's more likely to teach you good habits that carry over into other languages than bad ones.

Use version 2.5 or 2.6, though. Py3k isn't ready.

Name: Anonymous 2009-08-12 22:17

>>10
Ok, thanks for the advice!

Name: Anonymous 2009-08-12 22:19

>>10
Py3k is ready. People aren't ready for Py3k.

Name: Anonymous 2009-08-14 6:21

I'm quite convinced that the real problem of C++ is the const keyword. Put it somewhere and YOUR SOFTWARE WILL NEVER BECOME ENTERPRISE QUALITY SOFTWARE

Name: Anonymous 2009-08-14 7:25

>>13
s/NEVER/INSTANTLY/

Name: Anonymous 2009-08-14 9:41

>>9
Just learn Lisp.

Name: Anonymous 2009-08-14 11:42

>>15
And read SICP.

Name: Anonymous 2009-08-14 11:45

>>10
Py3k is absolutely fine for new programmers, and removes many of the pitfalls for people who think "Python" means "Just make the computer do what I want it to."

Name: Anonymous 2009-08-14 13:02

>>15-16
And then after that, learn Haskell.

Name: Anonymous 2009-08-14 13:09

>>18
And after that learn languages you can really use in the real world.

Name: The Amazing Anus !tly/rANUS. 2009-08-14 13:10

u mena PERL

Name: Anonymous 2009-08-14 13:35

>>17
Up to the point where they try to use a library discussed in their introductory text and it doesn't work. A lot of mainstream libraries still haven't been ported, and most reference materials still assume you're using 2.5.

Name: Anonymous 2009-08-14 14:28

>>21
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFYOUR MOMMA

Name: Anonymous 2009-08-14 14:57

Got it into (probably) working state, this is a messy and short refactor of your code.
http://pastebin.com/m7fa3826d

Name: Anonymous 2009-08-14 15:01

>>23
Also, get rid of that random shuffle and for now just use
THE_WORD = words[rand() % words.size()];

When you add multiple words to guess in the gameloop go back to shuffle.

Name: ​​​​​​​​​​ 2010-10-26 10:59

Name: 2012-10-06 17:41


Don't change these.
Name: Email:
Entire Thread Thread List