FORMATTING INPUT AND OUTPUT IN C++

FORMATTING INPUT AND OUTPUT IN C++
Any computer program must have a way of communicating with other devices. Input devices and output devices. This leads us to the concept of streams- a channel of communication from on point to another or a medium through which data flows.
Recall our favorite preprocessor directive:

#include “iostream.h”

What is in the iostream.h
By including the iostream library, you are in effect creating a stream between the your program and the other input and output devices making it possible for you to use the stream object(eg cout and cin). The iostream header file defines a collection of functions that are used for both input and output.
The iostream library define four stream objects that are automatically created when program execution begins. We have used two of these: the cin and cout. The two other standard streams are cerr and clog. But we would take a closer look at the cin and the cout objects.
• cout prints results in an “output window”
• cerr prints error messages in an “output window” (often the same
window as used by cout); in principle, this allows the programmer to create
error reports that are separate from the main output.
You don’t have to ‘declare’ these streams, if your program says it wants to use the
iostream library then the necessary declarations get included. Initially though, you’ll be using just the standard iostream objects: cout for output and cin for input.

for output to cout look like the following:

int aCounter;
double aVal;
char Message[] = "The results are: ";
...
cout << Message;
cout << aCounter;
cout << " and ";
cout << aVal;
...

The code fragments starts with the declarations of some variables, just so we
have something with values to output.:

int aCounter;
double aVal;
string Msg = "The results are: ";

The declaration

int aCounter;

defines aCounter as a variable that will hold an integer value. The declaration

double aVal;

specifies that there is to be a variable called aVal that will hold a double precision real number. (C and C++ have two representations for real numbers – float and double. double allows for greater accuracy.)

The declaration

string Msg
defines a string object called Msg
cout << Message;
cout << aCounter;
cout << " and ";
cout << aVal;

These are the requests to cout asking it to output some values.

The Iostream Operators and Manipulators
So far we know and have been using the two main iostream operators:

  • the insertion operator <<;
  • the extraction operator >>;

insertion means insert something you have to the stream (so that it would flow to wherever)
extraction means extract data from the stream for use by your program(say a data flowing from the keyboard entered by the user).
Let’s look at a simple program and see how they can be used.

//******************************************************
// Program to Compute area and circumference of a circle
// of circle given radius
// Programmer: Kindson Munonye
// Date Created; 15th February 2007
// Modified by: Jackie Franklin
//******************************************************

#include “iostream.h”
#include “string.h”
#include “stdlib.h”
int main() {
float area;
float circumference;
float Radius; // Radius of circle
cout &lt;&lt; "Circle radius (real number)? "; cin &lt;&lt; Radius;
area = 3.1415 * Radius * Radius;
circumference = 3.1415 * 2 * Radius;
cout &lt;&lt; "Area of circle is " ;
cout &lt;&lt; area;
cout &lt;&lt; " Circumference of the circle is ";
cout &lt;&lt; circumference;
return 0;
}

Type this program in the compiler and run it to see the output ( or copy and paste it into the compiler). What did you notice? Everything is jam-packed on one line! The ouput is shown in the following figure.

 

Let’s look at the line:

cout&lt;&lt;”Circle Radius(Real Number):;

This line displays the string constant “Circle Radius (real number)” to the standard output stream (the screen) with no formatting i.e the text is displayed starting from the next available space after the last output.
Recall your escape sequence “\n”. Including this character causes a newline to be inserted such that the next output to be displayed starts from the next line.

cout&lt;&lt;”Circle Radius (real number) \n”; //insert new line
cout&lt;&lt;”Circle Radius (real number)&lt;&lt;”\n”; //achieves the same thing
cout&lt;&lt;”Circle Radius (real number) &lt;&lt;endl; //achieves the same thing

The insertion operator << is always associated with the cout object (never use the extraction operator >>; with cout!

The next line says:

cin &gt;&gt; Radius;// a float value

This line reads data from the standard input (keyboard). It does the following:
• suspends the program execution and waits for the user to enter a value.
• gets whatever the user types in
• saves it in the memory location (variable) named Radius
The variable Radius is not enclosed in quotes like the preceding line because Radius is not a string , only strings are enclosed in quotes.

Using more than one data type in a single output statement.
It is also possible to us a single cout statement to output a number of variables and constants. This is shown in the program below.

//******************************************************
// Program to illustrate the use of the cout objects to
//format output
// Programmer: Kindson Munonye
// Date Created; 15th February 2007
// Modified by: Jackie Franklin
//******************************************************

#include "iostream.h"
#include "string.h"
#include "stdlib.h"
int main() {
char surname[20],firstname[20];
int age,height;
cout&lt;&lt;"Enter surname "; cin &gt;&gt; surname;
cout &lt;&lt; "Enter Firstname" ; cin &gt;&gt; firstname;
cout &lt;&lt; "Enter age"; cin &gt;&gt; age;
cout &lt;&lt; "Enter height(in feet)"; cin &gt;&gt; height;
cout &lt;&lt; " My name is "&lt;&lt; firstname[20]&lt;&lt;”\t” &lt;&lt; surname[20] &lt;&lt;"\n"&lt;&lt; " I am" &lt;&lt; age&lt;&lt;"years old"&lt;&lt;"\n"&lt;&lt;"My height is" &lt;&lt; height &lt;&lt;"\n"&lt;&lt;"Good Bye";
return 0;
}