Converting from CString to String and char array string needed to yield the same string type

Posted on 2005-03-06
Medium Priority
Last Modified: 2013-11-20
I have converted a couple of char array to string like this:
char username_in[20];
char password_in[20];
std::string a((char*) username_in);
std::string b((char*) password_in);   //this part compiled fine

I have convereted a couple of CString to String like this:
DDX_CBString(pDX, IDC_Username, L_username);  
DDX_CBString(pDX, IDC_Password, L_password);
std::string u1((LPCTSTR)L_username);
std::string p1((LPCTSTR)L_password);   //this part compiled fine

But when I try to do a string compare like this:
int sameU = strcmp(a,u1);
int sameP = strcmp(b,p1);

it gives me the error that the two types i'm trying to pass into the strcmp are not the same.
I'm confused because I thought i had changed both of the types to strings so that i can compare them.
So there is probably something wrong with my conversion but I'm not sure what...

This is the error I'm getting:
error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Question by:chinese_bunny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Assisted Solution

AlexFM earned 140 total points
ID: 13470464
int sameU = strcmp(a.c_str(),u1);
LVL 30

Accepted Solution

Axter earned 360 total points
ID: 13470474
Hi chinese_bunny,
You need to use the c_str member function.

int sameU = strcmp(a.c_str(),u1.c_str());
int sameP = strcmp(b.c_str(),p1.c_str());

The .c_str() method returns the constant char* pointer that the C-Style string functions are looking for.

Author Comment

ID: 13470487
oh my gosh! thank you very much to both of you.  it works like a charm now. 0 errors! <33333

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question