使用 While 语句从控制台读取

发布于 2024-10-06 11:25:50 字数 867 浏览 0 评论 0原文

在下面的代码中, cin 只提取非空白字符,因此我可以轻松计算出用户输入的输入字符数、大写字母等...,轻松忽略空格。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main( int argc, char** argv )
{
    int numberOfNonBlanks = 0;
    int numberOfUpperCase = 0;
    char c;
    while ( cin >> c )
    {
        ++numberOfNonBlanks;
        if ( ( c >= 'A' ) && ( c <= 'Z' ) )
        {
            ++numberOfUpperCase;
        }
    }
    cout << "Non blank characters: "  << numberOfNonBlanks << endl
         << "Upper case characters: " << numberOfUpperCase << endl;

    system( "PAUSE" );
}

我的问题是,什么构成无输入?我的意思是使用 while ( cin >> c ) 我接收用户输入的字符并计算任何内容的数量,但是直到什么时候?什么时候才会停止呢?什么条件会导致 while 循环退出无输入

谢谢你,

In the code below, cin only extracts non-blank characters, so I can easily work out the number of input characters, upper case, etc... that is enetered by the user, easily ignoring the blanks.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main( int argc, char** argv )
{
    int numberOfNonBlanks = 0;
    int numberOfUpperCase = 0;
    char c;
    while ( cin >> c )
    {
        ++numberOfNonBlanks;
        if ( ( c >= 'A' ) && ( c <= 'Z' ) )
        {
            ++numberOfUpperCase;
        }
    }
    cout << "Non blank characters: "  << numberOfNonBlanks << endl
         << "Upper case characters: " << numberOfUpperCase << endl;

    system( "PAUSE" );
}

My question is, what consitutes no input? I mean using while ( cin >> c ) I receive the user input characters and count the number of whatever, but until when? When would it stop? What would qualify as no input for the while loop to exit?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2024-10-13 11:25:50

直到得到 EOF 字符。

如果您的标准输入来自键盘:

  • 在 Windows 上按 ctrl^Z
  • 在 Unix 上按 ctrl^D

如果操作系统将文件重定向到标准输入,则通常会在文件末尾发生。

Until you get the EOF character.

If you std input is comming from the keyboard:

  • On Windows hit ctrl^Z
  • On Unix hit ctrl^D

If the OS is redirecting a file to the standard input then it will happen at the end of the file normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文