美文网首页
5.18 cin.fail() == false 循环输入输出字

5.18 cin.fail() == false 循环输入输出字

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

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

/********************************
 * 程序来源 :C++ primer plus
 * 章    节 :5.5
 * 名    称 :5.18 textin3.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(cin.fail() == false) {  //检测到EOF cin.fail()==true,cin.eof() == true 
        cout << ch;
        count ++;
        cin >> ch;
    } 
    cout << endl << count << " Charactors read.\n";
    return 0;
}


运行结果:

 
/**********************************
 *            程序输出            *
 **********************************
Enter chararcters;Enter '#' to quit:
my name is sunset
mynameissunsetmy love c++ java and c#
mylovec++javaandc#^Z

32 Charactors read.

--------------------------------
Process exited after 32.07 seconds with return value 0
请按任意键继续. . .
**********************************/ 

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

相关文章

  • 5.18 cin.fail() == false 循环输入输出字

    程序来源 :C++ primer plus章 节 :5.5名 称 :5.18 textin3.cpp功...

  • 写给2019年的8个关键词

    【关键词1】:输出 输入输出,角色不同。 角色不同,效率不同。 输入输出,完美循环。 多次循环,加速成长。 【关键...

  • Java 基础 Day05

    循环结构 循环结构的三种方式: while循环 while (循环条件 true/false) { 循...

  • 检测一个对象是否为空

    只要有一个元素,就会进入for循环,执行 return false 语句; // 返回false ...

  • python例题

    for循环、if 、乘方 、continue 、除法 输入输出、while循环 日期操作 批量打印 函数定义,递...

  • until循环

    #until 循环(false时进入循环) 和while 循环相反i=1until (($i > 1))doech...

  • js跳出forEach循环

    forEach循环中可使用return false终止本次循环,但不能想for那样使用break来跳出整个循环。(...

  • js:$each、forEach、for 和 return

    $.each jq提供的遍历方法: $each 跳出循环 return false、跳过循环 return tru...

  • Java面试题库一

    1.循环结构是如何结束循环的?(1).循环条件返回false(2).在循环体内,一旦执行到break,跳出循环注:...

  • 从零学java day2 第四章 流程控制 控制循环结构

    1.使用break结束循环 某些时候要在某种条件出现时强行终止循环,而不是等到循环条件为false时才推出循环。此...

网友评论

      本文标题:5.18 cin.fail() == false 循环输入输出字

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