2010-12-07 33 views
2

在下面的代碼中,cin僅提取非空白字符,因此我可以輕鬆計算出輸入字符數,大寫字母等等,這是用戶輸入的,容易忽略空白。使用While語句從控制檯讀取

#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"); 
} 

我的問題是,什麼構成no input?我的意思是使用while (cin >> c)我收到用戶輸入的字符並計算其中的數字,但直到什麼時候?什麼時候會停止?什麼會有資格作爲no input的while循環退出?

謝謝,

+0

我猜語言標籤丟失... – 2010-12-07 18:29:18

回答

3

直到你得到EOF字符。

如果STD輸入與鍵盤正在添加:

  • 在Windows上按Ctrl^Z
  • 在Unix上按Ctrl^d

如果操作系統被重定向一個文件到標準輸入,那麼它通常會在文件末尾發生。