Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


the separator

g double

Representation of a number using either the e
or the f format

G double

Representation of a number using either the E
or the f format
s String String representation
S String String representation

An example would be:

char Represents[20];
sprintf(Represents, "%f", );

If you want to display your own, unformatted words as part of the new string, you can
type anything before the % operator or after the formatting character:

char Represents[20];
sprintf(Represents, "The number is %f", );

If you are formatting a floating point number, you can specify the number of digits to
appear as the separator. To do this, type a period on the right side of the % symbol
followed by a number. For example, to produce a number with a precision of 4 digits,
you would type:

sprintf(Represents, "The number is %.4f", );

To create a longer or more advanced string, you can add as many combinations of % and
characters as necessary.

The % symbol used in the S argument is used as a place holder, informing the compiler
that an external value will be needed for that position. To specify a value for a place
holder, provide it as a third argument. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

char Represents[20];
double Number = 125.55;
sprintf(Represents, "The number is %.4f", Number);
}

If you created more than one combination of % and character, you can provide a value
for each, separated by a comma. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

char FirstName[] = "Ferdinand";
char LastName[] = "Coly";
char FullName[40];

sprintf(FullName, "Full Name: %s %s", FirstName, LastName);
Free download pdf