Welcome to Laser Pointer Forums - discuss green laser pointers, blue laser pointers, and all types of lasers

How to Register on LPF | LPF Donations

C++






Me... In four years... ;D
(I'm serious).

Anyway, NairB is learning C++ if I remember correctly, you may want to ask him.
 
Oh good, I can do php, css, html, javascript, autoit, visual basic, but just not C++...

Anyways, I got this code:
Code:
int __stdcall MyRecv(SOCKET x,char* b,int l,int pr,FARPROC pp)
   {
   // call original recv

      // do content processing here
//replace all instances of the word "hello" with "hi" in the variable "b"
      //end processing

   typedef int (__stdcall *pS)(SOCKET,char*,int,int);
   pS pps = (pS)pp;
   int rV = pps(x,b,l,pr);

   return rV;
#endif
   }

Where it says "//replace all instances of the word "hello" with "hi" in the variable "b"" is where I, well, need to replace all instances of the word "hello" with "hi" in the variable "b". Im really bad at C++, so any help would be great. With the other languages I use, there is usually a stringreplace() type function for replacing pieces of text with a different text.
 
NairB said:
Incidently, has anyone bought or read Deitel's new C++ book?.....(it also covers game programming with the OGRE graphics library) :D
I have a C/C++ programming book made by Deitel, I started reading it when I was in 9th grade, but I had to stop reading it because things (homeworks) got much worse, I read up to page 200 or something like that.

I know some basic C commands, but I don't really know C++, I'm taking a 4-year course of C/C++ and electronics/hardware in school though.
 
i am quite familiar with C++

all of my software on http://deeptide.com was written in C++ using dev-C++

the code you are looking at is using a function pointer. this is often used to call a function from a DLL file.

in terms of replacing, take a look at the std::string

i think that has a replace function that should get the job done.
 
I just started practicing C++ a few days ago, so I'm still a noob at it. :P
 
@spyorocks...I am nowhere ready to answer this spyorocks as I have only just started reading functions. :-/

I found this regarding how to convert from a string to char* array from a user textbox http://www.thescripts.com/forum/thread137848.html

Im not sure if that is what you might need but hopefully set you on the correct path.

I am interested though what c0ld said.

@c0ld....will the std::string library already have written code to carry out this replace function that spyorock needs?? :-/

@King K or anybody....Do you think that the Deitel books are the best for teaching C++. Thenew Deitel Version 6 book looks interesting. However, it tries to teach classes before functions, is that a bad thing do you think??

Oops...sorry for the slight hijack of your thread spyorocks....it is C++ related :-X
 
Ah, thats fine nairb. Maybe someone will come along and help me with my code. I have been looking everywhere for a C++ expert who could help me with this...
 
Hey Spyro,

Maybe this guy can help or you might find the answer on his website...Click the pic...



He happens to be the inventor of C++.... 8-)

NairB
 
spyrorocks said:
I dought he gives personal help to people...

LoL....I forgot to metion that but his website has lots of links etc to various resources. ;)
 
hey, you might find this example helpful

(compile this code as regular console application)

i think u will be able to implement some of this code in your own, first just try to understand how this example program works

Code:
#include <iostream>
#include <string>

/* If 'from' matches 'to' or 'from' is empty, */
/* does not parse 's', returns std::string::npos */
/* */
/* Otherwise returns number of replacements done */
/* */

std::string::size_type repl(std::string& s,
const std::string& from,
const std::string& to)
{
std::string::size_type cnt(std::string::npos);

if(from != to && !from.empty())
{
std::string::size_type pos1(0);
std::string::size_type pos2(0);
const std::string::size_type from_len(from.size());
const std::string::size_type to_len(to.size());
cnt = 0;

while((pos1 = s.find(from, pos2)) != std::string::npos)
{
s.replace(pos1, from_len, to);
pos2 = pos1 + to_len;
++cnt;
}
}

return cnt;
}

int main()
{
//std::string s("hellohello my name is hello ok helloshould be hello");
std::string s="hello my name is hello testhellotest";
const std::string old_seq("hello");
const std::string new_seq("hi");

std::cout << "Original string:\n"
<< '"' << s << '"'
<< "\n\n";

const std::string::size_type count(repl(s, old_seq, new_seq));
const std::string dq(count > 0, '"');
const bool parsed(count != std::string::npos);
const bool found(parsed && count > 0);

if(parsed)
{
std::cout << count
<< " occurences of sequence \"" << old_seq << "\""
<< (count ? " replaced with sequence " : " found")
<< dq << (count ? new_seq : "") << dq
<< "\n\n";
}
else
std::cout << "Nothing to change\n\n";

std::cout << (parsed && found
? std::string("New string:\n" + dq + s + dq)
: "No changes made")
<< '\n';
system("pause");
return 0;
}

Note:

if you keep getting stuck i would highly advise
http://codeguru.com/forum

imho this is the best programming forum on the internet

good luck & happy coding

regards
-avery
 
This is the code I got now:

Code:
// recv.h 
#include <string>

int __stdcall MyRecv(SOCKET x,char* b,int l,int pr,FARPROC pp)
   {
   // call original recv
      //MessageBox(NULL, b, "data", MB_OK);
      //repstring = std::string
std::string temp = b;
int index = temp.find("hello");

while(index != std::string::npos)
{
temp.replace(index, 5, "hi", 2);
index = temp.find("hello");
}

char *copy = new char[temp.size() + 1];
temp.copy(copy, temp.size());

   typedef int (__stdcall *pS)(SOCKET,char*,int,int);
   pS pps = (pS)pp;
   int rV = pps(x,copy,l,pr);

   delete [] copy;
   return rV;
   }

It compiles, but crashes when its used...
 


Back
Top