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

8.Data Input/Output, Reading, and Formatting

8.1 Displaying Data

8.1.1 Introduction

The values and expressions used in C++ are displayed using the cout class. To make the displaying of data more realistic, the cout class is configured to handle or format data to any desired result. While the cout class is defined in the iostream file, some other files provide other extensive techniques for displaying data to the console. The C language also is equipped with other formatting functions used for the same purpose. The ability to create a program made of mixed C, C++, C++/CLI languages enhance these formatting possibilities.

8.1.2General Display With cout, Write(), and WriteLine()

The ostream and the Console classes are used to display items on the console screen. The ostream class is from the ios library and commands all techniques of data output to C++ console applications. The cout we have used so far is a “child” of the ostream library. Using it, you you can display almost any type of value. For example, it can be used to display:

  • A word or sentence, called a string:  

#include <iostream>

using namespace std;

using namespace System;

int main()

{

cout << "Property Type: ";

Console::WriteLine(L"Single Family");

return 0;

}

  • A character:  

#include <iostream>

using namespace std;

using namespace System;

int main()

{

cout << "Property Type: ";

cout << 'S';

cout << '-';

Console::WriteLine(L's');

return 0;

}

  • A short integer:  

#include <iostream>

using namespace std;

using namespace System;

int main()

{

cout << "Number of Stories: ";

cout << 3;

cout << '\n';

Console::WriteLine(1);

return 0;

}

  • A floating number:  

#include <iostream>

using namespace std;

using namespace System;

int main()

{

cout << "Property Value: ";

cout << 355680;

cout << '\n';

Console::WriteLine(355680);

return 0;

}

Instead of displaying a value just using the cout or the Console classes, you can first declare and initialize a variable before displaying its value.

Besides the cout class and the extraction operator << used to display a character, cout can be used to display a (one) character. To do this, type cout followed by a period and followed by put(). The character must be included between parentheses and single-quotes. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

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

cout.put('S');

Console::WriteLine();

return 0;

}

This would produce:

Property Type: S

If the character is initialized from a variable, type the name of the variable between parentheses:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

char PropertyType = 'S';

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

cout.put(PropertyType);

Console::WriteLine();

return 0;

}

The character can also come from a variable requested from the user.

8.1.3Data Display With puts()

The C++ compiler ships with files that provide many functions that can be used to control how data displays on the screen. One of these files is the stdio.h header.

puts() is used to display a string on the console. To use it, type a double-quoted in its parentheses. Here is an example:

#include <iostream> // должно быть stdio.h?

using namespace std;

using namespace System;