473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strings/char problem :(

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

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

Sep 1 '07 #1
2 1686

Inner Dragon <da***********@gmail.comwrote in message...
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);
}
----------
--
Bob R
POVrookie
Sep 1 '07 #2
Inner Dragon wrote:
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);
}
Sep 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ben | last post by:
Hey everybody, I'm working on a program in c++, and I've come up against a problem that I can't figure out. I don't imagine that it's too difficult to solve, but it has been giving me trouble....
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.