Connecting Tech Pros Worldwide Forums | Help | Site Map

Enter two strings and pass as const char* and char*

Newbie
 
Join Date: Oct 2009
Posts: 21
#1: Oct 7 '09
I am having a user enter two strings: user, password. I then have to pass these two strings to a class function that accepts const char* for user and char* for password. How would I convert the strings to const char* and char* before I pass them? Am I right for assuming that I have to initialize user and password as strings so 'cin >>' works?

Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#2: Oct 7 '09

re: Enter two strings and pass as const char* and char*


If you use std::string, you can get const char* using c_str(), however to get char* (without const) you need to use vector<char> x and take address of its first element - &*x.begin()
Another question is why it requires char* for password - is it going to modify it?
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#3: Oct 7 '09

re: Enter two strings and pass as const char* and char*


But this rather begs the question if they are input as strings why not store them as strings in the class?
Newbie
 
Join Date: Oct 2009
Posts: 21
#4: Oct 7 '09

re: Enter two strings and pass as const char* and char*


Password is char* because it can be changed with the ChangePW function, which I will do next after I figure out how to pass the arguments. Banfa, the class PWServer is already written so I am forced to pass as const char* and char*. Is there any other declaration for user and password that I could use that would be easier to pass?
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#5: Oct 7 '09

re: Enter two strings and pass as const char* and char*


The password may be changeable, that does not mean you have to pass it to the class as char *, you could easily pass it as const char *.

However you say that PWServer is already written so you are stuck with passing const char * for user and char * for password.

Using string for user should work fine, as long as PWServer copies the data from the string in the function you pass it to. For password you are a bit stuffed, read it as a string, allocate a temporary buffer and populate it from the string to pass to PWServer, again assuming PWServer is going to copy the data from the pointer passed to it and then release the temporary buffer.

It sounds like PWServer has not been well designed.
Newbie
 
Join Date: Oct 2009
Posts: 21
#6: Oct 7 '09

re: Enter two strings and pass as const char* and char*


I have to pass them as const char* and char*. Therefore I have to convert them first. I get an error if I try to pass them as anything but. It's a project for school so that's why it is set up this way. I can post some code later tonight so you can see where I am making my mistake.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#7: Oct 7 '09

re: Enter two strings and pass as const char* and char*


From what I read you should not have a problem. The string entered using cin is a C string which is an array of char aqnd the name of the string is the address of element 0, so you have a char*.

You can pass this char* top a function that has a const char* argument with no problem. The char* will be copied to the const char* making the function unable to change the contents of the string, which is probably the intent.

You should post that code since I am not sure what your difficulty is at this point.
Newbie
 
Join Date: Oct 2009
Posts: 21
#8: Oct 7 '09

re: Enter two strings and pass as const char* and char*


string user;
string password;

cout << "User ID: ";
cin >> user;
cout << "Password: ";
cin >> password;
const char* Uptr;
char* PWptr;
user * Uptr;
password * PWptr;
PWServer *pwserverPtr;
pwserverPtr->CheckPW(Uptr, PWptr);

PWServer::PWCheck(const char* uid, char* pwd) as seen in pwserver.h
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#9: Oct 7 '09

re: Enter two strings and pass as const char* and char*


And what compiler errors do you get when you compile this code?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#10: Oct 7 '09

re: Enter two strings and pass as const char* and char*


Quote:

Originally Posted by randysimes

string user;
string password;

cout << "User ID: ";
cin >> user;
cout << "Password: ";
cin >> password;
const char* Uptr;
char* PWptr;
user * Uptr;
password * PWptr;
PWServer *pwserverPtr;
pwserverPtr->CheckPW(Uptr, PWptr);

PWServer::PWCheck(const char* uid, char* pwd) as seen in pwserver.h

What do you intend with user* and password* ?

I expect you want your user and password to be a char*.

Since you are using std::string, you need to convert this to a char*. To do that you code:

Expand|Select|Wrap|Line Numbers
  1. const char* Uptr = user.c_str();
Now you can use Uptr in your PWCheck call. the string::c_str() method exposes the string as a C string. Note it exposes it as a const char*. That prevents you from changing the contents of the string by using the pointer.

Do the same thing with your password.
Newbie
 
Join Date: Oct 2009
Posts: 21
#11: Oct 7 '09

re: Enter two strings and pass as const char* and char*


user has to be constant, password does not because there will be an option to change it later. When I enter the following lines
48. const char* Uptr = user.c_str();
49. char* PWptr = password.c_str();
50.PWServer *pwserverPtr;
51. pwserverPtr->CheckPW(Uptr, PWptr);

I get the following error
pwclient.cpp:49: error: invalid conversion from const char* to char
pwclient.cpp:51: error: invalid conversion from char to char*
pwclient.cpp:51: error: initializing argument 2 of int PWServer::CheckPW(const char*, char*)

Is there any way to initalize user and password as const char* and char* respectively and still use the cin >> command?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#12: Oct 8 '09

re: Enter two strings and pass as const char* and char*


see #2 - vector<char> v(100); ...initialize vector with content of the 'password' string... &*v.begin();
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#13: Oct 8 '09

re: Enter two strings and pass as const char* and char*


Quote:

Originally Posted by randysimes

Is there any way to initalize user and password as const char* and char* respectively and still use the cin >> command?


Yes. Did you read my Post#10 ?

C++ requires the const value be known when the const is created. You just can't define a const and not say what the value of the constant it.
Newbie
 
Join Date: Oct 2009
Posts: 21
#14: Oct 9 '09

re: Enter two strings and pass as const char* and char*


Thanks for all you help. I'm sure I will need more in the future. I was not constructing my class in the line: PWServer pws
Should have been: PWServer pws(argv[1], argv[2], 100)
None of this pointer stuff was needed. Now my dilema is how to see if the class function completed. The assignment states that a 1 will be returned if the operation was successful. How do I check for that value? This is what I had guessed, but was wrong: If(pws.ChangePW == 1)
cout << "success"
Newbie
 
Join Date: Oct 2009
Posts: 21
#15: Oct 9 '09

re: Enter two strings and pass as const char* and char*


Got it. Thanks all for your help
Reply