习惯不好,一直是在写字板上写程序,没编译

是呀,要输出长度最大的,我加了#include<string>后,运行结果如下:
Some states use income tax in addition to sales tax to raise the their revenues.
最长单词:Some
单词长度:4
最长单词:states
单词长度:6
最长单词:states
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:revenues.
单词长度:9
我倒是想出一个简单的算法  不过程序有问题  请大家帮忙改下吧 
#include<iostream>
#include<string>
using namespace std;
void main()
{
 int max=0;//单词的最大长度,初始值为0
 char m[20];//保存在循环结束后的单词
 char s[20];//保存输入的字符串
 char c[1];//保存字符串中的每个字符
 char *temp=new char[];//保存每个单词
 gets(s);//输入字符串
 for(int i=0;s[i]!='\0';i++)
 {
  
  c[0]=s[i];
  if(c[0]==' ')
  {
   if(strlen(temp)>max)//判断单词长度,并保存单词
   {
    max=strlen(temp);
    strcpy(m,temp);
    delete []temp;
    char *temp=new char[];
   }
  }
  strcat(temp,c);//把字符串中的单词一个字母一个字母的保存到temp字符数组中
 }
 delete []temp;
 cout<<"the longest word is "<<m<<endl;
 cout<<"length is "<<max<<endl;
}

#include <iostream>
#include <cstdlib>
#include <cctype>  //isalpha()
#include <vector>
using namespace std;
int main()
{
    vector<pair<string,int> > text;  //hold all word 
    string word;
    int max=0;
    int lengh;
    char ch;
    
    cout<<"please input text :"<<endl;
        //read line......
    while(1)
    {
        cin.get(ch);
        
        if(isalpha(ch))      //is a letter ?
            word+=ch;
        else
        {
            if(word!="")
            {
                lengh=word.size();
                if(max < lengh)
                    max = lengh;
                text.push_back( pair<string,int>(word, lengh) );
                word="";
            }
        }
        
    if(ch=='\n') break;
    }
    
    //output.............
    int size=text.size();
    cout<<"longest  word : \n";
    for(int i=0; i<size; i++)
    {
        if(text[i].second==max)
            cout<<'\t'<<text[i].first<<'\n';
    }
    cout<<"lengh    : "<<max<<endl;
    system("pause");
    return 0;
}
有简单的方法吗?   
[此贴子已经被作者于2006-1-8 21:52:22编辑过]

我没VC++编译器,看看行不
#include <iostream>
#include <cstdlib>
#include <cctype>  //isalpha()
#include <vector>
using namespace std;
int main()
{
    vector<pair<string,int> > text;  //hold all word 
    string word;
    const string null_s;
    int max=0;
    int lengh;
    char ch;
    
    cout<<"please input text :"<<endl;
//read line......
    while(1)
    {
        cin.get(ch);
        
        if(isalpha(ch))      //is a letter ?
            word+=ch;
        else
        {
            if(word!=null_s)
            {
                lengh=word.size();
                if(max < lengh)
                    max = lengh;
                text.push_back( pair<string,int>(word, lengh) );
                word="";
            }
        }
        
    if(ch=='\n') break;
    }
    
    //output.............
    int size=text.size();
    cout<<"longest  word : \n";
    for(int i=0; i<size; i++)
    {
        if(text[i].second==max)
            cout<<'\t'<<text[i].first<<'\n';
    }
    cout<<"lengh    : "<<max<<endl;
    system("pause");
    return 0;
}
[此贴子已经被作者于2006-1-9 14:32:55编辑过]
