Solved

Putting ints into CStrings

Posted on 2003-11-04
2
233 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

15 Experts available now in Live!

Get 1:1 Help Now