Topic :C++ Basic Input and Output in C++ Programming Language:

Basic Input & Output:

The C++ Standard Libraries Provide an Extensive set of Input/Output capabilities which we will see in subsequent chapters. In this Chapter, we will discuss the very basic Common I/O operations required for C++ Programming.

The C++ Standard Libraries Provide an Extensive set of Input/output Capabilities which we will see in the subsequent chapter. in this chapter, we will discuss very basic and most common I/O operations required for C++ programming.

C++ I/O occurs in streams, which are Sequences of bytes. if bytes flow from a device like a keyboard, a disk drive, or a Network connection etc. to main memory, this is called input operation and if bytes flow from main Memory to a device like a display screen, a Printer, a disk drive, or a Network connection, etc, this is called Output Operation.



Input/Output Library Header Files:

There are following header Files important to C++ Programs. its just about basic input and output operation. we will discuss about Libraries in our Library Tutorial.



<Iostream>

Iostream is used to work with standard input stream, the Standard output stream, the unbuffered standard error stream and the buffered standard error stream.



<Iomanip>

This file declares useful for Performing formatted I/O with so-called parameterized stream manipulators, Such as Setw and Setprecision. ( We will discuss about these Manipulators in our C++ Manipulators Tutorial.



<fstream>

This file declares services for user-controlled file Processing. we will discuss it in detail in Files Operation Tutorial.



The Standard Output Stream (cout):

The Predefined object cout is an Instance of ostream Class. the cout object is said to be "Connected to " the standard output device, which usually is the display srceen. The cout is used in conjunction with the stream insertion operator, which is written as "<<" which are two less-than signs are shown in the following example.



#include<iostream>
using namespace std;
main()
{
    string name=InfoBrother";
    cout<<"welcome to: ";
    cout<<name;
    return 0;
}


The "<<" Operator inserts the data that follows it into the stream that Precedes it. in the Above example, it inserted the literal string Welcome to , and the Name InfoBrother into the standard output stream cout. Notice that the sentence in the first statement is enclosed in Double quotes (") because it is a string literal, while in the next one name is not enclosed in double quotes. The double Quoting is what makes the difference; when the text is enclosed between them, the text is Printed literally; when they are not closed in quotes, the text is interpreted as the identifier of a variable, and its value is Printed instead, same like the Name. the name is an variable, so its value InfoBrother will printed, instead of Name



Multiple Insertion Operation (<<)

The insertion Operator (<<) may be used more than once in a Single statement as shown in the given example:



#include<iostream>
using namespace std;
main()
{
    string name="InfoBrother";
    cout<<"welcome to: " <<name;
    return 0;
}


insertion operation, cout, c++ : infobrother

The Standard input Stream (cin):

The Predefined object Cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as ">>" which are two greater-than signs as shown in the following example:



#include<iostream>
using namespace std;
main()
{
    int age;
    cout<<"How old are you"<<endl;
    cin>>age;
    cout<<"you are "<<age<<" year old:"<<endl;
    return 0;
}


When the above code is compiled and executed, it will prompt you to enter Your Age. You will enter your age then hit enter to see the result something as shown in "Check output"

in the above example the first statement declares a variable of type int. and the second line of code is an prompt. which will ask you about your age. and the third line of code will allow you to enter your age and through cin, it will stored your entered value in the "age" Variable. This Operation makes the Program wait for input for cin; generally, this means that the Program will wait for the user to enter something with the keyboard. in this case, Note that the characters introduced using the keyboard are only transmitted to the program when the Enter key is Pressed. Once .



Multiple Extraction Operation (>>)

The Extraction Operator (>>) may be used more than once in a Single statement as shown in the given example:



cin>>name>>age;


This will be equivalent to the following two statements:



cin>>name;
cin>>age;


Extraction operation, cin, infobrother

The Standard error Stream (cerr):

The Predefined object cerr is an instance of ostream class. The cerr object is said to be attached to the standard error device, which is also a display screen.but the object cerr is un-buffered and each stream is also used in conjunction with the stream insertion operator as shown in the following example:



#include<iostream>
using namespace std;
main()
{
    char str[] = "sorry for inconvenience...";
    cerr << "Error : " << str << endl;
    return 0;
} 


The Standard log Stream (clog):

The Predefined object clog is an instance of ostream class. The clog object is said to be attached to the standard error device, which is also a display screen.but the object cerr is buffered that mean each insertion to clog cause its output to be held in a buffer unitl the buffer or until the buffer is flushed.
The clog is also used in conjunction with the stream insertion operator as shown in the following example.



#include<iostream>
using namespace std;
main()
{
    char str[] = "sorry for inconvenience...";
    cerr << "Error : " << str << endl;
    return 0;
} 


You would not be able to see any difference in court, cerr, and log with these small examples, but while writing and executing big problems then difference will be clear to you. so it's a good programming practice to display error messages using the cerr stream and while displaying other log messages then clog should be used.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement