Programming With Integers - C and C++

Chapter chap2 section 13

In the C++ programming language the restriction that all declarations should appear before all executable statements is relaxed, this is why the Turbo C compiler (which is really a C++ compiler) happily accepted the version of the sum of two numbers program with the declaration of y after the input of x.

As well as the use of the library functions scanf() and printf() for input and output the C++ language supports an alternative form of input and output known as an IO stream. This uses the syntax

cout << value

for the output of a single value and the syntax

cin >> name-of-variable

for the input of a value to a variable. Note that, unlike scanf(), the IO stream input requires just the name of the variable without any preceding "&". The symbol ">>" is read as "gets from" and "<<" is read as "puts to". Before any IO stream operations are performed you must put the header line

#include <iostream.h>

in the program. The sum of two numbers program can be re-written in C++ in this form

//	Sum of two numbers program in C++

#include	<iostream.h>
main()
{
	int	x,y;
	cout << "Enter first number ";
	cin >> x;
	cout << "Enter second number ";
	cin >> y;
	cout << "The sum of the two numbers was ";
	cout << x+y;
	cout << "\n";
}
Note that there is no need to provide conversion information, the C++ compiler inspects the values and variables associated with IO stream input and output and arranges for the correct conversions. cout itself is a rather special sort of variable, it is actually of type IO-stream. More interestingly the expression

cout << x+y

is also of type IO-stream so multiple outputs can be written in this fashion

   cout << "The sum of x and y was " << x+y << "\n" 

To provide output width control there are several special functions associated with cout. These include cout.width() and cout.fill() . The function cout.width() sets the output field width for the next output operation only. It takes a single integer valued parameter. The function cout.fill() specifies the character to be used to fill the leading part of the next output field. It takes a single character constant as parameter. This consists of an actual constant enclosed in single quotes. You may be surprised that the names of these functions include a single dot, this does not mean that C++ allows dots within variable names, this means that the width and fill functions are associated with cout . The following two programs produce identical output. The first program uses C style output layout control, the second uses C++ style control.

main()
{
	int	x=25,y=50;
	printf("x = %5d, y = %05d\n",x,y);
}
The C++ version follows.
#include	<iostream.h>
main()
{
	int 	x=25,y=50;
	cout << "x = ";
	cout.width(5);
	cout.fill(' ');
	cout << x << ", y = ";
	cout.width(5);
	cout.fill('0');
	cout << y << "\n";
}
Both programs produced the following single line of output
x =    25, y = 00050
You will, almost certainly, think that the C version is simpler, it certainly involves a lot less coding. The advantages of the C++ approach will only become clear in more advanced and complex programs.


Exercises