Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

Int main()

{

double PropertyValue1 = 782500,

PropertyValue2 = 625560,

PropertyValue3 = 284782;

Console::Write(L"Property Value 1: ");

cout.width(8);

cout << PropertyValue1 << endl;

Console::Write(L"Property Value 2: ");

cout.width(14);

cout << PropertyValue2 << endl;

Console::Write(L"Property Value 3: ");

cout.width(10);

cout << PropertyValue3 << endl;

Console::WriteLine();

return 0;

}

This would produce:

Property Value 1: 782500

Property Value 2: 625560

Property Value 3: 284782

 

8.2.2Filling the Empty Space

As we have seen already, the compiler leaves empty spaces on the left side of the displaying value if the width is greater than the needed space. If you do not want that space to be empty, you can fill it with any character you want. To do that, call fill(). You can use it with empty parentheses. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue1 = 782500,

PropertyValue2 = 625560,

PropertyValue3 = 284782;

Console::Write(L"Property Value 1: ");

cout.width(8);

cout.fill();

cout << PropertyValue1 << endl;

Console::Write(L"Property Value 2: ");

cout.width(14);

cout << PropertyValue2 << endl;

Console::Write(L"Property Value 3: ");

cout.width(10);

cout << PropertyValue3 << endl;

Console::WriteLine();

return 0;

}

Alternatively, you can enter in its parentheses the character that would be used in place of the empty spaces. The  character can be included between single-quotes. If no fill() was called previously, it displays the intended value "as is". If there was a previous call to fill(), the fill() immediately following would still keep the value in the parentheses of the previous fill(), then empty it. In other words, if there is another call to fill() without a value in its parentheses, no character would be displayed, until a new call to the fill(). Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue1 = 782500,

PropertyValue2 = 625560,

PropertyValue3 = 284782;

Console::Write(L"Property Value 1: ");

cout.width(8);

cout.fill();

cout << PropertyValue1 << endl;

Console::Write(L"Property Value 2: ");

cout.width(14);

cout.fill('#');

cout << PropertyValue2 << endl;

Console::Write(L"Property Value 3: ");

cout.width(10);

cout.fill();

cout << PropertyValue3 << endl;

Console::WriteLine();

return 0;

}

This would produce:

Property Value 1: 782500

Property Value 2: ########625560

Property Value 3: ####284782

 

8.2.3The dec, hex, and oct Operators

The most usual way to show an integer number on the console is to display it “as is”. To display a variable that has been declared and initialized as

int Value = 152;

you would write:

cout << Value;

The compiler would take care of displaying it. If you have an integer in decimal format and would like to display its equivalent hexadecimal representation, use the hex operator as follows:

#include <iostream>

using namespace std;

using namespace System;