使用 While 语句从控制台读取
在下面的代码中, 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直到得到 EOF 字符。
如果您的标准输入来自键盘:
如果操作系统将文件重定向到标准输入,则通常会在文件末尾发生。
Until you get the EOF character.
If you std input is comming from the keyboard:
If the OS is redirecting a file to the standard input then it will happen at the end of the file normally.