Reading from the Console using the While Statement
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;
}
开发者_C百科 }
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,
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.
精彩评论