Link to home
Start Free TrialLog in
Avatar of Philluminati
Philluminati

asked on

Putting ints into CStrings

How can i get my

int age

into this CString correctly. When the method is called like this cout << myClient.toString();
it prints funny characters instead of the the number stored in the int. How do i get it to "go in" correctly

CODE EXTRACT--------------------

CString Client::toString()
{
      CString retVal = "Name:\t"  + name + "\nAge\t:" + age + "\nSex:\t" + sex + "\nTelephone:\t" + telephone + '\n';
      return retVal;
}

Surely this is obvious to someone!!!

Thanks in advance!

Phill!
Avatar of grg99
grg99

You're probably getting the right answer :)

You're saying: Pls Mr Compiler, take the address of where you're going to put the string "Name:\t", then add that to my variable "name",
then add a bunch more addresses and integers together too.

In classsic C, you'd probably do something like:  
retVal = malloc(200);
sprintf( retVal, "Name:\t:%d\nAge:\t:%d ..... ",   name, age, ... );

In C++ you probably wan t to wrap all the integer variables with some function that converts them to CStrings, then the "+" operator will be interpreted as the overloaded CString concat operator, and things will work a bit better.    Except you're returning a local variable, which is unlikely to work very well in the long run.  











ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial