Hangman, Lowercase Input for Uppercase letter is invalid

Almost done with the code for my hangman assignment, I just need a bit of help (or at least a huge clue) on how to count a lowercase input as correct. Let's say the phrase to guess is "Please Kill Me Now", if the player inputs a lowercase for 'P', 'K', 'M', and 'N', it is invalid. Otherwise, if you input Uppercase for the following letters, it's correct.

Here is my code:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <string>
using namespace std;

// Set unsolved function
string setUnsolved (string phrase);
string updateUnsolved (string phrase, string unsolved, char guess);
char getGuess (string prevGuesses);
void clearScreen();

int main ()
{
    string phrase;
    string all_Guessed;
    string unsolved;
    string guess;

    char currentguess;
    int chances = 7;    // number of chances to guess the phrase

    // Prompt player to enter a phrase
    cout << "Enter phrase: ";
    cout << endl;
    getline(cin, phrase);

    // Clears screen to initiate hangman game
    clearScreen ();

    unsolved = setUnsolved(phrase);
    cout << "Phrase: " << unsolved << endl;

    do
    {
        currentguess = getGuess(all_Guessed);

        all_Guessed += currentguess;

        if (phrase.find(currentguess) > phrase.size())
        {
            chances--;
        }

        clearScreen();

            unsolved = updateUnsolved(phrase, unsolved, currentguess);

            cout << unsolved << endl;

            //cout << "Guesses so far: " << all_Guessed << endl;


            if (phrase.find(currentguess) > phrase.size())
            {

                cout << chances << " incorrect guesses left" << endl;
            }

    }
    while (chances > 0 && unsolved.find('-') != string::npos);
    // WIN LOSE CONDITION

    if (chances <= 0)
    {
        cout << "You lost!";
    }
    else if (unsolved.find('-') == string::npos)
    {
        cout << "Congratulations!! You won!";
    }
    cout << endl;
    return (0);

    //end of int main()

}


string setUnsolved(string phrase)

{
    for (int i = 0; i < phrase.length(); i++)
    {
        // Check if letter or space or exclamation point
        if (phrase.at(i) != ' ' && phrase.at(i) != '!')
        {
            phrase.at(i) = '-';
        }
    }

    return(phrase);
}

string updateUnsolved(string phrase, string unsolved, char guess)
{

    int chances = 7;

    if (phrase.find(guess) != string::npos)
    {

        //loop through all characters and replace '-'
        for (unsigned int i = 0; i < unsolved.length(); i++)
        {

                if (phrase.at(i) == guess)
            {

                unsolved.at(i) = guess;
            }
        }
    }

    return(unsolved);
}

char getGuess(string prevGuesses)
{
   string all_Guessed;
   char guess;
   cout << "Enter a guess: ";
   cout << endl;
   cin >> guess;
   while (isalpha(guess) == false || prevGuesses.find(guess)
          < prevGuesses.size())
   {
       cout << "Enter a guess: " << endl;
       cin >> guess;
   }

   all_Guessed += guess;
   return (guess);
}

void clearScreen()
{
   system ("CLS");
}
Last edited on
Remember that C++ is case sensitive, 'C' != 'c'. For this program you may want to consider always comparing against either upper or lower case letters. Look up the documentation for either the toupper() or tolower() functions.


Yeah, I just looked into that, in which it seemed like I had hope, but after that it's now having a problem for guessing one's with the lowercase characters. For example, if the phrase is "Food"; 'f' would now be valid, but I have to enter 'o' and 'd' a couple of times until it's correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

char getGuess(string prevGuesses)
{
   string all_Guessed;
   char guess;
   cout << "Enter a guess: ";
   cout << endl;
   cin >> guess;

   guess = toupper(guess);

   while (isalpha(guess) == false || prevGuesses.find(guess)
          < prevGuesses.size())
   {
       cout << "Enter a guess: " << endl;
       cin >> guess;
   }

   all_Guessed += guess;
   return (guess);
}
 


You also need to change guess inside the loop after you ask for another character.

What do I exchange it with?
Your program works pretty well, I just modified it a little so that uppercase letters get the same treatment as lowercase letters.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <string>
using namespace std;

// Set unsolved function
string setUnsolved (string phrase);
string updateUnsolved (string phrase, string unsolved, char guess);
char getGuess (string prevGuesses);
void clearScreen();

int main ()
{
    string phrase;
    string all_Guessed;
    string unsolved;
    string guess;

    char currentguess;
    int chances = 7;    // number of chances to guess the phrase

    // Prompt player to enter a phrase
    cout << "Enter phrase: ";
    cout << endl;
    getline(cin, phrase);

	string phrase2 = phrase; // Make a copy
	
	int i;
	for(i = 0; i < phrase2.size(); i++) phrase2[i] = toupper(phrase2[i]);

    // Clears screen to initiate hangman game
    clearScreen ();

    unsolved = setUnsolved(phrase);
    cout << "Phrase: " << unsolved << endl;

    do
    {
        currentguess = getGuess(all_Guessed);

        all_Guessed += currentguess;

        if (phrase.find(currentguess) == std::string::npos)
        {
            chances--;
        }

        clearScreen();

            unsolved = updateUnsolved(phrase, unsolved, currentguess);

            cout << unsolved << endl;

            //cout << "Guesses so far: " << all_Guessed << endl;

            if (phrase2.find(toupper(currentguess)) > phrase.size())
            {
                cout << chances << " incorrect guesses left" << endl;
            }

    }
    while (chances > 0 && unsolved.find('-') != string::npos);
    // WIN LOSE CONDITION

    if (chances <= 0)
    {
        cout << "You lost!";
    }
    else if (unsolved.find('-') == string::npos)
    {
        cout << "Congratulations!! You won!";
    }
    cout << endl;
    return (0);

    //end of int main()

}


