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

Int main()

{

puts("Real Estate");

Console::WriteLine();

return 0;

}

The puts() function lacks (не имеет) the formatting possibilities of cout. It is destined to only display a single string. To display more complex strings, you can combine it with other operators or functions. The string to display on a puts() function can also originate from a declared and initialized variable. In this case, you would provide the name of the variable.

8.1.4Data Display With printf()

The printf() function is mostly used in the C language programs. By default, it is used to display a string on the console. To display a string using printf(), simply enclose it between double-quotes in the parentheses. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

printf("Real Estate");

Console::WriteLine();

return 0;

}

8.1.5Data Display With printf_s()

Besides printf(), you can also use printf_s() to display a string. Simply enter it in the parentheses. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

printf_s("Real Estate");

Console::WriteLine();

return 0;

}

This behaves the same way as the printf().

8.2Controlling and Formatting Data Output

8.2.1  The Width of Data Display

The cout class is equipped with width() to set the amount of space needed to display an item on the console. You can display the intended value with empty parentheses for width(). Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue = 782500;

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

cout.width();

cout << PropertyValue;

Console::WriteLine();

return 0;

}

This would produce:

Property Value: 782500

As an alternative, you can type an integer in the parentheses of width(). In order to display the intended value that could be a string, an integer or a floating number, the compiler would count the number of characters (for a char or a string) or the number of digits (for an integer or a float, including the period for decimals). If the value of the argument you supplied is less than or equal to the number of characters or digits, the compiler would ignore the number in the parentheses and display the intended value. If the value in the parentheses is greater than the number of characters or digits, the compiler would display the intended value using the total value in the parentheses but leaving empty spaces on the left of the intended value.

The value to display comes after calling width(). If you are displaying only one value, the scenario above would apply straight forward. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue = 782500;

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

cout.width(16);

cout << PropertyValue;

Console::WriteLine();

return 0;

}

This would produce:

Property Value: 782500

To display various values, you can call width() for each and they can use different width values as in the following example:

#include <iostream>

using namespace std;

using namespace System;