C++ programming USES put to output a single character and cin input stream

  • 2020-05-05 11:33:51
  • OfStack

outputs a single character
with C++ stream member function put cout and the insert operator "<" are commonly used in programs < "Implementation output, cout stream in memory has the corresponding buffer. Sometimes users have special output requirements, such as only one character. In addition to the member functions for format control described above, the ostream class also provides a member function, put, dedicated to output a single character. Such as:


  cout.put('a');


The result of calling this function is to display a character a on the screen. The argument to the put function can be a character or the ASCII code for the character (or an integer expression). Such as


  cout.put(65 + 32);


The character a is also displayed because 97 is the ASCII code for the character a.

The put function can be called continuously in a single statement. Such as:


  cout.put(71).put(79).put(79). put(68).put('\n');


Display GOOD on the screen.

There is a string "BASIC" that requires them to be output in reverse order.


#include <iostream>
using namespace std;
int main( )
{
  char *a="BASIC";// Character pointer to 'B'
  for(int i=4;i>=0;i--)
   cout.put(*(a+i));         // The output starts with the last character 
  cout.put('\n');
  return 0;
}

The runtime prints
on the screen


CISAB

In addition to using the cout.put function to output a character, you can also use the putchar function to output a character. The putchar function is used in the C language and is defined in the stdio.h header. C++ retains this function, defined in the iostream header file.

You can also use the putchar function.


#include <iostream> // You can also use #include <stdio.h> And not the next line 
using namespace std;
int main( )
{
  char *a="BASIC";
  for(int i=4;i>=0;i--)
   putchar(*(a+i));
  putchar('\n');
}

The result is the same as before.

The member function put can be called not only with the cout stream object, but also with other stream objects of the ostream class.


C++ cin input stream details
Standard input stream is the flow of data from standard input devices (keyboards) to programs. Four stream objects cin, cout, cerr, clog are defined in the header file iostream.h (see: classes and objects related to C++ I/o). cin is the input stream, cout, cerr, clog are the output streams.

cin is an object of the istream class, which gets data from the standard input device (keyboard), and the variables in the program are extracted by the stream ">" > Extract data from the stream. Stream extractor "> > "When extracting data from a stream, whitespace, tab key, newline and other whitespace characters in the input stream are usually skipped.

Note: it is only after entering the data and then pressing the enter key that the line is fed into the keyboard buffer to form an input stream, extracting the operator "> > "To extract the data. Care needs to be taken to ensure that reading from the stream is done properly.

For example:


  int a,b;
  cin>>a>>b;


Enter
from the keyboard


  21 abc � 


The variable a extracts the integer 21 from the input stream, the extraction operation is successful, and the cin stream is in a normal state. However, when the variable b was about to extract an integer, it encountered the letter a, and the extraction operation apparently failed. At this point, the cin stream was put into an error state. Only in a normal state can data be extracted from the input stream.

When an invalid character is encountered or a file terminator is encountered (not a newline character, but the data in the file has been read), the input stream cin is in an error state, i.e., the data cannot be extracted normally. At this point all extraction operations on the cin stream terminate. In IBM PC and its compatibles, the end of the file is represented by Ctrl + Z. In the UNIX and Macintosh systems, the end of a file is represented by Ctrl + D. When the input stream cin is in an error state, if you test the value of cin, you will find that its value is false(false), that is, cin is a value of 0. If the input stream is in a normal state, the value of cin is true(true), that is, cin is a non-zero value. You can test the value of cin to see if the flow object is in a normal state and the extraction operation is successful. Such as:


  if(!cn) // flow cin In the state of pin output, unable to extract data normally 
    cout<<"error";

Test the truth value of cin to determine whether the flow object is in a normal state.


#include <iostream>
using namespace std;
int main( )
{
  float grade;
  cout<<"enter grade:";
  while(cin>>grade)// from cin Stream read data 
  {
   if(grade>=85) cout<<grade<<"GOOD!"<<endl;
   if(grade<60) cout<<grade<<"fail!"<<endl;
   cout<<"enter grade:";
  }
  cout<<"The end."<<endl;
  return 0;
}

Stream extract "> > "Continues to extract data from the stream (one floating-point number at a time). If it succeeds, it is given to grade, where cin is true and cin is false if it fails. If you type a file terminator, the data is complete.

Here's how it works:


  cout.put(65 + 32);
0

When a file terminator is encountered, the program ends. If the input data is


  cout.put(65 + 32);
1


Stream extract ">" > "Extract 100, assign grade, if statement processing. Then it encounters "/", which is considered an invalid character, and cin returns 0. Loop ends, output "The end."

Running this program on different C++ systems is somewhat different in the final handling. This is the result of running the program in GCC environment. If you run the program in VC++ environment (I use win7 system), when typing Ctrl + Z, you need to hit enter twice to print "The end."


Related articles: