美文网首页
5.19 (ch = cin.get()) != EOF 循环输

5.19 (ch = cin.get()) != EOF 循环输

作者: 壹顾倾城 | 来源:发表于2020-01-16 21:06 被阅读0次

    程序来源 :C++ primer plus
    章 节 :5.5
    名 称 :5.19 textin4.cpp
    功 能 :循环输入输出字符串
    开发时间 :2020-1-9
    版 本 :v1.0
    运行测试 :通过
    C++11支持:执行工具-编译选项 输入
    -std=c++11 并打钩

    /********************************
     * 程序来源 :C++ primer plus
     * 章    节 :5.5
     * 名    称 :5.19 textin4.cpp
     * 功    能 :循环输入输出字符串
     * 开发时间 :2020-1-9
     * 版    本 :v1.0
     * 运行测试 :通过
     * C++11支持:执行工具-编译选项 输入
     *            -std=c++11 并打钩
     *******************************/
    #include <iostream>
    
    using namespace std;
    
    int main() {
        char ch;
        int count;
        cout <<"Enter chararcters;Enter '#' to quit:\n";
        cin >> ch;    //can't read space, Enter
        while((ch = cin.get()) != EOF) {  //test fo end-of-file
            count ++;                     //test win10 ctrl+z + Enter for EOF
            cin >> ch;
        } 
        cout << endl << count << " Charactors read.\n";
        return 0;
    }
    
    

    运行结果:

      
    /**********************************
     *            程序输出            *
     **********************************
    Enter chararcters;Enter '#' to quit:
    my name is tiaya
    my love c++ c java and c#
    ^Z
    
    18 Charactors read.
    
    --------------------------------
    Process exited after 27.27 seconds with return value 0
    请按任意键继续. . .
    **********************************/
    
    

    声明:本代例子码源自教材非原创,是笔者的学习笔记,仅用于学习交流。

    相关文章

      网友评论

          本文标题:5.19 (ch = cin.get()) != EOF 循环输

          本文链接:https://www.haomeiwen.com/subject/eadqzctx.html