Solved

Putting ints into CStrings

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 121
C++ finding a sting in a char* string from a text file 3 130
Dynamically allocate memory 9 70
Outlook 13 84
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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