Having some troubles cleaning things up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>


using namespace std;

int main()
{
	char word[20];		//holds word
	char star[20];		//holds "*"'s for unsolved letters
	int count = 0;	    //Variable for a count
	char guess;			// char value for entering a guess
	int tries;			//variable for storing number of tries to be guessed.
	int triesLeft;		//For displaying the number of guesses left in the puzzle
	int x;

	cout << "Enter a word to be guessed" << endl;
	cin.getline(word, 20);
	int puzzLength = strlen(word);		//finds length of puzzle, stores INT value to puzzlength
	
	cout << "Enter the number of tries" << endl; // Sets the number of guesses in the puzzle
	cin >> tries;

	triesLeft = tries; // Sets triesLeft to the correct number of available tries

		
	
	strcpy_s(star, word);				//copy word to the 'star' array
	
	for (count = 0; count < puzzLength; count++) //converts characters to *'s to represent stars
	{		

		if (isalnum(word[count])) star[count] = '*';
		else star[count] = word[count];
		
	}	

	
	
	
		
		
			for(x = 0; x <= tries; x++)
			{
				
				{
				cout << "The word so far: " << star << "." << endl;
				cout << "Enter a guess." << endl;
				cin >> guess;
				
			
				triesLeft = tries - x;
	

				}

				//For loop for checking if the guess was correct.
				for (count = 0; count <= puzzLength; count++)
				{
	
					if (guess == word[count]) 
					{ 
						star[count] = guess;		//Converts the "star" to the correct answer
						cout << "Correct! You have " << triesLeft << " Tries left" << endl;
					}
					else 
						cout << "Wrong! you have " << triesLeft << " Tries left" << endl;
	
				}		
			}		
		
		if (strcmp (word, star) != 0)
			cout << "You Win!" << endl;
		
		else 
			cout << "You Lose!" << endl;
	
	cin.get();
	return 0;
}


That is my code, and for the most part it works. What im having trouble cleaning up, is:
1) Program does not say "You Win" if you guess everything correctly, Ive tried putting if\else statements at the top and bottom of my for loop (which i have there now), ive tried setting a while loop using strcmp, but the program just cant seem to tell whether or not the answer is correct, display the correct message, and exit.

2) When the program checks an answer it checks the entire array (which is good! but..) it gives out "wrong answer" for every letter not guessed correctly, I.E. if the word was "Bunny" and a "B" was entered, output would look like
Correct! you have 4 tries left!
Wrong! you have 4 tries left!
Wrong! you have 4 tries left!
Wrong! you have 4 tries left!
Wrong! you have 4 tries left!

Otherwise, it checks the answer fine, but I havent figured out how to clean up the massive amounts of spam coming out that however.


Any help with fixing those two issues would be greatly appreciated!


(edited to clean up my source code to make it readable)
Last edited on
Code tags please,can´t read the code...have you tried flushing the input stream?
Check line 57,you should check the word first and then give messages to the user one at a time,you should also check your for structures,they are upside down...and you are not diminishing the triesLeft variable when you should.At the final part,you should use strncmp instead of strcmp.BTW,i don't know if this is for an assignment or for fun but its easier to use the c++ string class for this type of programs,you should try it.Cheers.
Last edited on
Thanks mike!

To clarify, this is for an assignment.

I tried changing strcmp to strncmp, and got "error C2661: 'strncmp' : no overloaded function takes 2 arguments" as an error, (do i need to do something else along with changing to strncmp?)

As for my for structures, not sure what you mean by "upside down" do you mean i should check for the correct guess before i start counting the tries? i.e. theoretically swap lines 57 and 42?
use strncmp with the puzzlLength as third argument,because strcmp will compare the whole arrays,even if the two words are equal the rest of each array will have garbage because you didn´t initialize them,and that garbage will be different for both word and star.Anyways,you should always use strncmp and strncpy even if you are sure you are doing the right thing,it is a better practice.With the for structures i mean upside-down in the way that you have the right thing in your head but its upside-down in your code,specially the one starting on line 57.With the triesLeft variable,it would be better for you to just write --triesLeft when required,and you should only do it when the user is wrong.I think you are doing it during each cycle which is a logical error.Cheers.
Last edited on
Thanks again!

I understand what you mean about the triesLeft thing, it *should* only be when the user is wrong, but in the sample output given for the assignment, its supposed to be there on every attempt made, right or wrong. I'll play around a bit with the strncmp and see if i cant make that work, again thanks for the help!
What i meant is that you forgot to write a escape mechanism when the user clears the whole word..you are just using counters for the whole thing..check line 51.And yes,the final part problem is your use of strcmp,i already tested your program after fixing it and works fine.Strncmp takes three arguments,the third being the number of characters to copy,which has to be puzzlLength in your program.Cheers.
Topic archived. No new replies allowed.