[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Need some help with a CString

Posted on 2007-10-21
Medium Priority
245 Views
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"
or
"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\$
012345678

Is there anything in C++ that I can easy build a string with numbers?
So the string is trimed right?

500 points.
Best regards,
Thomas

0
Question by:tsp2002

LVL 2

Accepted Solution

gravit9 earned 1000 total points
ID: 20118904
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.

Reference: http://msdn2.microsoft.com/en-us/library/wc7014hz(VS.71).aspx
0

LVL 5

Assisted Solution

discon0 earned 800 total points
ID: 20118925
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
0

LVL 39

Assisted Solution

itsmeandnobodyelse earned 200 total points
ID: 20122476
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

0

Author Comment

ID: 20123147
thanks for the help
Best regards from
Thomas
0

## Featured Post

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
###### Suggested Courses
Course of the Month19 days, 3 hours left to enroll