string setUnsolved(string phrase)
{
    for (int i = 0; i < phrase.length(); i++)
    {
        // Check if letter or space or exclamation point
        if (phrase.at(i) != ' ' && phrase.at(i) != '!')
        {
            phrase.at(i) = '-';
        }
    }

    return(phrase);
}

string updateUnsolved(string phrase, string unsolved, char guess)
{
    int chances = 7;

	string phrase2 = phrase; // Make a copy
	
	int i;
	for(i = 0; i < phrase2.size(); i++) phrase2[i] = toupper(phrase2[i]);

    if (phrase2.find(toupper(guess)) != string::npos)
    {

        //loop through all characters and replace '-'
        for (unsigned int i = 0; i < unsolved.length(); i++)
        {

                if (toupper(phrase.at(i)) == toupper(guess))
            {

                unsolved.at(i) = guess;
            }
        }
    }

    return(unsolved);
}

char getGuess(string prevGuesses)
{
   string all_Guessed;
   char guess;
   cout << "Enter a guess: ";
   cout << endl;
   cin >> guess;
   while (isalpha(guess) == false || prevGuesses.find(guess)
          < prevGuesses.size())
   {
       cout << "Enter a guess: " << endl;
       cin >> guess;
   }

   all_Guessed += guess;
   return (guess);
}

void clearScreen()
{
   system ("CLS");
}
Hi! Thanks so much helping me edit the code, it worked really well, but how can I output it as capital letters? When I entered 'p' for 'P' with the modified code, it only outputs it to to lowercase letter.

Let's say the phrase is "Laptop", 'l' is now a valid input, but it'll output the phrase as all lowercase instead of having the uppercase for 'L'.
Last edited on
You need to convert the input to uppercase.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <string>
using namespace std;

// Set unsolved function
string setUnsolved (string phrase);
string updateUnsolved (string phrase, string unsolved, char guess);
char getGuess (string prevGuesses);
void clearScreen();

int main ()
{
    string phrase;
    string all_Guessed;
    string unsolved;
    string guess;

    char currentguess;
    int chances = 7;    // number of chances to guess the phrase

    // Prompt player to enter a phrase
    cout << "Enter phrase: ";
    cout << endl;
    getline(cin, phrase);

	string phrase2 = phrase; // Make a copy
	
	int i;
	for(i = 0; i < phrase2.size(); i++) phrase2[i] = toupper(phrase2[i]);

	phrase = phrase2;

    // Clears screen to initiate hangman game
    clearScreen ();

    unsolved = setUnsolved(phrase);
    cout << "Phrase: " << unsolved << endl;

    do
    {
        currentguess = getGuess(all_Guessed);

        all_Guessed += currentguess;

        if (phrase.find(currentguess) == std::string::npos)
        {
            chances--;
        }

        clearScreen();

            unsolved = updateUnsolved(phrase, unsolved, currentguess);

            cout << unsolved << endl;

            //cout << "Guesses so far: " << all_Guessed << endl;

            if (phrase2.find(toupper(currentguess)) > phrase.size())
            {
                cout << chances << " incorrect guesses left" << endl;
            }

    }
    while (chances > 0 && unsolved.find('-') != string::npos);
    // WIN LOSE CONDITION

    if (chances <= 0)
    {
        cout << "You lost!";
    }
    else if (unsolved.find('-') == string::npos)
    {
        cout << "Congratulations!! You won!";
    }
    cout << endl;
    return (0);

    //end of int main()

}


string setUnsolved(string phrase)
{
    for (int i = 0; i < phrase.length(); i++)
    {
        // Check if letter or space or exclamation point
        if (phrase.at(i) != ' ' && phrase.at(i) != '!')
        {
            phrase.at(i) = '-';
        }
    }

    return(phrase);
}

string updateUnsolved(string phrase, string unsolved, char guess)
{
    int chances = 7;

	string phrase2 = phrase; // Make a copy
	
	int i;
	for(i = 0; i < phrase2.size(); i++) phrase2[i] = toupper(phrase2[i]);

    if (phrase2.find(toupper(guess)) != string::npos)
    {

        //loop through all characters and replace '-'
        for (unsigned int i = 0; i < unsolved.length(); i++)
        {

                if (toupper(phrase.at(i)) == toupper(guess))
            {

                unsolved.at(i) = guess;
            }
        }
    }

    return(unsolved);
}

char getGuess(string prevGuesses)
{
   string all_Guessed;
   char guess;
   cout << "Enter a guess: ";
   cout << endl;
   cin >> guess;
   while (isalpha(guess) == false || prevGuesses.find(guess)
          < prevGuesses.size())
   {
       cout << "Enter a guess: " << endl;
       cin >> guess;
   }

   guess = toupper(guess);

   all_Guessed += guess;
   return (guess);
}

void clearScreen()
{
   system ("CLS");
}
Topic archived. No new replies allowed.