Will the Following Cin Statement Automatically Stop Reading Input When the Array Is Filled?

Reading time: thirty minutes | Coding time: 10 minutes

C++ uses the concept of streams to perform I/O operations. A stream is a sequence of bytes in which grapheme sequences are 'flown into' or 'flow out of'. Streams operate as the intermediate between the plan and the I/O devices thus helping the programmers to achieve device independent I/O operations. The two types of streams in C++ are:

  • input stream
  • output stream

IOstreams-1


  1. iostream - iostream stands for standard input-output stream. This header file contains definitions to objects like cin, cout, cerr etc.
  2. iomanip - iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.
  3. fstream - This header file describes the file stream. This header file is used to handle the data beingness read from a file as input or information being written into the file every bit output.

The objects existence discussed in this commodity are cin and cout . They are the most commonly used objects for taking inputs and printing outputs.

Standard output stream (cout)

cout is an object of the class ostream. The standard output device is the display screen. The characters are inserted into the output stream. The insertion operator (<<) is used forth with cout to insert values into the stream so that the output device (monitor) tin can utilise them.

Usage

          #include<iostream> using namespace std; int principal()  {      int Hullo=10;     cout<<"Howdy"; //This prints the text "Hello" on the display screen.     cout<<Hello ; // Prints the contents of the variable 'How-do-you-do'(10) on the screen.     cout<<x;     //Prints number 10 on the screen }                  

Multiple insertion operations (<<) can exist chained in a single statement:

          cout<<"Welcome"<<"to"<<"OpenGenus!";                  

Output

          Welcome to OpenGenus!                  

Chaining multiple insertions is useful to mix literals and variables in a single statement:

          cout<<"My name is "<<name<<" and I am "<<age<<"years quondam";                  

Assuming the variable name is initialised with 'Alice' and age with 25, the output of the previous statement would be :

          My proper noun is Alice and I am 25 years sometime                  

The cout operator does non add linebreaks add the terminate of an output. To add together a linebreak, the newline character ('\n') has to be used.

          cout<<"This is sentence 1.\northward"; cout<<"This is sentence 2.";                  

Output

          This is sentence 1. This is sentence two.                  

The same thing tin can also be achieved using endl manipulator instead of the new-line character.

          cout<<"This is sentence ane."<<endl; cout<<"This is sentence 2.";                  

This produces the same output equally above.

Annotation - While both the newline graphic symbol and endl produce the aforementioned output, the key difference betwixt the two is that endl causes a flushing of the output buffer every fourth dimension it is called, whereas '\n' does non.

Output Formatting :

Formatting the output is an of import part of any awarding, to improve the user interface and enhance the readability.

Formatting in C++ is done through the employ of I/O manipulators. Most of these manipulators are bachelor in iostream and iomanip.

The standard manipulators are :-

  • endl - Introduces a line break on the output stream similar to '\n'. The instance was seen above .

  • setw() - The setw() manipulator sets the width of the field assigned for the output. It takes the size of the field (in number of characters) as statement.

Instance :

          #include<iostream.h> #include<iomanip.h> void main() {     cout<<setw(6)<<"X"; }                  

Output

          _ _ _ _ _X                  

When chaining multiple insertion operations, the setw() manipulator does non stick from one operation to the next. To set up the width for each functioning, setw() has to exist repeated.

Example :

          cout<<setw(3)<<one<<"\northward"<<setw(3)<<2<<"\n"<<setw(three)<<three;                  

Output :

          _ _1 _ _2 _ _3                  
  • setprecision() - The setprecision() manipulator sets the total number of digits to exist displayed when floating-point numbers are printed.

Example :

          cout<<setprecision(5)<<22.0/7;                  

Output

          three.1415                  

The setprecision() manipulator can also exist used to set up the number of decimal places to exist displayed. To practice this, you take to set an ios flag.

          cout.setf(ios::stock-still); cout<<setprecision(5)<<22.0/7;                  

Output

          three.14159                  

Note - Unlike setw() , all the subsequent couts after setprecision() retain the precision set with the last setprecision(). That means setprecision() is "sticky".

  • Additional IOS flags :
flag significant
left left-justify the output
right right-justify the output
showpoint displays decimal point and trailing zeros for all
all floating point numbers, even if decimal places
are non required
showpos display a plus sign before positive values
scientific brandish floating point numbers in scientific note

* Standard input stream (cin) :

cin is an object of the grade istream . The standard input device is they keyboard. When the user enters characters via the keyboard, the keycode is placed in the input stream. The extraction operator ( >> ) is used with cin to extract values from the stream.

Usage

          #include <iostream> using namespace std;  void master () {   int i;   cout << "Please enter an integer value: ";   cin >> i;   cout << "The value you entered is " << i;   }                  

Output

          Please enter an integer value: 12 The value you entered is 12                  

Like the insertion operator, extraction operator can as well exist chained in a single argument.

          cin >> 10 >> y;                  

This is the equivalent of

          cin>>x; cin>>y;                  

In both cases, the user must provide two values separated past any blank separator (infinite, tab or newline).

Using cin with strings

cin can be used to read strings the same way it is used with other primal data types. However, cin always considers whatsoever white space character as terminating the value being extracted. So extracting a cord with cin means extracting only a discussion and non a sentence.

To overcome this, a function chosen getline() can be used that takes the stream (cin) every bit beginning statement, and the string variable as 2nd.

Instance :

          void primary() {   string myname;   cout <<"Enter your name: ";   getline (cin, myname);   cout<<"Welcome "<<myname; }                  

Output

          Enter your name: Alice Cooper Welcome Alice Cooper                  

Another way to utilise getline with cin :

cin.getline(char buffer, int length) - Reads a stream of characters into the string buffer. It stops when it has read length-1 characters or when information technology finds an end-of-line grapheme ('\n').

Example :

          void main() { 	char accost[xx];	 	cout << "Address: "; 	cin.getline(accost, twenty);     cout<<"You alive in "<<address; }                  

Output

          Address: Baker Street, UK Yous live in Bakery Street, United kingdom                  

Autonomously from getline(), cin tin besides exist used with other member functions. Some of them include :

funtion meaning
cin.get(char &ch) Reads an input character and shop it in ch
cin.read(char buffer, int northward) Reads n bytes from the stream into the buffer
cin.ignore(int n): Ignores the adjacent due north characters from the stream
cin.eof(): Returns a nonzero value if terminate of file is reached

With this article at OpenGenus, you must have the complete thought of Cin and Cout in C++. Enjoy.

Will the Following Cin Statement Automatically Stop Reading Input When the Array Is Filled?

Source: https://iq.opengenus.org/cin-and-cout-in-cpp/

0 Response to "Will the Following Cin Statement Automatically Stop Reading Input When the Array Is Filled?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel