高手帮忙看一下这个c++程序哪里有错?

来源:百度知道 编辑:UC知道 时间:2024/07/08 01:30:23
程序是用来统计输入的单词,输出单词数和对应的数目,为什么我的程序没法判断呢?
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

struct data
{
string str;
int count;
};

int main()
{
cout<<"Please enter a series of words"
" followed by end-of-file"<<endl;
vector<data> word1;
data x;
string y;
while(cin>>x.str)
{
x.str=y;
x.count=1;
word1.push_back(x);
}
typedef vector<data>::size_type vec_sz;
vec_sz size1 = word1.size();
vector<data> word2;
word2.push_back(word1[0]);
for(int i=1;i<size1;i++)
if(word2[0].str==word1[i].str)
word2[0].count++;
else
word2.push_back(word1[i]);
vec_sz size2 =word2.size();
for(int j=1;j<size2;j++)
for(int k=1;k<size1;k++)
if(word2[j]

while(cin>>x.str) 改为while(cin >> y)
这么简单个程序用得着这样写吗,练习一下STL也不用这样吧,写得太乱了

你在输入时候提示:Please enter a series of words followed by end-of-file
但是你在输入语句中去没有判断何时结束输入,所以改程序就陷入了无限的输入状态~~