Grime wrote: |
---|
Some people will say that you should have even lesser code in main function but I can't critique on that. |
I think you're right, but keep in mind I'm following through a book, and this chapter was about functions. He asked us to re-write his program by adding in two specific functions, since the previous version had everything in main().
FurryGuy wrote: |
---|
The biggest thing that stands out for me as being somewhat inefficient is shuffling the container of words. |
Seems to be the general consensus here, and as dutch pointed out, random_shuffle no longer exists anyway. Perils of learning from outdated source material... Good thing we have these forums!
jib wrote: |
---|
It also doesn't make much sense to const qualify a parameter sent by value. And note that passing std::string by value can be an expensive operation and you should consider passing them by reference/const reference. |
Does it not make sense
because using references is the better way? Or because it's not good practice to send any constants by value? Again, I'm working from a book and that's how the author set it up, his argument being that once chosen, the secret word won't be changing. Therefore, should THE_WORD not be constant? Or should I have a different solution for checking the user input against the word (I'm guessing pass the reference to the function instead of the string value)?
I'm still confused, all these years later, by the difference between iterators, references, and pointers, and when to use which. From everything I've read, so is every other newbie. I've even gone to the point of investigating
why C++ has pointers in the first place to see if it would help me understand. I would be eternally grateful to anyone who can point me towards a resource that can finally clear this up for me.
dhayden wrote: |
---|
Keep related code together. |
Originally I had:
1 2 3 4 5
|
// main loop
while (wrong < MAX_WRONG && soFar != THE_WORD)
{
used += guess;
guess = getGuess(used);
|
But I just had this nagging feeling that updating the string of used characters should be done
after getting the user's guess and checking it. I suppose it doesn't really matter, and you're right - keeping it together makes it clearer.
dhayden wrote: |
---|
Getting and validating input is a perfect example of a loop where the exit condition is really in the middle. Don't be afraid to code it that way. |
I think I see what you mean. It's an infinite loop that only exits/returns if the user enters a character that hasn't been used. Is that for clarity's sake? Also, I hear what you're saying about the braces. Which brings up another question. You made some other remarks about commenting my code, and as far as formatting and commenting (i.e. "coding style") goes, I've read so much conflicting opinion on the matter. What ultimately sets the standard in a professional or team setting?
Thanks again everyone, especially you, dutch - for going above and beyond and challenging me to think!