Hello there! This is my first post so I'd like to say hello to everyone on the forums!

Anyway, I am coding a hangman game. I read my words from a text file with ifstream, then I randomize a word and I set the string "sSelectedWord" to the randomized word.

After that I get user input

cin >> sGuess

and I set a for loop up to check if sGuess is the same as any letter in sSelectedWord

sGuess == sSelectedWord[i]

Where i is the sSelectedWord.length

But I have no idea how to compare the entered "sGuess" with "sSelectedWord"

Hopefully it makes enough sense for anyone to understand and be able to help.

In a nutshell the question would be "How do I compare "cin >> sGuess" with "sSelectedWord""

I remember doing this somehow but I don't remember how and I lost the code...

EDIT: Dave Sinkula told me to just post the code so:

void mainSPGameFunc() {

	readFileGetRandom();
	string sSelectedWord = readFileGetRandom(); 
	string sGuess;

	int iWordLength = sSelectedWord.length();
		
	cout << "Guess the word!\n";
	for(int i = 0; i < iWordLength; i++) {
		cout << "_ ";
	}
	
	cin >> sGuess;

	[B]for(int i = 0; i < iWordLength; i++) {
		if(sGuess == sSelectedWord[i]) {
			cout << "RIGHT!";
		}
		system("PAUSE");
	}[/B]
}

Cut down to the parts I needed help with. I put the code where I try to compare the strings with bold tags

Don't paraphrase code, just copy and paste (and thanks for using code tags).

Don't paraphrase code, just copy and paste (and thanks for using code tags).

The code is way too long... I guess I could cut out parts but usually people just find it annoying if I post a bunch of code when I can just post a question. I'll just go ahead and cut out the unnecessary parts then and update my post

void mainSPGameFunc() {

	readFileGetRandom();
	string sSelectedWord = readFileGetRandom(); 
	string sGuess;

	int iWordLength = sSelectedWord.length();
		
	cout << "Guess the word!\n";
	for(int i = 0; i < iWordLength; i++) {
		cout << "_ ";
	}
	
	cin >> sGuess;

	[B]for(int i = 0; i < iWordLength; i++) {
		if(sGuess == sSelectedWord[i]) {
			cout << "RIGHT!";
		}
		system("PAUSE");
	}[/B]
}

Since sGuess and sSelectedWord both are strings you could compare them by simply

if (sGuess == sSelectWord)

by doing sSelectedWord you actually point to the character that position in the string

Edit: just re-read your post..

and I set a for loop up to check if sGuess is the same as any letter in sSelectedWord

Do you want to compare sGuess with any letter of sSelectedWord? Not sure then what that means :( .. the answer above might not help. Probably you need to declare sGuess as a 'char'. Can you explain a bit more in detail?

Well, what I want to do is that (I assume you know how to play hangman?) I get the users guess and store it in sGuess (s = string so stringGuess) and then I want to compare sGuess with the whole word.

Say we have the word "icecream" and I put in "c" as my guess. Then sGuess will be c (sGuess = c). Now I want to compare sGuess with all the letters in the word "icecream", therefor I set up the for loop as seen here:

for(int i = 0; i < iWordLength; i++) {
			if(sGuess == sSelectedWord[i]) {
				cout << "RIGHT!";
			}
		}

There I want to compare sGuess (in this case the letter "c") with sSelectedWord (in this case the word "icecream") so it would go

c = i
c = c
c = e
c = c
c = r
c = e
c = a
c = m

and therefor it would output "RIGHT!" once because c = c only once right? Understand?

So your comparison is right but you need to declare sGuess as a 'char' data type. Then you can compare sGuess with sSelectedWord

char sGuess

i would have sGuess be a char variable in this case. as stated earlier there may be multiple occurrences of the guess letter in the random word. for that reason you would either need another array to keep track of the positions in the random word or you could use a flag system if you are using a class. a simple way of doing this might be

void foo()
{
       char guess;
       string word;
       bool complete = false
       // get word from function
       int wordLength = word.length();
       int * correctPlaces = new int[wordlength];
       for(;;)
       {
              cout<< "\nPlease enter a guess: ";
              cin >> guess;
              for (int i = 0; i < wordLength; i++)
              {
                     if (guess == word[i])
                            corrrectPlaces[i] = 1;
              }
              cout << "\nOkay this is what you have so far";
              for (int j = 0; j < wordLength; j++)
              {
                     if(correctPlaces[j] == 1)
                            cout << word[j];
                     else
                            cout << "_";
              }
              for (int k = 0; k < wordLength; k++)
              {
                     if (correctPlaces[k] == 1)
                            complete = true;
                     else
                     {
                            complete = false;
                            break;
                     }
              }
              if (complete)
                     break;
       }
}

this is just a quick example of how to deal with multiply same letters in a string. depened on how you application is written this might not work for you but this should get you going

i would have sGuess be a char variable in this case. as stated earlier there may be multiple occurrences of the guess letter in the random word. for that reason you would either need another array to keep track of the positions in the random word or you could use a flag system if you are using a class. a simple way of doing this might be

void foo()
{
       char guess;
       string word;
       bool complete = false
       // get word from function
       int wordLength = word.length();
       int * correctPlaces = new int[wordlength];
       for(;;)
       {
              cout<< "\nPlease enter a guess: ";
              cin >> guess;
              for (int i = 0; i < wordLength; i++)
              {
                     if (guess == word[i])
                            corrrectPlaces[i] = 1;
              }
              cout << "\nOkay this is what you have so far";
              for (int j = 0; j < wordLength; j++)
              {
                     if(correctPlaces[j] == 1)
                            cout << word[j];
                     else
                            cout << "_";
              }
              for (int k = 0; k < wordLength; k++)
              {
                     if (correctPlaces[k] == 1)
                            complete = true;
                     else
                     {
                            complete = false;
                            break;
                     }
              }
              if (complete)
                     break;
       }
}

this is just a quick example of how to deal with multiply same letters in a string. depened on how you application is written this might not work for you but this should get you going

I use a third array, and then when all the slots in that array are the same as the sSelectedWord (when the word is solved basically).

So your comparison is right but you need to declare sGuess as a 'char' data type. Then you can compare sGuess with sSelectedWord

char sGuess

Yes that actually worked. I feel dumb now. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.