C++ Word Guess game almost done, few problems ?

I'm making a Word Guess game.
RULES:
-Player1 will enter a word of any length for Player 2 to guess. [DONE]
-Player 2 will have 5 letter guesses and 5 word guesses total. [DONE]
-Ignore case; ex. If user enter 'T' display it as lower (without using toupper/tolower)[STUCK]
-If user guesses correct letter, replace dash(es) with letter, and what position; ex: "testing" and Player2 enters 'T' then output should be "You guessed the letters at 1 and 4 correct letter guess correct. You have 4 letter guesses remaining. Enter your first word guess".

PROBLEMS:
-Program doesn't ignore case [need to fix]
-If the letterguess is correct, or incorrect, count it as 1 letter guess and have them attempt their first word guess. DO not worry about a user guessing letters multiple times, just count it as a used guess.
-If I run the word: "testing" and I enter "T" it should display "You guessed the correct letter at 1 and 4. Enter word guess" / instead if I enter 'T' it displays "Wrong letter".... if I enter 't' it only reads the first t and not the fourth.

Any help appreciated. I am so close to being done, just stumped.



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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    string word, guessword, guessletter;
    string dashes = string(word.length(), '_');
    int numletterguess = 5;
    int numwordguess = 5;
    bool gameover = false;
    bool validletterguess;
    bool validwordguess;

    cout << "Player 1. Enter a word for Player 2 to guess: " << endl;
    getline(cin, word);

    dashes = string(word.length(), '_');

    while (gameover == false && numletterguess > 0 && numwordguess > 0) // start game Round 1. 
    {
        guessletter = "";
        cout << "Player 2, you have " << numletterguess << " letter guesses left and " << numwordguess << " word guesses left. " << endl;
        cout << "Enter your letter guess: " << endl;
        cin >> guessletter; //letter guess

        while (guessletter.length() != 1)  // if the letter guess is more than a single letter, loop until user enters 1 letter.
        {
            cout << "Enter only 1 Letter. " << endl;
            cout << "Try again: " << endl;
            cin >> guessletter;

        }
        numletterguess--;
        validletterguess = false;
        for (int i = 0; i < word.length(); i++) //ignore case
        {
            if (guessletter.at(0) == word.at(i))
            {
                cout << "You guessed a letter correct at position " << i + 1 << " of the word" << endl;
                dashes.at(i) = word.at(i);
                cout << dashes << endl;
                validletterguess = true;
                break;
            }
        }
        if (validletterguess == true) {
            continue;
        }
        else {
            cout << "Wrong letter guessed" << endl;
            cout << "Player 2, you have " << numletterguess << " letter guesses left" << endl;
            cout << "Enter your wordguess" << endl;
            cin >> guessword;
            if (guessword == word) {
                gameover = true;
                break;
            }
            else {
                numwordguess--;
                cout << "You guessed the wrong word!" << endl;
                cout << "You have " << numwordguess << " guess left" << endl;
                continue;
            }
        }
    }
    if (gameover == true)
    {
        cout << "You have beaten the game!" << endl;
    }
    else cout << "Game Over!" << endl;

}
the difference between 'a' and 'A' is constant. a char is just an integer. you can do your own toupper type function (or, just inline if you only need it in a place or two).

for multiple letters, you need to flip them all in a loop ... for all the letters in the word, if it matches, replace the dash with the letter.

Last edited on
@jonnin instead of using a char for letter guess, I'm using a string. I couldn't figure out how to make char = word.length().

would you mind elaborating a bit more?

1. I have a function to ignore case, but it is not working, and I don't know how to go about fixing it.
2. Even if I dont guess uppercase, and I guess a lowercase 't' it will only read the first instance, and I cannot figure out how to make it check for every instance of the letter.
3. I have to add word guesses, and I cant get that to work either.

I'm not just trying to beg for answers, but I've been working on this for over a week, and my time is almost up.
1
2
3
for(int i = 0; i < somestring.length(); i++)
  if(somestring[i] == guess)
     dashstring[i] = guess;



if(guess == somestring[i] || guess+('a'-'A') == somestring[i]) //case ignored


and
if(wordguess == somestring)
//winner, they guessed right!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;
bool checkWord(string word, string guess) // check word and guess
{
    int counter = 0;
    for (int i = 0; i < word.length() && i < guess.length(); i++) // ignore case
    {
        if (tolower(guess[i]) == tolower(word[i]))
            counter++;
    }
    return counter == word.length();


using tolower is the only way I can get it to work.. however thats the only thing I cant use.
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
#include <iostream>
#include <string>

bool is_uper(char ch);
char to_uper(char ch);

int main()
{
    std::string word{"Testing"};
    
    
    size_t no_left = word.length();
    std::string current_attempt = std::string( no_left, '-');
    std::cout << word << ' ' << current_attempt << '\n';
    char trial_char;
    
    
    while(no_left > 0)
    {
        std::cout << "Enter character: ";
        std::cin >> trial_char;
        
        for(size_t i = 0; i < word.length(); i++)
        {
            if( to_uper(word[i]) == to_uper(trial_char) && (current_attempt[i] == '-') )
            {
                no_left--;
                if( is_uper(word[i]) )
                    current_attempt[i] = to_uper(trial_char);
                else
                    current_attempt[i] = trial_char;
            }
        }
        std::cout << current_attempt << '\n';
    }
    
    return 0;
}

bool is_uper(char ch)
{
    if( ch <= 'Z')
        return true;
    else
        return false;
}

char to_uper(char ch)
{
    if( is_uper(ch) )
        return ch;
    else
        return ch - 'a' + 'A';
}


Testing -------
Enter character: g
------g
Enter character: s
--s---g
Enter character: i
--s-i-g
Enter character: t
T-sti-g
Enter character: n
T-sting
Enter character: e
Testing
Program ended with exit code: 0
Last edited on
using tolower is the only way I can get it to work.. however thats the only thing I cant use.


Nothing stopping you writing your own version!
Topic archived. No new replies allowed.