5.5 looping and text input
Use Cin to enter
1#include <iostream>2 intMain ()3 {4 using namespacestd;5 Charch;6 intCount=0;7cout<<"Enter the Characters,enter #to quit";8Cin>>ch;9 while(ch!='#')Ten { Onecout<<ch; Acount++; -Cin>>ch; - } thecout<<endl<<count<<"characters Read"; - return 0; -}
The program enters characters, with # as the closing tag, and counts the number of characters before the #.
CIN ignores spaces and line breaks, so there are no statistics and no spaces displayed.
Use Cin.get (CH) to read the next character of the input, even if it is a space, and assign it to the variable ch.
1#include <iostream>2 intMain ()3 {4 using namespacestd;5 Charch;6 intCount=0;7cout<<"Enter the Characters,enter #to quit";8Cin.Get(CH);9 while(ch!='#')Ten { Onecout<<ch; Acount++; -Cin.Get(CH); - } thecout<<endl<<count<<"characters Read"; - return 0; -}
End of File condition: Detect end of file (EOF)
member functions EOF () and Fail ()
CTRL + Z indicates the end.
1#include <iostream>2 intMain ()3 {4 using namespacestd;5 Charch;6 intCount=0;7cout<<"Enter the Characters,enter #to quit";8Cin.Get(CH);9 while(Cin.fail () = =false)Ten { Onecout<<ch; Acount++; -Cin.Get(CH); - } thecout<<endl<<count<<"characters Read"; - return 0; -}
Cin.clear () can clear the EOF marker so that the input continues.
5.5,5.6