c++: 用cin读string遇到的问题

woshidong 2008-09-08 01:23:12
程序功能:输入一串字符串,计算出总的词的个数和the出现次数数和a出现次数

问题:进入 while(cin>>buf){后不会跳出循环
String.cpp
1 #include<iostream>
2 #include"String.h"
3 using namespace std;
4
5 int main(){
6 int aCnt=0, theCnt=0,wdCnt=0;
7 String buf, the("the");
8 while(cin>>buf){
9 ++wdCnt;
10 cout<<buf<<'\n';
11
12 if(buf==the||buf=="The") ++theCnt;
13 for(int ix=0;ix<buf.size();++ix){
14 switch(buf[ix]){
15 case 'a':case 'A': ++aCnt;break;
16 default:break;
17 }
18 }
19 }
20 cout<<"\n"<<wdCnt<<"\na:"<<aCnt<<"\nthe:"<<theCnt<<endl;
}


String.h
#include<cstring>
#include<iostream>
#include<cassert>
#include<iomanip>
using namespace std;
class String;
inline istream& operator>>(istream&, String&);
inline ostream& operator<<(ostream&, const String&);
class String{
public:
String();
String(const char*);
String(const String&);
~String();
String& operator=(const String&);
String& operator=(const char*);
bool operator==(const String&);
bool operator==(const char*);
char& operator[](int);
int size(){
return _size;
}
char* c_str(){
return _string;
}
private:
int _size;
char *_string;
};
inline String::String(){
_size=0;
_string=0;
}
inline String::String(const char* str){
if(!str){
_size=0;
_string=0;
}
else{
_size=strlen(str);
_string=new char[_size+1];
strcpy(_string, str);
}
}
inline String::String(const String& rhs){
_size=rhs._size;
if(!rhs._string) _string=0;
else{
_string=new char[_size+1];
strcpy(_string, rhs._string);
}
}
inline String::~String(){
delete [] _string;
}
inline String& String::operator=(const char *s){
if(!s){
_size=0;
delete [] _string;
_string=0;
}
else{
_size=strlen(s);
delete [] _string;
_string=new char[_size+1];
strcpy(_string, s);
}
return *this;
}
inline String& String::operator=(const String& rhs){
if(this!=&rhs){
delete [] _string;
_size=rhs._size;
if(!rhs._string) _string=0;
else{
_string=new char[_size+1];
strcpy(_string, rhs._string);
}
}
return *this;
}
inline bool String::operator==(const char* s){
return strcmp(_string, s)?false:true;
}
inline bool String::operator==(const String& rhs){
if(_size!=rhs._size) return false;
return strcmp(_string, rhs._string)?false:true;
}
inline char& String::operator[](int elem){
assert(elem>=0&&elem<_size);
return _string[elem];
}
inline istream& operator>>(istream &io, String& s){
const int limit_string_size=4096;
char inBuf[limit_string_size];
io>>setw(limit_string_size)>>inBuf;
s=inBuf;
return io;
}
inline ostream& operator<<(ostream &io, String& s){
return io<<s.c_str();
}


...全文
436 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshidong 2008-09-08
  • 打赏
  • 举报
回复
谢谢!解决了!
帅得不敢出门 2008-09-08
  • 打赏
  • 举报
回复
我想你的程序意思有问题
你说说你要实现什么?
while里面问题复杂.
一直输入String而又没有设置结束条件,你如何能够让程序自行退出while?
wangdeqie 2008-09-08
  • 打赏
  • 举报
回复
如果楼主用的是VC DEV C++的话,用ctl+z,来修改cin的标志位,使其无效,从而终止输入操作
太乙 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lann64 的回复:]
ctl+z 和 ctrl+D 都对,一个是MS下windows和dos用的,一个是unix linux用的
[/Quote]

嗯,受教了,以前用过linux,难怪第一反应是ctrl+d!哈哈~~
lann64 2008-09-08
  • 打赏
  • 举报
回复
ctl+z 和 ctrl+D 都对,一个是MS下windows和dos用的,一个是unix linux用的
qqwx_1986 2008-09-08
  • 打赏
  • 举报
回复
ctrl+z
恩好像是
luxiaoxun 2008-09-08
  • 打赏
  • 举报
回复
while(cin>>buf)会检测输入状态是否正确
ctrl+z结束输入
太乙 2008-09-08
  • 打赏
  • 举报
回复
说错了,是ctrl+z

heihei

http://topic.csdn.net/u/20080326/11/2a832ffd-9e1a-44cd-ad81-449ad3f570c5.html
Ijiuweiyaohul 2008-09-08
  • 打赏
  • 举报
回复
你这样写while(cin<<buf)貌似不是很安全啊
你出的是什么错啊?
太乙 2008-09-08
  • 打赏
  • 举报
回复
问题:进入 while(cin>>buf){后不会跳出循环

不是程序的问题,试试按按ctrl+d!

因为你 的 buf是string类型的!

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