当前位置:首页 » 多媒体相关

为什么while(cin>>word)达不大要求?


测试程序如下:  
  #include   <iostream>  
  #include   <string>  
  using   namespace   std;  
   
  int   main(){  
  string   word;  
  while(cin>>word)  
  cout>>word<<endl;  
   
  system("pause");  
  return   0;  
  }  
  ---------------------------  
  为什么会无限循环?  
  当输入到串尾后,循环判断应该为假的啊  
   
 

推荐阅读

  • 胜创SD内存256M 700元 [详细内容]
  • 北京:诺记6600 中端拍照集体降价 [详细内容]
  • “彩拍”桑达彩韵HT818登场 [详细内容]
  • [上海]台电256M闪盘298元促销 [详细内容]
  • 省电技巧 [详细内容]
  • 飞碟3D游戏手机 泛泰S3500正式上市 [详细内容]
  • 256MB的MP3只需665元-爱国者 先锋 B06 [详细内容]
  • 网友回答:
    网友:caoyu015

    按   ctrl+z   或者   ctrl+c   或者   ctrl+break才会返回false;

    网友:jack_wq

    while(cin>>word)  
      改为while(cin>>word)  
      {  
              if   (word   ==   \n)  
                      break;  
                你的代码!  
      }  
      否则死循环!

    网友:wizlian

    测试程序如下:  
      #include   <iostream>  
      #include   <string>  
      using   namespace   std;  
       
      int   main(){  
      string   word;  
      while(cin>>word)  
      cout>>word<<endl;---------此处笔误cou<<word<<endl;  
       
      system("pause");  
      return   0;  
      }  
      ---------------------------  
      为什么会无限循环?  
      当输入到串尾后,循环判断应该为假的啊  
      可是当用于文件的输入输出时确是可以的啊  
      //测试程序如下:我现在在网吧,中间有语法问题,还望大家见谅  
      #include   <fstream>  
      #include   <string>  
      using   namespace   std;  
       
      int   main(){  
      fstream   fp("1.txt",out);  
      string   word;  
      while(fp>>word)  
      cout<<word<endl;  
       
      system("pause");  
      return   0;  
      }  
       
     

    网友:njtu

    使用ctrl+z   就可以产生一个   eof使其结束。  
       
      如果只想输入一个字符串使用:  
       
      cin>>word;  
      就可以了,不用循环。

    .

    讨论区

    Login