The following gives an error: ** (NameList[][] is an array of type
string)
Form1->Caption = "MyProg " + NameList[CurrentStudent][0] + " You are
currently at Level " + NameList[CurrentStudent][2]);
I get :
[C++Error] Login.cpp(62): Could not find a match for
'System::AnsiString::operator
=(std::basic_string<char,std::char_traits<char>,std::allocator<char> >)'.
Randal
There is nothing wrong in using String to set the caption of a form. In the
statement above there is a ")" that doesn't appear to have a sister "(", try
correcting that.
By the way if you want to convert a String into AnsiString it's simpel:
String s1;
AnsiString as1;
s1 = "Hello";
as1 = s1; // The String is then converted
>I want to build a string the display as a form caption.
>but the form caption it AnsiString...
>
>The following gives an error: ** (NameList[][] is an array of type
>string)
>
>Form1->Caption = "MyProg " + NameList[CurrentStudent][0] + " You are
>currently at Level " + NameList[CurrentStudent][2]);
You cannot mix type AnsiString (or String) and type std::string.
Try
AnsiString FormCaption;
FormCaption.sprintf( "MyProg %s You are currently at level ",
NameList[CurrentStudent][0].c_str(),
NameList[CurrentStudent][2].c_str());
Form1->Caption = FormCaption;
--
General information:
* Post to the right group - http://www.borland.com/newsgroups/
* Do not cross- or multipost
* Research at http://www.mers.com/searchsite.html
Stefan Hoffmeister - http://www.econos.de/
TeamB - http://www.teamb.com/
Randal
>I tryed it but I get sprintf is not a member of AnsiString?
Oh, so you are not using C++Builder 4.0.
Does AnsiString have the printf member in your (apparently old) version of
C++Builder? If not, try the Format member as a last resort.
I tried:
//
char buff[100] = "";
string tmpstr1 = NameList[CurrentStudent][0];
string tmpstr2 = NameList[CurrentStudent][2];
sprintf(buff,"Testing Hello %s This is a test %s"
,tmpstr1, tmpstr2);
Form1->Caption = buff;
//
Now the sprintf is not working correctlly?
The out put is "Testing Hello This is a test name"
^ %s tmpstr1 is missing???
Where name is the data in tmpstr1 (shown in the second %s)?
and the first %s is skipped?
( I checked to validate data is in tmpstr2 and tmpstr1)
Why was the first %s skipped??
Randal
> string tmpstr1 = NameList[CurrentStudent][0];
> string tmpstr2 = NameList[CurrentStudent][2];
> sprintf(buff,"Testing Hello %s This is a test %s"
> ,tmpstr1, tmpstr2);
Try
sprintf(buff,"Testing Hello %s This is a test %s"
,tmpstr1.c_str(), tmpstr2.c_str());
I guess this is a borland thing?
Randal
Case Closed... ;-)
>That fixed it...
>I did not think std::string could use "c_str()"??
Why not?
>I guess this is a borland thing?
No. ISO C++.
Form1->Caption = "MyProg " + AnsiString(NameList[CurrentStudent][0].c_str()) + ...
?
Terry West
>Why not ...
>
>Form1->Caption = "MyProg " + AnsiString(NameList[CurrentStudent][0].c_str()) + ...
This is another option, but I personally find this to be unreadable.
I agree about the readability but what I was trying to ask was ...
Which string --> AnsiString conversion method is generally to be preferred ...
1) the AnsiString(string.c_str()) constructor
2) the Format("....%s....",string.c_str()) AnsiString method
3) the sprintf("....%s....",string.c_str()) AnsiString method
- particularly if the objective is to format multiple strings into a single AnsiString
(as in the original post)? Also, if you've the inclination, why?
Forgive any perceived pedantry in the question but, as an STL tyro grappling
with trying to find its 'best' integration into the VCL ....
Terry West
I wote for this one when possible or
AnsiString As;
As=string.c_str();
when not.
> 2) the Format("....%s....",string.c_str()) AnsiString method
> 3) the sprintf("....%s....",string.c_str()) AnsiString method
AFAIK these two shoudn't work as AnsiString::c_str() should return const
char*. But it doesn't so they do work. I'd avoid them anyhow as I expect
(read I hope) that AnsiString::c_str() will return const char* in future
release.
> - particularly if the objective is to format multiple strings into a
single AnsiString
> (as in the original post)? Also, if you've the inclination, why?
In that case opeartor AnsiString::operator += (char*) or
std:.string::operator+=(std:.string) are my choices (whichever you prefer).
goran
>Which string --> AnsiString conversion method is generally to be preferred ...
>
>1) the AnsiString(string.c_str()) constructor
This is the only conversion operation.
>2) the Format("....%s....",string.c_str()) AnsiString method
>3) the sprintf("....%s....",string.c_str()) AnsiString method
These are formatting operations.
>- particularly if the objective is to format multiple strings into a single AnsiString
>(as in the original post)? Also, if you've the inclination, why?
I prefer sprintf over Format, as sprintf is more readable than the
slightly forced open-array emulation of Format.