Good old hangman

Hello and thank you for your time to possibly help me with this. I have scoured the internet and books and forums but this version of Hangman seems to be different than the others. I am currently having an issue with when they correctly guess a letter I'm supposed to have it output something like "Congrats you guessed the letter in spot x and x" for if it's in two spots. For example "Hello" and they guess 'l'. I was given pseudocode to work with and I don't know if I've done something wrong or forgotten something. The error that arises is "expression must have integral or unscoped enum type" for the " correct" section in output. I'm hoping someone could help steer me in the right direction.

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

int main()
{
    bool validLetterGuess = true;
    string word;
    string guess1;
    string guess2;
    string dashes(word.size(), '_');
    int guessremain = 0;

    cout << "Welcome to hangman!\n";
    cout << "Player one will enter a word, player two will have 5 letter and 5 word guesses.\n";
    '\n';
    cout << "Player one, would you please enter your word:";
    cin >> word;
    system("CLS");
    for (int i = 0; i < word.length(); i++)
    {
        dashes += "-";
    }
    cout << dashes;
    cout << "\n";

    while (guessremain < 5)
    {
        cout << "Player two, make your letter guess: ";
        cin >> guess1;
        while (guess1.length() != 1)
        {
            cout << "I'm sorry, you must enter a letter.\n";
            cout << "Player two, make your letter guess: ";
            cin >> guess1;
        }
        bool multipleLetters = false;
        string output = "";
        for (int i = 0; i < word.length(); i++)
        {
            if (guess1.at(0) == word.length())
            {
                if (multipleLetters == false)
                {
                    output = "You got position" + i + " correct";
                    multipleLetters = true;
                }
                else
                {
                    output += " and ";
                    output += i;
                }
            }
        }

        
    }
}
Line 45/51: You cannot at a numeric value to the string. Use std::to_string(i) instead.

Line 41: That expression doesn't make sense. You want this: if (word[i] == guess1.at(0))
output = "You got position" + i + " correct";
you can't do addition with C strings which is what literal strings are. Add S to them and you can.
https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s
or:
output = "You got position"s + to_string(i) + " correct"s;

you can add a c-string to a c++ string, though. I don't actually know about the left side, but I think to_string() + "" is ok as it adds to an existing string after that change. But I don't think ""+string works, ... you can try it, if it does not like it try with the s...
Last edited on
I figure you can't do addition, this is the code he gave me to work from:
Where I would start is by concatenating to some kind of string. You also need to know how many letters were guessed correctly in that loop. So, you would need to have some kind of if statement checking to see if the user has gotten two letters correct on one guess.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool multipleLetters = false;
string output = "";
for(all letters in the word)
{
       if(letter == letter in the word)
        {
               if(multipleLetters == false) 
               {
                     output = "You got psition " + i + " correct";
                      multipleLetters = true;
               }
               else
                {
                      output += " and ";
                       output += i;
                }
        }
}


This whole assignment is driving me insane because I want to figure it on my own, but haven't been able to since he's so picky as to what we use since we are beginners. I can't even use toupper or tolower for this >.<
So like, to.string we haven't covered, can't use that. Same with c.string.

This is what the result I supposed to come to, couldn't see a upload image link so had to upload it myself. https://ibb.co/72y6TWp
you don't HAVE to do anything any particular way.
a little extra processing can give you a simple way out.
step one, count the number of correct hits:
for all the letters in the word
count += (letter == guessletter); //false adds zero, true adds 1
also check here for a win, maybe? somewhere you have to do that.
if !count //you don't have any matches, let user know they didn't get any hits message
cout failmsg etc
else if count ==1
{
find position of the match and
cout single message //letters should become letter in the output, mostly
}
else //you have more than 1 match
cout "you guessed the letters as positions " //letters and positions are both plural here
for(int i = 0; i<count; i++)
{
find match
cout << position;
if (i!= count-2) cout <<", ";
}

yea, its ugly. but a little juggling the logic and knowledge gets it done with limited toolset. It is totally possible to do all this in a single pass over the string, rather than the pre-count, but the logic is more complex. If you are feeling up to it, you can give that a try, but the first thing in software development is to get it working, then you can make it better.

as for other foolishness... you can write your own toupper or tolower ...
if (letter>='a' && letter <='z') letter += constant; //there is a constant offset value you can use. it may be a negative number or subtraction, look at an ascii table to find it. that is toupper, tolower is similar

i avoided tostring by just having cout print the integer -- it knows what to do.
Last edited on
Topic archived. No new replies allowed.