Question:
C++ String Usage Problem?
?
2009-11-20 05:22:03 UTC
Hi i'm a newbie in C++ WINAPI gui programming.

Here is a part of my code :
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
DWORD drives = GetLogicalDrives();
int mask = 0;
char drv;
string sDrv;
for(int i=1;i<=26;i++){
mask = int(Math::Pow(2.0, i));
if(drives & mask){
drv = 'A'+i;
sprintf(sDrv, "%c:\\", drv);
this->cdCombo->Items->Add(sDrv);
}
}
}

========================================================================
when compiling, i got these error messages :

1>c:\users\sxstrat\documents\visual studio 2010\projects\conv\conv\Form1.h(148): error C2664: 'sprintf' : cannot convert parameter 1 from 'std::string' to 'char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\sxstrat\documents\visual studio 2010\projects\conv\conv\Form1.h(149): error C2664: 'System::Windows::Forms::ComboBox::ObjectCollection::Add' : cannot convert parameter 1 from 'std::string' to 'System::Object ^'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

========================================================================
i dont really understand why the sprintf function cannot do the conversion, and why the System::Windows::Forms::ComboBox::ObjectCollection::Add cannot add value of type string. FYI, i already #include and have put the "using namespace std;" line.
========================================================================
anyone have the solution?

BTW, im using VC++ 2010 Express
Three answers:
?
2009-11-20 05:49:44 UTC
This is a part answer, only.



sDrv is declared string

you have do declare sDrv something like:

char sDrv[256];

sprintf(sDrv, "%c:\\", drv);

then your compiler won't complain

about sprintf, anymore.
cja
2009-11-20 09:11:46 UTC
The previous responder got to the gist of it, but I'll elaborate.



You say you don't understand why sprintf can't convert from string to char*. There is no conversion from string to char*, it's not sprintf's fault. Besides that, sprintf is from the C library, where there is no such thing as a string. The string class is from the C++ library. The char array in a C++ string object can be accessed by the c_str( ) operation of string, but you should not try to have sprintf write to that address.



If you want to use sprintf, stick with char arrays. If you want to use a C++ string, there are ways to get sprintf-like functionality, e.g., by using a stringstream.
?
2016-10-18 10:45:48 UTC
you will use the C function noted as strcmp(). it incredibly is bare bones whilst in comparison with the C string type, notwithstanding it relatively works: int matched = strcmp( password, samplepassword ) if( matched == 0 ) { printf("get entry to granted"; } Counter-intuitive, 0 skill they matched. you additionally can use strncmp to learn in basic terms element of strings. See under for extra C string applications. HTH


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...