VC++初学者 请教高手ctrl+z的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 12:12:23
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a,b;//当前的单词和前一单词
string c;//重复次数最多的单词
int m=0,n=1;//数和重复次数的最大值
cout<<"enter some words(ctrl+z to end):"<<endl;
while(cin>>b){
if(b==a)
++m;
else{
if(m>n){
n=m;
c=a;
}
m=1;
}
a=b;
}
if(n!=1)
cout<<'"'<<c<<'"'
<<"repeated for"<<n
<<"times."<<endl;
else
cout<<"there is no repeated word."<<endl;
return 0;
}
当运行这个程序时 我输入了一连串的string对象 而且在后面写了ctrl+z回车 但是还是没有结果 不知道问什么 请高手赐教!!!!急……

while(cin>>b):cin是标准输入输出流中的输入对象,cin>>b的值不能为假,所以while是死循环,while以后的语句执行不到;
你写的ctrl+z 只是一个要输出用的字符串,并不能起到热键的作用,要启用热键比较烦,控制台中一般定义一些简单的退出标示就可以了,比如:
...
while(1){
cin>>b;
if(b=="ctrl+z") break;
if(b==a)
...
输入字符串"ctrl+z"的时候,跳出循环。

先记下来

其实很简单