hangman help

Hey so I've been fighting with this for a few days now. I'm trying to create a simple hangman game. The problem I'm having is understanding how to use the str::compare func.

1
2
3
4
5
6
7
8
  Char answer[6];//I have tried to use just string answer; too
  String a;
  Cin >>answer;
  Cin >> a;//for the first letter guess.
  If(answer.compare(0,'a') == 0){
     Cout << "you got it!!";
       }else if(answer.compare(0,'a') != 0){
         Cout << "try again";


My question is, how do I use the compare function to compare only certain characters against a different string



I apologize for all the capitalizations, I've typed all this out from an android phone and it capatilizes some things. My code right now works, but it freezes after I give a letter. This is just a snippet of the full program. So it looks a little bare too. The rest is all simple cout intro statements with the rules and such.
Last edited on
I've been using the reference pages this whole time. I don't understand how to implement it into the coding though and I was wondering if I could just have an example that is more simplified, and similar to mine. Yet still makes me work for it a bit. I know that's asking a lot. At least with what I understand of programming. After almost an hour of playing around and trying I'm still lost. I have tried to use temp strings to store answer in a const char such ad:

1
2
   Const string answer;
   String realAnswer = answer;


And I've been trying to figure it out. If you stored the world 'Hello' then your first letter as an H in string a then it should be

Compare(0,1,'a') ==0)


Correct?
Last edited on
string comparison works like this
1
2
3
4
5
6
7
8
string answer = "Hello";

// compare the whole string
if (answer == "Hello")

// compare a single character

if (answer[0] == 'H')
So I have:
1
2
3
4
5
6
7
8
9
  String answer;
  String a;
    cin>>answer;
    cin>>a;
  If(answer[0]=='a'){
     Cout << "got it";
     } else if(answer[0]!='a'){
        Cout<<"nope";
}


And my program runs, but it runs the else if statement every time.
Last edited on
When you input 'a' is it small 'a' or capital 'A'?

anyways for a single character you don't need a string, a char would be equally helpful
I've tried small and capital. And okay, I'll play around with making 'a' a char instead
Topic archived. No new replies allowed.