strings/char problem :(

I

Inner Dragon

Hey guys, I've been having a real hard time trying to pass a string as
parameter and then converting it to char to be used with
SDLNet_TCP_Recv. It's been hell with me though, here's my non-working
code... any help is greatly appreciated! :)

------------------------------------------------------------------------------------------------

void Login(std::string &pass){
cout << pass << endl;
char * loginstr;
/*
What I want to archieve is to be able to use:

sprintf(loginstr,"user\npass");

And then send it to the respective function, Send():

void Send(char *msg){
SDLNet_TCP_Send(sock,msg,strlen(msg));
}

as: Send(loginstr);
*/

}

int main(int argc, const char* argv[]) {
std::string pass;
cout << "Password: ";
cin >> pass;
Login(pass);
return(0);
}

----------------------------------------------------------------------------------------
 
B

BobR

Inner Dragon said:
Hey guys, I've been having a real hard time trying to pass a string as
parameter and then converting it to char to be used with
SDLNet_TCP_Recv. It's been hell with me though, here's my non-working
code... any help is greatly appreciated! :)
----------

void Login(std::string &pass){
cout << pass << endl;
char * loginstr;
/*
What I want to archieve is to be able to use:

sprintf(loginstr,"user\npass");

std::string mystring("user\npass");
mystring.push_back('\0'); // insurance!
char *loginstr( &mystring.at(0) );
std::cout<<"loginstr="<<loginstr<<std::endl;
And then send it to the respective function, Send():

void Send(char *msg){
SDLNet_TCP_Send(sock,msg,strlen(msg));
}
as: Send(loginstr);
*/

Change 'Send()' to:

void Send( char const *msg ){}

Then:
Send( myString.c_str() );
}

int main(int argc, const char* argv[]) {
std::string pass;
cout << "Password: ";
cin >> pass;
Login(pass);
return(0);
}
----------
 
G

Gianni Mariani

Inner said:
Hey guys, I've been having a real hard time trying to pass a string as
parameter and then converting it to char to be used with
SDLNet_TCP_Recv. It's been hell with me though, here's my non-working
code... any help is greatly appreciated! :)

------------------------------------------------------------------------------------------------

void Login(std::string &pass){
cout << pass << endl;
char * loginstr;
/*
What I want to archieve is to be able to use:

sprintf(loginstr,"user\npass");

NEVER use sprintf on user based input... In fact, never use sprintf
-period-.
And then send it to the respective function, Send():

void Send(char *msg){
SDLNet_TCP_Send(sock,msg,strlen(msg));
}

as: Send(loginstr);
*/

}

int main(int argc, const char* argv[]) {
std::string pass;
cout << "Password: ";
cin >> pass;
Login(pass);
return(0);
}

void Login(const std::string &pass) // << note the const
{
cout << pass << endl;

std::string msg = std::string(user) + '\n' + pass;

SDLNet_TCP_Send( sock, msg.data(), msg.length());
}

int main(int argc, const char* argv[]) {
std::string pass;
cout << "Password: ";
cin >> pass;
Login(pass);
return(0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top