Solved

Putting ints into CStrings

Posted on 2003-11-04
2
236 Views
Last Modified: 2010-04-02
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!
0
Comment
Question by:Philluminati
2 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 9682836
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.  











0
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 9683486
You may be able to use CString::Format.  It formats the string contents according to a format string like sprintf.  For example:

CString retval;
retval.Format( "Name:\t%s\nAge\t:%d\nSex:\t%c\nTelephone:\t%s\n", name, age, sex, telephone);

This only works if your data has types that sprintf knows.  You can put out a CString with a format specification of %s if you convert the corresponding argument.  For example, if name is a CString, you would match the %s with name.operator LPCTSTR().

Maybe I'm missing something, but I don't see any problem with returning a local variable by value.

--efn
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now