-
October 9th, 2011, 02:34 PM
#1
Comparing an inputted char to a string
I am currently working on a hangman game right now and I need to be able to compare an inputted char to the sting containing the current word for the game. but I don't know how to do that!
Here is what I have so far. I word to guess is "programming" and is extracted from a separate .dat file.
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
const string blank;
int main()
{
ifstream inFile;
ofstream outFile;
string wordToGuess;
char guessLetter;
inFile.open("hangman.dat");
cout << "Word: computer\n"
<< "Word: science\n"
<< "Word: programming\n\n";
inFile >> wordToGuess >> wordToGuess >> wordToGuess;
cout << "First Word in File to Guess: " << wordToGuess << endl << endl;
cout << " -------|\n",
cout << " | |\n",
cout << " |\n",
cout << " |\n",
cout << " |\n",
cout << " |\n",
cout << " -----";
cout << "\n\nEnter a letter to guess: ";
cin >> guessLetter;
guessLetter = toupper(guessLetter);
cout << "You entered: " << guessLetter << endl << endl;
cout << endl << endl;
(here is where I need to check "guessLetter" to "wordToGuess" which is the string "programming")
[/code]
-
October 9th, 2011, 03:17 PM
#2
Re: Comparing an inputted char to a string
Originally Posted by Shadoninja
I am currently working on a hangman game right now and I need to be able to compare an inputted char to the sting containing the current word for the game. but I don't know how to do that!
1) Write a loop testing each character in the string to the inputted character
or
2) Use std::find to search a series of value for a particular value.
Code:
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "abc123";
// search for the letter 'c' in string s
if ( find( s.begin(), s.end(), 'c') != s.end())
{
// the letter 'c' exists in abc123
}
else
{
// letter 'c' does not exist
}
}
Regards,
Paul McKenzie
-
October 9th, 2011, 05:07 PM
#3
Re: Comparing an inputted char to a string
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|