| View previous topic :: View next topic |
| Author |
Message |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Nov 24, 2009 6:00 pm Post subject: |
|
|
| Fendaril wrote: | | For a project such as this you REALLY do not need a class. |
Do you make it a habit to continually challenge your intellectual superiors?
There is no reason to not use a class. The whole point of the project is to learn. He wouldn't learn anything if he treated C++ as a lenient version of C like the rest of this forum does.
|
|
| Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Tue Nov 24, 2009 6:03 pm Post subject: |
|
|
| Flyte wrote: | This is what I mean by "thinking in classes".
| Code: | #include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
class MaskedString {
const string m_string;
set<char> m_letters;
bool IsMasked(const char C) const {
return (m_letters.find(C) == m_letters.end());
}
public:
MaskedString(const string & Target)
: m_string(Target) {}
string Masked() const {
string ret(m_string);
replace_if(ret.begin(), ret.end(), bind1st(mem_fun(&MaskedString::IsMasked), this), '-');
return ret;
}
string Real() const {
return m_string;
}
void Unmask(const char C) {
m_letters.insert(C);
}
};
int main()
{
string random = "zorro";
MaskedString zorro(random);
zorro.Unmask('z');
zorro.Unmask('r');
cout << zorro.Masked() << endl;
cout << zorro.Real() << endl;
return 0;
} |
|
I'm fairly new at the C++ language, I haven't really got into classes. =(
| Fendaril wrote: |
Loop through the string. If the inputted letter equals to a letter in the string display it. It is not hard.
Like I said you can index Strings like arrays. |
Could something like this be used to find the occurrences?
| Code: |
#include <iostream>
#include <string>
using namespace std;
int main() {
string guessWord = "Hello";
string letter = "";
int letterPos = 0;
cout << "Enter a letter: ";
cin >> letter;
int numChars = guessWord.length();
for(int x = 0; x < numChars; x++)
{
if(guessWord.find(letter, 0) != -1) {
letterPos = guessWord.find(letter);
cout << "Yes, letter " << letter << " was found!";
cout << "\nThe letter you enter was found in the string at index position ";
cout << letterPos << endl;
x = numChars;
} else {
cout << "Nope\n";
x = numChars;
}
}
return 0;
}
|
Last edited by IMRobbie on Fri Jan 29, 2010 2:27 pm; edited 7 times in total |
|
| Back to top |
|
 |
Fendaril Cheater
Reputation: 0
Joined: 08 Nov 2009 Posts: 27
|
Posted: Tue Nov 24, 2009 6:11 pm Post subject: |
|
|
| yes.
|
|
| Back to top |
|
 |
|