EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  Input and Output in C++

Input and Output in C++ | C++ for EmSAT Achieve PDF Download

C++ offers libraries that provide numerous methods for performing input and output operations. In C++, input and output are carried out in the form of streams, which are essentially a sequence of bytes.

  • Input Stream: When bytes flow from a device (e.g. keyboard) to the main memory, the process is known as input.
  • Output Stream: When bytes flow in the opposite direction, i.e. from main memory to a device (e.g. display screen), the process is known as output.

Input and Output in C++ | C++ for EmSAT Achieve

Header files available in C++ for Input/Output operations are

  • iostream: The iostream header file stands for standard input-output stream and contains definitions of various objects, such as cin, cout, cerr, etc., which are used for performing input and output operations.
  • iomanip: The iomanip header file stands for input-output manipulators and contains declarations of various methods that are used for manipulating streams, such as setw, setprecision, etc.
  • fstream: The fstream header file is primarily used for working with file streams. It is used for handling the data that is read from a file as input or written to a file as output.
  • bits/stdc++: The bits/stdc++ header file includes every standard library in C++. In programming contests, using this file is a good practice, especially when time is crucial and you want to minimize the time spent on routine tasks. 

In C++, it is common practice to include the statement 'using namespace std;' after including the required header files. The rationale behind this practice is that all of the standard library definitions are enclosed within the 'std' namespace. Since the library functions are not defined at the global scope, we use the 'namespace std' statement to be able to use them without having to write 'STD::' before every line of code (e.g. STD::cout, etc.).

The two instances cout in C++ and cin in C++ of iostream class are used very often for printing outputs and taking inputs respectively. These two are the most basic methods of taking input and printing output in C++. To use cin and cout in C++ one must include the header file iostream in the program.
This article mainly discusses the objects defined in the header file iostream like the cin and cout.  

  • Standard output stream (cout): Usually the standard output device is the display screen. The C++ cout statement is the instance of the ostream class. It is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

#include <iostream>

using namespace std;

int main() {
// Declaring a character array 'string_array'

char string_array[] = "Hello World!";

// Printing the string value and a message to the console

cout << string_array << " - A simple program in C++";

return 0;

}

Output

Hello World! - A simple program in C++

  • This program uses the 'iostream' library to enable input and output streams and the 'using namespace std' statement to use the standard namespace. It declares a character array 'string_array' and initializes it with the value "Hello World!". 
  • The 'cout' statement is then used to print the value of the 'string_array' and a message to the console. Finally, the program returns the value 0 to indicate successful execution.

Time Complexity: O(1)
Auxiliary Space: O(1)

In the program given above, the '<<' operator is known as the insertion operator. It is used to insert the value of the 'sample' string variable followed by the string "A computer science portal for geeks" into the standard output stream 'cout'. The output is then displayed on the screen.

  • In C++, the standard input device is usually the keyboard. The 'cin' statement is an instance of the 'istream' class and is used to read input from the standard input device (which is usually a keyboard). 
  • To read inputs, the extraction operator '>>' is used along with the 'cin' object. This operator extracts the data from the 'cin' object that is entered using the keyboard.

#include <iostream>

using namespace std;

int main()

{

int userAge;

cout << "Please enter your age:";

cin >> userAge;

cout << "\nYour age is: " << userAge;

return 0;

}

Input

18

Output

Enter your age:
Your age is: 18

Time Complexity: O(1)
Auxiliary Space: O(1)

The program above prompts the user to input their age. The object cin is used to get input from the user, and the extraction operator(>>) is used to extract the age entered by the user, which is then stored in the integer variable age.

  • In C++, cerr is the un-buffered standard error stream that is used to output error messages. As it is un-buffered, cerr is ideal when it is necessary to display the error message immediately. 
  • Unlike cout, cerr does not have a buffer to store the error message, which means that the error message will not be saved in a file if the output is redirected.

#include <iostream>

using namespace std;

int main()

{

    // Outputting an error message using cerr

    cerr << "An error has occurred." << endl;

    return 0;

}

Output

An error has occurred.

Time Complexity: O(1)
Auxiliary Space: O(1)

  • buffered standard error stream (clog): This is also an instance of ostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled. or the buffer is not explicitly flushed (using flush()). The error message will be displayed on the screen too.

#include <iostream> 

using namespace std; 

int main()

{

    clog << "A warning occurred"; 

    return 0;

}

Output

A warning occurred

  • In the above C++ program, clog is a stream object that is used to output logging or informational messages. It is also an instance of the iostream class just like cout and cerr. However, unlike cerr, clog is buffered. 
  • This means that the messages written to clog are stored in a buffer and can be displayed later. clog is typically used for non-critical informational messages, warnings, or debugging statements. 
  • In this program, the statement clog << "A warning occurred"; writes the message "A warning occurred" to the clog object, which is then displayed on the console.

Time Complexity: O(1)
Auxiliary Space: O(1)

The document Input and Output in C++ | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

past year papers

,

Input and Output in C++ | C++ for EmSAT Achieve

,

Extra Questions

,

study material

,

mock tests for examination

,

Sample Paper

,

Semester Notes

,

Previous Year Questions with Solutions

,

Exam

,

Important questions

,

Objective type Questions

,

shortcuts and tricks

,

Input and Output in C++ | C++ for EmSAT Achieve

,

MCQs

,

video lectures

,

ppt

,

Summary

,

Free

,

pdf

,

practice quizzes

,

Input and Output in C++ | C++ for EmSAT Achieve

;