Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant archive_postsperpage - assumed 'archive_postsperpage' (this will throw an Error in a future version of PHP) in ..../archive/index.php on line 456
Some c++ help [Archive] - Forums

PDA

View Full Version : Some c++ help



mazoo
08-13-2007, 06:24 PM
I currently creating a bot in C++ but I've ran into a probelm,
When I want to change the title of the clinent using SetWindowText I don't know how to make the output as I like :S
Want it to be like -> "1920 exp to next Level."

I've tried things like SetWindowText(<my_handle>, exp + "to next Level."); but I'm affraid that my C++ skills ain't too good xD

Any help will be greeted.

zionz
08-13-2007, 09:34 PM
There are many ways to do this, i prefer this one (more C than C++ tough):



char msg[512];
sprintf(msg,"%i + to next Level.", exp);
SetWindowText(<my_handle>, msg);

Kekke
08-13-2007, 10:11 PM
Easier and safer(?) way the way I like it:




#include <iostream>
#include <sstream>

using namespace std;

int main()
{
int exp = 512; //declares
string msg;
stringstream buffer;

buffer << exp; //transfer exp to stringstream
buffer >> msg; //transfer the stringstream to msg (a string)

msg += " exp to new level"; //append the text to our string

cout << msg << endl; // cout it, here is where you setwindowtext it.
cin.get();
}


As I thought:



Most buffer overflow problems in C can be traced directly back to the standard C library. The worst culprits are the problematic string operations that do no argument checking (strcpy, strcat, sprintf, gets).

zionz
08-13-2007, 10:30 PM
Kekke is right, that way is much safer, but if you want it safe in C you can always use snprintf(), about speed, i think the c version will be much faster, but if speed is not an issue, then always use the safest method.

mazoo
08-15-2007, 07:26 AM
Thanks! Problem solved.

rand0m_
08-20-2007, 06:04 PM
Or you could just do something like:
SetWindowText(<my_handle>, (exp + " experience left.") );

:p

zionz
08-20-2007, 08:39 PM
Or you could just do something like:
SetWindowText(<my_handle>, (exp + " experience left.") );

:p

That wont work in c++, SetWindowText takes a char* as a parameter (guessing that exp is a string class in that example)

rand0m_
08-22-2007, 06:56 PM
That wont work in c++, SetWindowText takes a char* as a parameter (guessing that exp is a string class in that example)


Hmm? when using (exp + " experience left.") it creates a string. If you'd have some experience with strings and chars et c, you would know that you can use the "c_str()" function to convert this string into the correct type. :D

zionz
08-22-2007, 10:31 PM
I know but you first have to convert exp (int) to a string and thats the original problem :p.

You mean this way?:



#include <iostream>

using namespace std;

int main()
{
string cexp = "100";

printf((cexp + " left").c_str());
return 0;
}


Or you say that cexp being int will allow you to do the same?

Kekke
08-23-2007, 12:43 AM
Yeah.. exp will most likely be an int.