Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Strings and characters
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
IMRobbie
Newbie cheater
Reputation: 0

Joined: 26 Sep 2009
Posts: 24

PostPosted: Wed Nov 18, 2009 9:28 pm    Post subject: [C++]Strings and characters Reply with quote

How can you determined if a character entered is contained somewhere in a string?

I want the program to display, yes if the character is in the string.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Nov 18, 2009 10:50 pm    Post subject: Reply with quote

http://www.cplusplus.com/reference/string/string/find/
Back to top
View user's profile Send private message
IMRobbie
Newbie cheater
Reputation: 0

Joined: 26 Sep 2009
Posts: 24

PostPosted: Mon Nov 23, 2009 3:14 pm    Post subject: Reply with quote

How would you check for multiple occurrences of the same letter in the string?
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Nov 23, 2009 3:28 pm    Post subject: Reply with quote

Da0omph wrote:
How would you check for multiple occurrences of the same letter in the string?


http://www.cplusplus.com/reference/algorithm/count/
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Tue Nov 24, 2009 1:02 pm    Post subject: Reply with quote

Code:
int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
   int ret = 0;
   for(int i = 0; i < sizeOfArray; i++)
   {
      if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
            ret++;
   }
   return ret;
}
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Nov 24, 2009 1:11 pm    Post subject: Reply with quote

&Vage wrote:
Code:
int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
   int ret = 0;
   for(int i = 0; i < sizeOfArray; i++)
   {
      if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
            ret++;
   }
   return ret;
}

lmfao

I wonder what happens when sizeof( uString ) is something huge

actually, let's just consider the case when i = sizeOfArray - 1 and sizeof( UString ) is say.. 2 ?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue Nov 24, 2009 1:46 pm    Post subject: Reply with quote

&Vage wrote:
Code:
int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
   int ret = 0;
   for(int i = 0; i < sizeOfArray; i++)
   {
      if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
            ret++;
   }
   return ret;
}


what
Back to top
View user's profile Send private message
IMRobbie
Newbie cheater
Reputation: 0

Joined: 26 Sep 2009
Posts: 24

PostPosted: Tue Nov 24, 2009 3:32 pm    Post subject: Reply with quote

I'm on the verge on making a hangman game, so I'm trying to figure out a way to do it.

This is what I got so far.
Code:

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

string randWord();

int main()
{
   string guessWord = randWord(); //sets the returning value of randWord function in guessWord
   
   return 0;
}

string randWord()
{
   string wordBankArray[3] = {"random", "words", "here"};
   
   srand(static_cast<int> (time(0)));
   int randWord = rand() % (2 + 1);
   
   return wordBankArray[randWord];
}


Basically, it just selects a string out of wordBankArray and sticks it in the string variable guessWord.
Does anyone have any suggestion to what to do next?
Back to top
View user's profile Send private message
Fendaril
Cheater
Reputation: 0

Joined: 08 Nov 2009
Posts: 27

PostPosted: Tue Nov 24, 2009 4:16 pm    Post subject: Reply with quote

Think. In hangman if you enter in a letter it crosses out all occurences of that letter and reveals it. I believe in c++ strings can be indexed like arrays do something like
Code:

String word=wordBankArray[0];//sets to random

//lets say we picked a

word[1]="";

//word is now equal to rndom.


This is a very vague example but add more things like checking if the word length is 0. Then the person got the word right.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Nov 24, 2009 4:28 pm    Post subject: Reply with quote

Da0omph wrote:
Does anyone have any suggestion to what to do next?


Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.

Also, this is C++ and not C. Try and think in classes.
Back to top
View user's profile Send private message
IMRobbie
Newbie cheater
Reputation: 0

Joined: 26 Sep 2009
Posts: 24

PostPosted: Tue Nov 24, 2009 4:37 pm    Post subject: Reply with quote

Flyte wrote:
Da0omph wrote:
Does anyone have any suggestion to what to do next?


Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.

Also, this is C++ and not C. Try and think in classes.

You see, that's the trouble I'm having.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Nov 24, 2009 4:56 pm    Post subject: Reply with quote

Da0omph wrote:
Flyte wrote:
Da0omph wrote:
Does anyone have any suggestion to what to do next?


Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.

Also, this is C++ and not C. Try and think in classes.

You see, that's the trouble I'm having.

http://www.cplusplus.com/reference/string/string/replace/
Back to top
View user's profile Send private message
Fendaril
Cheater
Reputation: 0

Joined: 08 Nov 2009
Posts: 27

PostPosted: Tue Nov 24, 2009 5:21 pm    Post subject: Reply with quote

Slugsnack wrote:
Da0omph wrote:
Flyte wrote:
Da0omph wrote:
Does anyone have any suggestion to what to do next?


Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.

Also, this is C++ and not C. Try and think in classes.

You see, that's the trouble I'm having.

http://www.cplusplus.com/reference/string/string/replace/


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.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Nov 24, 2009 5:44 pm    Post subject: Reply with quote

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;
}
Back to top
View user's profile Send private message
Fendaril
Cheater
Reputation: 0

Joined: 08 Nov 2009
Posts: 27

PostPosted: Tue Nov 24, 2009 5:51 pm    Post subject: Reply with quote

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;
}


For a project such as this you REALLY do not need a class.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites