Home

Data Input/Output, Reading, and Formatting

 

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.

General 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
Press any key to continue . . .

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.

Data 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>

using namespace std;
using namespace System;

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.

Data 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;
}

Data 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().

 

Previous Copyright © 2006-2007 FunctionX, Inc. Next