Word guessing game

Hello, I am taking my first semester of comp. sci and so far it's been fairly easy. Unfortunately, we reached vectors and 2-d arrays and while the concepts of them are not hard, the assignment I was given is kicking my butt. No I am not looking for someone to do my homework, I am looking for tips on how to start the problem. The assignment is to have the user guess letters and when they have a letter correct replace the '*' (say the word I chose is cow so the user will see *** because it has 3 letters) with the letter they just input.(so say they guess 'O' now on their next turn it will look like this *o*) In a way it's like hangman because they have limited tries to get all the letters in the word. My instructor is telling us to use vectors. And to have our correct answer(a word of our choosing for the user to guess) be in an array. The instructor wants us to have a function that displays the vector (solved that part) and a function that takes the user input and compares it to the correct answer. If the user input is a letter that is in the word they are trying to guess then replace the * with the letter.

I hope that all makes sense. The following is some of my code and I am simply stuck with how to proceed. I do not know how to take in someone's input and then compare it to the correct word, and then replace those '*' with the letter the user put in.


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
#include<iostream>
#include<vector>

using namespace std;

int main()
{
	vector <char> v1(5);
	vector <char> v2(5);

	v1[0] = 's';
	v1[1] = 'l';   //the word I am having the user guess is "sleep"
	v1[2] = 'e';
	v1[3] = 'e';
	v1[4] = 'p';


	v2[0] = '*';
	v2[1] = '*';
	v2[2] = '*';
	v2[3] = '*';
	v2[4] = '*';
	

	//const int size = 5;
	//char answer[size];     //this is supposed to be the array that holds sleep but i am not even sure what to do with this

	int guesses = 10;
	char guess;
	int revealed = 0;
	
	cout << "guess the word!" << endl;

	for (int i = 0; i < 10; i++) //i want the user to have 10 tries
	{
		cout << "You have " << guesses << " left" << endl;
		displayV2(v2);
		checkGuess() //still working on this
	}

	return 0;
}

void displayV1(vector <char> v1)
{
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << endl;
	}
}

void displayV2(vector <char> v2)
{
	for (int i = 0; i < v2.size(); i++)
	{
		cout << v2[i] << endl;
	}
}

vector <char> checkGuess(vector <char> vector)
{
	char guess;
	cin >> guess;   //so here is where I dont know how to take someone's input and then compare it to the right answer, which is sleep. 

	vector.push_back(guess);

	return vector;
}


I know this is a mess but I am really struggling here any help would be greatly appreciated. Thank you
Last edited on
Hello sankarl,

Some hints to get you started:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

std::vector<char> v1{ 's', 'l', 'e', 'e', 'p' }; does the same as what you have, but will eliminate ten lines of code because you can do the same for "v2" by replacing the letters with *s.

These two lines
1
2
//const int size = 5;
//char answer[size]; 

The first line I would write as constexpr int MAXSIZE{ 5 };, you could also just use "const" if needed, and put it at the top of "main" where it is easy to find and change when needed. The capital letters help to remind you that it is defined as a constant.

Line 2 would then be char answer[MAXSIZE];.

In the for loop the "cout" statement will always print "guesses" as 10 because "guesses" never changes. Consider the for loop as: for (int i = 0; i < 10; i++, guesses--)

In function "checkGuess(vector <char> vector)" you have a good start until you reach vector.push_back(guess);. All this does is add to the end of the vector. What you need to do is check "v1" to find which element of the vector matches the user guess and then change that same position in "v2" replacing the * with the letter guessed.

If you pass the vector by reference there will be no need to return the vector back to "main". Actually back in main you do not even receive the returned value.

For someone new to coding I do not see this as a mess, but more as uninformed, inexperienced or lack of good teaching. Stick around here for awhile and read as much as you can and you will pick on some good coding and good habits.

Now having looked at your code I see no need for an array, the two line you have commented out, because the vectors are a better choice and you can work with the same as you would an array. So unless it is required I would forget about the array. If it is required a little more information on the way it would be used would help.

I will load your code up and see if I find anything else that I may have missed just looking at the code.

Hope that helps,

Andy
Hey Andy,

thank you very much for responding to my post. I did figure it out eventually after breaking it into small problems and attempting to solve those problems. Just came back to reply since I forgot to reply earlier when I posted this. Thanks again for the response!
Topic archived. No new replies allowed.