Word guessing program and C strings.

#include <iostream>
using namespace std;
int main()
{
char word[10];
char letter;
char newword[nl];
char wrong[nw];
char correct[nc];
int nl = 0;
int nw = 0;
int nc = 0;
cout << "Enter a word." << endl;
cin >> word;
for(int i = 0; i<10; i++)
if (word[i] != newword[i]




cout << "Guess a letter." << endl;
cin >> letter;
for (int i = nw; i<6; i++)
if (letter != word[10])



return 0;
I am really stuck on this. I making a program to tell the number of letters in an inputted word and keep track of wrong and previously guessed correct letters. Right now I am trying to figure out how to insert letters from one array to another. Like, if a wrong letter is inputted how would I put it into the "wrong" array? Also how would I specify that if it is not any letter in the array, then it is wrong?
Last edited on
Tried to take different approach with boolean arrays. It's incomplete and I don't think the output is working how I want it to.
#include <iostream>

using namespace std;

int main()
{
char input;
char right[10];
bool wrong [27] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0'};
int right_guess = 0;
int wrong_guess = 0;
int guesses_left;
wrong == false;
guesses_left = 6 - wrong_guess;
cout << "Enter a word." << endl;
cin >> right;
cout << "Guess a letter" << endl;
cin >> input;
if (right [input]==)

if (wrong[input - 'a']==true)
{
cout << "Already guessed, try again." << endl;
}
else if (wrong[input - 'a'] == false)
{wrong [input - 'a']=true;
wrong_guess++;
cout << "Nope! Guess another letter. " << guesses_left << " guesses left." << endl;


return 0;
}
}
You can take a look at this. I basically edited your code to actually work but I did it quickly and didn't document it or test it thoroughly. I also didn't use any functions because it looks like you haven't learned them yet but I used a pointer.

I still don't understand why you would want them to enter a word and then guess it's letters. It's useless since they gave you the actual word. A more clever approach would be to give them a list of words and then randomly choose one for them to guess.

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
int main()
{
	char letter;
	string word;
	int size = 0;	
	int guesses = 0;
	int num_correct = 0;	
	int num_incorrect = 0;
	bool guessed = false;
	bool* correct_guesses;

	cout << "Enter a word." << endl;
	cin >> word;
	
	size = word.size(); 
	
	guesses = size; 
	correct_guesses = new bool[size];	
	
	// set all values to false
	memset(correct_guesses, false, sizeof correct_guesses);	
	
	while (guesses > 0)
	{
		cout << "Guess a letter" << endl;
		cin >> letter;
		guesses--;
		guessed = false;	

		for (int i = 0; i < size && !guessed; i++)
		{
			if (tolower(letter) == tolower(word[i]))
			{	
				if (correct_guesses[i] == true)
				{
					guessed = true;	
					cout << "Already guessed, try again." 
                                               << guesses << " guesses left." << endl;
				}				
				else
				{
					num_correct++;
					guessed = true;
					correct_guesses[i] = true;
					cout << "You guessed it! Nice job." 
                                               << guesses << " guesses left." << endl;
				}
			}
		}			
		if (guessed == false)
		{
			num_incorrect++;
			cout << "Nope! Guess another letter. " 
                               << guesses << " guesses left." << endl;
		}	
	}

	cout << "You had " << num_correct << " correct guesses and " 
               << num_incorrect << " incorrect guesses." << endl;
	
	// cleanup dynamic data
	delete[] correct_guesses;	

	return 0;
}
I just worked on it some more. Now I need to figure how to reset match as false every time a letter is guessed.
Oops wrong code. Here's the right one.
#include <iostream>

using namespace std;
int numbletter(char word[], int size);
int main()
{
char input;
bool match=false;
char word[11];
bool right[10];
char alph [27] = "abcdefghijklmnopqrstuvwxyz";
bool wrong [26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int right_guess = 0;
int wrong_guess = 0;
int guesses_left;
int letters_left;
int correct_letters;
guesses_left = 6 - wrong_guess;
letters_left = 0;
cout << "Enter a word." << endl;
cin >> word;
while (wrong_guess<6 && correct_letters<6)
{
cout << "Guess a letter" << endl;
cin >> input;

for (int i=0; i<11; i++)
{
if (input ==word[i]){
correct_letters++;
right[i]=true;
match=true;
}
}
if (match==false)
{
for (int i =0; i<27; i++)
{
if (input == alph[i])
wrong[i] = true;

if (wrong[i]==true)
{
cout << "Already guessed, try again." << endl;
}
else if (wrong[i] == false)
{wrong [i]=true;
wrong_guess++;
cout << "Nope! Guess another letter. " << guesses_left << " guesses left." << endl;
}

}
}
}
return 0;
}


int numbletter(char word[], int size)
{
int num_letters = 0;
for (int i = 0; i<size; i++)
{if (word[i] == '\0')
{
i=size;
}
else
{
num_letters++;
}
}
return num_letters;
}

Last edited on
I would take a look at my sample code and adopt some of the simpler techniques.


Your logic has a lot of flaws. For example the condition:

while (wrong_guess<6 && correct_letters<6)


this is not going to do anything useful because of the statements above it:

1
2
3
int wrong_guess = 0;
int correct_letters;
guesses_left = 6 - wrong_guess;


You are setting guesses_left to 6 so it will not be less than 6 initially and you didn't even initialize correct_letters so the compiler will contain whatever in was in memory last.
Topic archived. No new replies allowed.