C++ Basic Input/Output

Whenever we have to take input or print output of any data type in C++ we must include a header file known as iostream (i.e input/output stream). Without iostream compiler will throw an error while taking input or printing an output.

Syntax:

  1. To print the Output in C++ the instance cout is used along with an insertion operator(<<).
  2. To take the Input in C++ the instance cin is used along with an extraction operator(>>).

Now after understanding the header files and (input/output) syntax, Let’s understand an important concept i.e Namespace standard.

Let’s understand namespace standards with the help of examples.

Example: Before using namespace standard

Code:

C++Code

#include <iostream>

int main() {
  std::cout << "Hello there, ";
  std::cout << "Welcome to TUF...!";
  return 0;
}

Output: Hello there, Welcome to TUF…!

From the above example we can see that, Every time when we are printing some output or taking some input we are writing the prefix std:: before every input or output statement which is creating an overhead.

So, to get rid of this problem we can use namespace std  (std is an abbreviation for the standard)

The members of the “std” namespace are cout, cin, endl, etc. So with the help of namespace std, we get rid of writing extra lines of codes.

How to use namespace standards in our program?

The answer is pretty simple here, We can use namespace standard in our program by simpling writing using namespace std; right below or after including header files.

Example: After using namespace standard

Code:

C++ Code

#include <iostream>

using namespace std;
int main() {
  cout << "Hello there, ";
  cout << "Welcome to TUF...!";
  return 0;
}

Output: Hello there, Welcome to TUF…!

Now let’s understand the input and output of different data types in C++.

Below is the code explaining how to take input and print output of different data types in C++

Code:

C++ Code

#include <iostream>

using namespace std;
int main() {
  // For integer input and output
  int num; // Integer number
  cout << "Enter a random number: ";
  cin >> num; // Taking integer input
  cout << "\nThe number you selected is: " << num; // Printing output

  // For float input and output
  float flt;
  cout << "\nEnter a random float number: ";
  cin >> flt;
  cout << "\nThe float you selected is: " << flt;

  //for String input and output
  string name;
  cout << "\nEnter your name: ";
  cin >> name;
  cout << "\nYour name is: " << name;

  // for array input and output
  int arr[5];
  cout << "\nEnter the elements of the array :";
  for (int i = 0; i < 5; i++) {
    cin >> arr[i]; // taking input of an array
  }
  cout << "The elements of array are: ";
  for (int i = 0; i < 5; i++) {
    cout << arr[i] << " "; // printing output
  }

  return 0;
}

Output: 

Enter a random number: 5

The number you selected is: 5

Enter a random float number: 5.12

The float you selected is: 5.12

Enter your name: TakeuUforward

Your name is: TakeUforward

Enter the elements of the array :1 2 3 4 5

The elements of array are: 1 2 3 4 5 

Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article