Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Putting ints into CStrings

Posted on 2003-11-04
2
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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