C + + read keyboard input (Cin/cin.getline ()/cin.get ()/cin.clear ())

Source: Internet
Author: User
1.cin

C + + uses CIN to easily read the characters entered by the keyboard, for example:

Test input
#include <iostream>							
int main ()								
{										
	using namespace std;
	const int size =;
	Char name[size];
	Char Pl[size];//program language

	cout<< "Enter Your Name:";
	cin>>name;
	cout<< "Enter your favorite program language:";
	cin>>pl;
	cout<< "Hello" <<name<< ", your favorite program language is" <<pl<<endl;
	return 0;
}


First, we enter only one word for each input, as follows:

But if we fill in more than two words in the first input:

It can be found that the second input has not yet had time to respond, it has been shown.

The reason is that CIN uses whitespace (spaces, tabs, and line breaks) to set the bounds of the string. This means that CIN reads only one word when reading a character array input, and after reading the word, CIN places the string in an array and automatically ends with a null character. In addition, CIN does not have a good control of the number of characters entered, that is, the number of input characters is greater than the size of the array is not handled. 2.cin.getline ()

Use Cin.getline () to read a row of data, based on the above situation.

The Cin.getline () function reads the entire row, using a newline character entered with the ENTER key to determine the end of the input. The function has two parameters. The first parameter is the name of the array that is used to store the input rows, the second is the number of characters to read (including the null character), and the Cin.getline () member function stops reading when it reads a specified number of characters or encounters a newline character

As follows:

cout<< "Enter Your Name:";
Cin.getline (name,size);
cout<< "Enter your favorite program language:";
Cin.getline (pl,size);
cout<< "Hello" <<name<< ", your favorite program language is" <<pl<<endl;

Now to get the input of multiple words:

The discovery is normal. It should be noted that Cin.getline () dropped the newline character. 3.cin.get ()

There are several variants of the Get () function, which have exactly the same parameters as Getline (), but the function no longer reads and discards line breaks (which means that the newline character is still in the input queue):

cout<< "Enter Your Name:";
Cin.get (name,size);
cout<< "Enter your favorite program language:";
Cin.get (pl,size);
cout<< "Hello" <<name<< ", your favorite program language is" <<pl<<endl;

Try to enter:

The discovery is not good because, after the first call, the line break will be retained in the input queue, so the first character seen at the second call is a line feed. So get thinks that it has reached the end of the line, and no readable content has been found. Get () (a variant without any arguments) can be read to take off a character, including a newline character. So you can:

Cin.get (name,size). get ();
Cin.get (pl,size). get ();

Can.

Note: Some C + + versions do not have a get () variant without parameters. There may be variants that have only char parameters. You can use the following instead:

Char Ch;cin.get (name,size). get (CH); 4. Read empty rows and other problems

What to do when getline () or get () reads an empty row. Typically the next input statement begins reading at the end of the previous read; However, when there is a blank line, when get () reads a blank row, the expiration bit (failbit) is set, which means that the next entry will be blocked, but you can restore the input with the following command: Cin.clear () cout< < "Enter your name:";

cout<< "Enter Your Name:";  
Cin.get (name,size). get ();  
if (!cin)
{
	cin.clear ();
	while (Cin.get ()!= ' \ n ')
	{
		continue
	}
} cout<< "Enter your favorite program language:";  
Cin.get (pl,size). get ();  
cout<< "Hello" <<name<< ", your favorite program language is" <<pl<<endl;
Another potential problem is that the input character may be longer than the allocated space. If the input line contains more characters than specified, the Get () and getline () will leave the remaining characters in the input queue, and getline () will also set the expiration bit and close the input.

cout<< "Enter Your name:";  
Cin.getline (name,size);
if (!cin)
{
cin.clear ();
while (Cin.get ()!= ' \ n ')
{
continue;
}
cout<< "Enter your favorite program language:";  
Cin.getline (pl,size);
cout<< "Hello" <<name<< ", your favorite program language is" <<pl<<endl

5. Mixed input strings and numbers

int age = 0;
Char Address[size] = {0};

cout<< "Enter your Age:";
cin>>age;
cout<< "Enter your Address:";
Cin.getline (address,size);
cout<< "Your age are" <<age << ", your address is" <<address<<endl;
There is a problem with the writing above, which will cause the second input to have no chance. The reason: When Cin reads the age, the line break generated by the return key is retained in the input queue, and after the Cin.getline () sees the line break, it is considered a blank line, and an empty string is assigned to the address array. The solution is to add cin.get () after cin>>age;;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.