473,397 Members | 1,950 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,397 software developers and data experts.

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

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?
Oct 7 '09 #1
14 3640
newb16
687 512MB
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?
Oct 7 '09 #2
Banfa
9,065 Expert Mod 8TB
But this rather begs the question if they are input as strings why not store them as strings in the class?
Oct 7 '09 #3
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?
Oct 7 '09 #4
Banfa
9,065 Expert Mod 8TB
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.
Oct 7 '09 #5
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.
Oct 7 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 7 '09 #7
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
Oct 7 '09 #8
Banfa
9,065 Expert Mod 8TB
And what compiler errors do you get when you compile this code?
Oct 7 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 7 '09 #10
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?
Oct 7 '09 #11
newb16
687 512MB
see #2 - vector<char> v(100); ...initialize vector with content of the 'password' string... &*v.begin();
Oct 8 '09 #12
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 8 '09 #13
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"
Oct 9 '09 #14
Got it. Thanks all for your help
Oct 9 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: stroker_ace | last post by:
Hi, I wonder if anyone could point me in the direction of a discussion on the similarities and differences between the C++ String type and char* strings? The reason I ask is because I am...
7
by: David Jacques | last post by:
I have to reformat a string from the form "SRID=4269;POINT(-90.673 69.4310000006199)" to GeometryFromText('POINT (-141.095 68.5430000006417)',4269) ); I have a function to do this by...
7
by: David. E. Goble | last post by:
Hi all; I need to build a list of strings in one function and use the list in another function. ie buildlist(char list, FILE **infile); { int i;
47
by: pkirk25 | last post by:
I've made a small program to demonstrate one problem I'm having fixing strings in C. I need to be able to remove HTML mark-ups from text lines. I create my variable, pass it to my function,...
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
7
by: Adrian | last post by:
Hi, I want a const static std::set of strings which is case insensitive for the values. So I have the following which seems to work but something doesnt seem right about it. Is there a better...
2
by: Inner Dragon | last post by:
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...
20
by: Win Sock | last post by:
Hi All, somebody told me this morning that the following is leagal. char *a = "Hello wrold"; The memory is automatically allocated on the fly. Is this correct?
7
by: arnuld | last post by:
On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote: Either you are not understanding me or I am not able to understand this <p_strcmpthing. This is your code:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.