Home > Back-end >  The cin. The get () reads a carriage return
The cin. The get () reads a carriage return

Time:04-04

 
Int main () {
Char str0 [30]="\ 0";
While (true) {
Cin. Get (str0, 30);
Cin. Ignore (128), '\ n');
Cout & lt; <"Success" & lt; }
return 0;
}

This is the wrong code:
Will die when enter only enter circulation, namely the infinite output "success", they do not have the opportunity to enter,
Cin. Ignore () should have cleared the cache area, why cin. The get () also will go to read?
If use cin> And does not recognize the enter, only when input something in that line, run to cout
For big science about

CodePudding user response:

Gold can gather, the code without almighty,
I carefully grope for summarizing the advantages and disadvantages of memory it,
Their good qualities and use, its not good and avoid,
That's all.

CodePudding user response:

Such modification, and can realize the requirement of the building Lord, but to enter Ctrl + Z end input, always couldn't refund the input state:
 # include & lt; Iostream. H> 

Int main () {
Char str0 [30]="\ 0";
While (true) {
Cin. Get (str0, 30);//function will line feeds, carriage returns operators such as reserve in the input queue, and state of cin will be set to false
Cin. The clear ();//will be state of cin qing 0
Cin. Sync ();//let the flow of corresponding data synchronization
//cin. Ignore (128), '\ n');
Cout & lt; <"Success" & lt; }

return 0;
}

Suggest to change this:
 int main () {
Char str0 [30]="\ 0";
While (cin> Str0) {//Ctrl + Z, the input end of the
Cout & lt; <"Success" & lt; }

return 0;
}
//cin> Str0; A carriage return don't read in the buffer
//cin. The get (); A carriage return into the buffer

Cin cin cin. The get (). The clear () some detailed usage, can CSDN or online search,
  • Related