while (CIN>>STR) exits the dead loop

Source: Internet
Author: User

Today in the practice of suddenly found this problem, Baidu's feeling is quite common, so remember!

//Title Description////Write a program that accepts a hexadecimal numeric string and outputs the decimal string for that value. ////Input Description://Enter a hexadecimal numeric string. //////Output Description://The decimal string that outputs the numeric value. #include<iostream>#include<string>using namespacestd;voidFunstringstr) {    if(Str.empty ()) {return; }    intj=0; intresult=0, n=1; //while (str[i++]! = ' x '); //read the next character in X    intLen = Str.size ()-1;  while(str[len]!='x')//if there's no X, it goes down, it's wrong.    {        if(str[len]<='Z'&&str[len]>='A') J= Str[len]-'A'+Ten;//converting characters to shaping        Else{J= Str[len]-'0'; } result= J*n +result; N=n* -; Len--; if(len==0)        {             Break; }} cout<< result <<Endl;}intMain () {stringstr;//While (CIN>>STR)//the dead loop actually passed.//say in Windows input CTRL + Z is back, sometimes press two times,//{    //Fun (str); //}     //cin >> str; //While (str!= "")//to solve the dead loop, seemingly no//{    //Fun (str); //cin >> str; //}    //istream& getline (istream& is, string& str, char delim); //The return value is a IStream type and cannot be used as a judgment condition for while exit//istream& getline (istream& is, string& str); //Return Value//The same as parameter is.getline (CIN,STR);  while(str! =" ")//Resolve dead Loop, spacebar to exit{fun (str);    Getline (CIN, str); }    return 0;}////#include <iostream>//#include <vector>//#include <string>//using namespace std;//int main ()//{//vector<string> Vect;//string str;//While (Cin >> str)//    {//if (str = = "0")//this way you can also exit//Break ;//vect.push_back (str);//    }//return 0;//}

Explanations are:

in C++primer, the author often uses the while (Cin>>word) similar syntax, but how to end, there may be doubts. Sometimes it takes two shortcuts to end a loop, and sometimes it only takes one loop. The actual phenomena in Unix systems are:
* When input is "string, enter, Ctrl+d", jump out of the loop;
* When the input is "string, Ctrl+d, enter" The time, will not jump out of the loop;
* When input is "string, Ctrl+d, enter, Ctrl+d", jump out of the loop;
* When input is "string, Ctrl+d,ctrl+d", jump out of the loop;
The actual behavior in the Windows system is being investigated.
The specific reasons are explained as follows:
The input (CIN) buffer is a row buffer. When you enter a string of characters from the keyboard and press ENTER, the characters inode are first sent to the input buffer for storage. Each time the ENTER key is pressed, CIN detects if the input buffer has readable data.
CIN also checks to see if the keyboard has a CTRL + Z or CTRL+D key pressed as the end-of-flow flag, which is checked in two ways: block and non-blocking.
The blocking check method refers to whether the previous Ctrl + Z key Press is checked only after the return press is pressed, and the non-blocking style refers to the way in which the response is pressed immediately after ctrl+d. If a character has been entered from the keyboard before pressing Ctrl+d, the CTRL+D function is equivalent to a carriage return, which means that the characters are sent to the input buffer for reading, and ctrl+d no longer functions as a stream terminator. If there is no keyboard input before pressing Ctrl+d, then CTRL+D is the signal for the end of the stream.
The blocking style has one feature: it is possible to detect if there is a CTRL + Z press before the carriage return. Another feature is that if there is readable data in the input buffer, CTRL + Z is not detected (because there is data to be read and cannot be considered at the end of the stream). It is also important to know that CTRL + Z produces not a normal ASCII value, that is, it produces not a character, so it will not be stored in the input buffer as other characters entered from the keyboard.
Windows system generally uses the blocking check Ctrl + Z, Unix/linux system generally adopts non-blocking inspection ctrl+d.

while (CIN>>STR) exits the dead loop

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.