注册 登录
编程论坛 C++教室

```我弄了一个小时了``还是不知道那里错了``入门的`

死了都要C 发布于 2007-12-19 19:13, 968 次点击
头文件和相关声明已经给出

题目要求是将输入的文本行里的  单词  放到vector里面
我的问题是不关怎么输入结果都是 There is no text.
我不知道我那里弄错了```我弄了很久都找不出来那里错了``
帮帮忙啊``谢谢``


int main(void)
{
    cout << "Please enter text ( Ctrl+z to end ) :" << endl ;
   
    string str ;   
    vector<string> vstr ;
   
    while ( cin >> str )
    {
          string :: size_type len = 0 ;
          bool add = true ;
         
          while( len != str.size() )
          {            
               if ( isalpha(str[len]) == true )
                  ++len ;
               else   
               {
                  len = str.size() ;
                  add = false ;  
               }
          }
         
          if ( add == true )
             vstr.push_back(str) ;
    }
   
    if ( vstr.size() == 0 )
    {
       cout << "There is no text ." << endl ;
       return 1 ;
    }

    for ( vector<string> :: size_type len = 0; len != vstr.size(); ++len )
    {
        cout << vstr[len] << " " ;
    }
   
    cout << endl ;
    return 0 ;
}
3 回复
#2
aipb20072007-12-19 19:34
isalpha(str[len]) == true

谁告诉你isalpha的返回是bool类型?
#3
phb7112007-12-19 22:09

     
isalpha
    

  原型:extern int isalpha(int c);
  
  用法:#include <ctype.h>
  
  功能:判断字符c是否为英文字母
  
  说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
  
  举例:

      // isalpha.c
      
      #include <syslib.h>
      #include <ctype.h>
      #include <stdio.h>

      main()
      {
        int c;
        
        clrscr();        // clear screen
        printf("Press a key");
        for(;;)
        {
          c=getchar();
          clrscr();
          printf("%c: %s letter",c,isalpha(c)?"is":"not");
        }
        return 0;    // just to avoid warnings by compiler
      }
#4
死了都要C2007-12-26 17:38
书上说:当c为英文字母a-z或A-Z时,返回true。

  而bool类型的值就是true 或 false``所以我就定义了bool来做为标记``

   现在知道返回的是一个非0的int值了```

  谢谢``
1