Concatenate CString var + int var 
Author Message
 Concatenate CString var + int var

Assume:

CSting xxxx;
int num;
...
num = 1;
xxxx = "this is the current num:" + num;

Surprisingly this will result in the following text in the xxxx var:
his is the current num:

The first character is omitted and no num is appended. Why ?
How do I concatenate these two variable parts ?

Bye

Marcus

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Fri, 28 Dec 2001 03:00:00 GMT  
 Concatenate CString var + int var
Because you're doing pointer arithmetic, not string formatting.  That is to
say, you're taking the address of your static string "this...", adding 1 to
it ("his..."), and assigning it to your CString.  What you *want* to do is
xxxx.Format("This... %d", num).

HTH

--

-Steven M. Forrester
Directeur Dveloppement
Capsule Technologie

Quote:

> Assume:

> CSting xxxx;
> int num;
> ...
> num = 1;
> xxxx = "this is the current num:" + num;

> Surprisingly this will result in the following text in the xxxx var:
> his is the current num:

> The first character is omitted and no num is appended. Why ?
> How do I concatenate these two variable parts ?

> Bye

> Marcus

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



Fri, 28 Dec 2001 03:00:00 GMT  
 Concatenate CString var + int var

Quote:

>Assume:

>CSting xxxx;
>int num;
>...
>num = 1;
>xxxx = "this is the current num:" + num;

>Surprisingly this will result in the following text in the xxxx var:
>his is the current num:

>The first character is omitted and no num is appended. Why ?
>How do I concatenate these two variable parts ?

A string literal has type "array of char" and thus decays into a char* in
this context, so you're doing some pointer arithmetic. Remember that C++
operator overloading can apply only when at least one of the operands is of
a user-defined type.

CString has no ctor that turns a number into its textual representation, nor
does it define an overloaded operator+ for this, so you'll need to use
CString::Format, sprintf, _itoa, iostreams, etc.

--
Doug Harrison

Visual C++ MVP



Fri, 28 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Concatenate CString var + int var

2. How to change CString var into int var?

3. CONFLICT - Global Var of a .LIB Vs Global Var of a program which uses the LIB

4. var = !var;

5. extern var vs. static extern var

6. type *var -- vs. -- type* var

7. Args: var number & var types

8. how to call C++ func(int &var) from C.

9. int to var conversion

10. pass null to int var in C#

11. WEIRD input letter in an int var?

12. CDialog - member var: typ int: negative values

 

 
Powered by phpBB® Forum Software