这个WHILE循环为什么跳不出来

happycoco123 2014-06-04 12:03:49
我是想求一个输入一串单词,然后看哪个单词连续重复的次数多,然后输出出来,但是好像while循环跳不出来。
就是想求一个boy girl now now now,然后输出now循环了3次

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

int main()
{
cout<<"enter some words"<<endl;
string preword,curword,maxword;//前一个单词,当前输入的单词,重复出现的单词
int curtime=0,maxtime=1;//当前出现的次数,最大重复的次数
while(cin>>curword)
{
if(curword==preword)
++curtime;
else{
if(curtime>maxtime)
{
maxtime=curtime;
maxword=preword;

}
curtime=1;
}
preword=curword;
}
if(maxtime!=1)//如果有重复的单词
cout<<"the repeat word is"<<maxword<<"the time is"<<maxtime<<endl;
else//如果没有重复的单词
cout<<"there is no repeat times"<<endl;
return 0;
}
...全文
485 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
happycoco123 2014-06-04
  • 打赏
  • 举报
回复
引用 4 楼 anshiny 的回复:
int main() {
cout << "enter some words" << endl;
string preword, curword, maxword;//前一个单词,当前输入的单词,重复出现的单词
int curtime, maxtime;//当前出现的次数,最大重复的次数
int first = 1;
while(cin >> curword) {
if(first == 1){
curtime = 1;
maxword = curword;
maxtime = 1;
preword = curword;
first = 0;
} else if(curword == preword){
++curtime;
if(curtime > maxtime){
maxtime = curtime;
maxword = curword;
}
} else {
curtime = 1;
preword = curword;
}
}
if(maxtime == 1)
cout<<"no repeat"<<endl;
else
cout<<"the repeat word is "<<maxword<<", "<< maxtime << " times." << endl;
return 0;
}
我拿你的程序试了还是一样

就是输完了按ENTER,难道是我的编译器有问题
happycoco123 2014-06-04
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
Linux:Ctrl+D Windows:Enter,Ctrl+Z,Enter
没用啊,我就是输完几个单词之后按ENTER,还是继续输入单词,就没有执行最后的if和else的cout语句,我的程序有错吗?
anshiny 2014-06-04
  • 打赏
  • 举报
回复
int main() {
	cout << "enter some words" << endl;
	string preword, curword, maxword;//前一个单词,当前输入的单词,重复出现的单词
	int curtime, maxtime;//当前出现的次数,最大重复的次数
	int first = 1;
	while(cin >> curword) {
		if(first == 1){
			curtime = 1;
			maxword = curword;
			maxtime = 1;
			preword = curword;
			first = 0;
		} else if(curword == preword){
			++curtime;
			if(curtime > maxtime){
				maxtime = curtime;
				maxword = curword;
			}
		} else {
			curtime = 1;
			preword = curword;
		}
	}
    if(maxtime == 1)
		cout<<"no repeat"<<endl;		
	else
		cout<<"the repeat word is "<<maxword<<", "<< maxtime << " times." << endl;		
	return 0;
}
modyaj 2014-06-04
  • 打赏
  • 举报
回复
你原计划是怎么跳出来的!?
ztenv 版主 2014-06-04
  • 打赏
  • 举报
回复
调试一下就行了吧?
赵4老师 2014-06-04
  • 打赏
  • 举报
回复
Linux:Ctrl+D Windows:Enter,Ctrl+Z,Enter
xmlored 2014-06-04
  • 打赏
  • 举报
回复
while(cin>>curword)
这个难道不是没循环一次,到判断是否跳出循环时,又等待输入,这个表达式的直不会为0吧
anshiny 2014-06-04
  • 打赏
  • 举报
回复
按Enter,Ctrl+Z,Enter
happycoco123 2014-06-04
  • 打赏
  • 举报
回复
引用 11 楼 jianwen0529 的回复:
要么你加个判断退出的条件,要么你取行,然后用字符串流分离单词。 如果还有其他好方法,还忘楼下告知。
我知道原因了,Enter不是cin>>curword退出循环的条件,while还在等待下一个输入字符串。如果设成cin.get()!=‘\n’就可以了
happycoco123 2014-06-04
  • 打赏
  • 举报
回复
引用 12 楼 shlvshe00 的回复:
#include<iostream>
#include<string>
using namespace std;

void main()
{
	cout<<"enter some words"<<endl;
	string preword="",curword="",maxword="";//前一个单词,当前输入的单词,重复出现的单词
	int curtime=0,maxtime=1;//当前出现的次数,最大重复的次数
	do
	{
		cin>>curword;
		cout<<curword<<endl;
		if(curword==preword)
			++curtime;
		else
			curtime=1;

		if(curtime>maxtime)
		{
			maxtime=curtime;
			maxword=preword;
			cout<<maxword<<endl;
		}
		preword=curword;
	}while(cin.get()!='\n');
	if(maxtime!=1)//如果有重复的单词
		cout<<"the repeat word is"<<maxword<<"the time is"<<maxtime<<endl;
	else//如果没有重复的单词
		cout<<"there is no repeat times"<<endl;
}
谢谢,你的这个可以完成,难道是因为我的while条件不对吗?我在其他程序利用这个条件,都是Enter后就结束了,为什么这个不行
Pandorym 2014-06-04
  • 打赏
  • 举报
回复
while(cin>>curword)
这货是什么意思? 判断条件是个输入语句?
shlvshe00 2014-06-04
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;

void main()
{
	cout<<"enter some words"<<endl;
	string preword="",curword="",maxword="";//前一个单词,当前输入的单词,重复出现的单词
	int curtime=0,maxtime=1;//当前出现的次数,最大重复的次数
	do
	{
		cin>>curword;
		cout<<curword<<endl;
		if(curword==preword)
			++curtime;
		else
			curtime=1;

		if(curtime>maxtime)
		{
			maxtime=curtime;
			maxword=preword;
			cout<<maxword<<endl;
		}
		preword=curword;
	}while(cin.get()!='\n');
	if(maxtime!=1)//如果有重复的单词
		cout<<"the repeat word is"<<maxword<<"the time is"<<maxtime<<endl;
	else//如果没有重复的单词
		cout<<"there is no repeat times"<<endl;
}
幻夢之葉 2014-06-04
  • 打赏
  • 举报
回复
要么你加个判断退出的条件,要么你取行,然后用字符串流分离单词。 如果还有其他好方法,还忘楼下告知。
shlvshe00 2014-06-04
  • 打赏
  • 举报
回复
你没有跳出循环的语句啊
幻夢之葉 2014-06-04
  • 打赏
  • 举报
回复
回车的作用:通知cin已输入一行数据,cin可从输入行提取输入数据,依次赋给变量 并不表示输入流结束,尤其是你的while循环!!
赵4老师 2014-06-04
  • 打赏
  • 举报
回复
按Enter 和 按Enter,Ctrl+Z,Enter 不是一回事!
幻夢之葉 2014-06-04
  • 打赏
  • 举报
回复
while(cin>>curword) 一直等待输入。原因暂时解释不了,稍等!!

65,206

社区成员

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

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