一段很简单的代码,但不知道为什么会死循环了,百思不得其解。解决方法

一段很简单的代码,但不知道为什么会死循环了,百思不得其解。。。
本帖最后由 derekrose 于 2013-12-25 11:17:32 编辑
/*
  this program is used for the statistic of the 
  average length of the input words
  
  input is some words separated by one or more blank;
  output is the average length of the input words
*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
   fstream f;
   f.open("test.txt" ,ios::in);
   
   if(f.fail()){
      cout << "no document exist" << endl;
      return 0;             
   }
   
   int num=0;   //denotes the number of words;
   double sum=0;   //denotes the number of letter in all the words
  
   while(!f.eof()){
      char c[100];
      cin.getline(c, 100, ' ');
      num++;
      sum+=strlen(c);                
      cout << sum << endl;
      cout << num << endl;
   }
   
   cout << sum/num << endl;
   
   f.close();
   
   system("pause");
   return 0;
}

------解决方案--------------------
f.getline(c,99,' ');
------解决方案--------------------
按 ctrl+z,看看能否结束循环?
------解决方案--------------------
你就都试试呗
------解决方案--------------------
百撕不得骑姐的时候就不妨再撕几下。。。一段很简单的代码,但不知道为什么会死循环了,百思不得其解。解决方法
------解决方案--------------------
Ctrl+Z 结束输入。