Need some help with a CString

Posted on 2007-10-21
Last Modified: 2010-04-01
I would like to build  a CString like the following:

I want to fill the string with 2 numbers  1)doulbe  2)int
The string should always be 8 bytes long

I also want to fill the 4-6 bytes with " x "

CString A="1.50 x 1500"
                   "5.00 x   100"
                   "0.55 x     10"
                   "0.01 x   500"
length=8      012345678

In my old days with business basic I had a function like this one:
int y=200;
Y$ =  y using "####"

double d=0.55
D$ = d using  "##.##"
So I can easly build a String like this "0.55 x  200"          String =  D$+" x "+Y$
Is there anything in C++ that I can easy build a string with numbers?
So the string is trimed right?

Please let me know, thanks.
500 points.
Best regards,

Question by:tsp2002
    LVL 2

    Accepted Solution

    You can go 2 ways about it.
    1) Make a function:
    void SetString(CString* str, double dNum, int nNum)
        str->Format("%.2f x %d", dNum, nNum)

    // you can make if statements for numbers under 10 and over 10 and add spaces or zeroes
    to the format string accordingly to make sure the string is always 8 char long.

    2) You can inherit a CString class and add a member function to format the string, this will require more
    code and will add complexity to your code, I would personally juts make the function and just call it whenever I need it.

    LVL 5

    Assisted Solution

    It's what gravit9 said.
    One tiny correction though, the %d should be %3d or %4d, where the number between % and 'd' is the number of digits you want for the integer.

    By the way, the examples you have given are bigger than 8 characters.
    "1.50 x 1500"
    1.50 = 4 chars
    space x space = 3 chars
    1500 = 4 chars
    LVL 39

    Assisted Solution

    You can use stringstream classes to format strings:

    // put that below #include "stdafx.h"

    #include <sstream>
    #include <iomanip>
    using namespace std;  

           CString A;
           double d1 = 1.5;
           int        i2  = 1000;
           ostringstream oss;
           oss << setw(4) << right << d1 << " x " << setw(4) << right << i2;
           CString A = oss.str().c_str();

    setw(4) makes the next column 4 bytes.
    right makes the next output right-justified.

    oss.str() gets the std::string from the ostringstream.
    oss.c_str() gets a 'const char*' from the std::string what can be assigned to a CString.

    Regards, Alex

    Author Comment

    thanks for the help
    Best regards from

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    730 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

    17 Experts available now in Live!

    Get 1:1 Help Now