5.5,5.6

Source: Internet
Author: User

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

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.