I was working on this program for my class assignment and a guy in my class suggested using a nested for loop to improve my code though he didn't explain how to go about doing that or how it improves it. I ended up getting a 2nd year who does peer tutoring to assist me with trying to figure out how to do it. Well it turned out the guy in my first year class was either insane or on a programming level the 2nd year wasn't on and knew stuff he didn't even know existed. Anyways he tried to integrate it into my program but couldn't figure out how to do it. And now part of my program doesn't work and I can't figure out what the issue is. The issue seems to be that it skips the entire "while (errorCount < 6 || correctCount < 5)" loop or at least it appears to and then starts the "while (errorCount == 6 || correctCount == 5)" loop even though it doesn't meet the requirements. I've tried redoing the loop 4 times using various loops but it isn't working for me for some weird reason. So can someone help solve this?
And please keep in mind I know the coding is terrible my prof just teaches the bare basics and then leaves everything else to us to learn so I'm rush learning this and only have 6 weeks of experience.
I think you should not implicitly criticise your teacher (the "I don't understand, therefore it is the teacher's fault" syndrome), nor knock your class colleague, who made an eminently sensible suggestion. "Nested" loops just mean one loop fully inside another - their use here would (a) make the code more readable and (b) make it more flexible than simply guessing one particular word "BASICS" - there's not much point in playing this game twice. Some string or character functions might also pre-empt the need for nested loops.
In terms of your loop skipping ... your prompt for playerInput is only done once and then you keep running the following "while" loop with the SAME value; you need to move this request for playerInput to immediately after your current line 46.
This might just about get the game to run, but it won't be correct because I could, for example, put the letter 'A' in 5 times and, because it was just one of the correct letters, it will accumulate my correctCount. You need a check that this letter hasn't already been chosen (e.g. by checking the current state in alphabet).
There are a huge number of problems with this program and I doubt that either myself or anyone else will want to spend much time on it unless you make it more readable (as your classmate suggested). Here are just a few suggestions:
- most compilers will need #include <cstdlib> in order to use system() - add this;
- fix the constants that are set incorrectly; leftArm and leftLeg are NOT using escape characters: think about the difference between / and \
- don't assign integers to character variables (playerInput, yesOrNo) - they don't need initialising anyway
- whilst debugging, remove anything to do with drawing the gallows - it just makes your program unreadable; you can put this back once all the rest of the logic is correct; you only need the accumulated error and correct counts whilst you are getting the program logic correct;
- whilst debugging, please don't clear the screen - you need to see what is going on
- whilst debugging, print out some intermediate variables (especially errorCount and correctCount)
- don't put spaces in HIDDEN_WORD or alphabet - it makes it really difficult to count positions;
- consider using toupper() to avoid having to consider both 'b' and 'B' for example
Make the first suggested change (moving your playerInput prompt into the required while loop) and fix any compiler errors/warnings. I'm afraid I (and probably most) cannot afford the time to advise any further on this until you make this program vastly more readable. It also tends to put me off if you imply that your teacher or classmates are unhelpful.
Line 12: Truncation of character constant left_arm and left_leg. '//' is two bytes. You're trying to store that into a single character.
As previously pointed out, your game is coded to work with a single hidden word. If you change the hidden word, you have recode the program. Not a good design.
The sense of willYouPlay seems backwards.
Lose the spaces in HIDDEN_WORD and alphabet.
You draw the gallows in multiple places. Consider doing this in once place. i.e. a function.