#include <iostream.h>
#include <string>
using namespace std;
int main()

// 初始化一个string 对象用来存放搜索的结果
string search_word = "sunnic";
// 把一个bool 变量初始化为false
bool found = false;
//bool founds =true;
string next_word;
while ( cin >> next_word)
if ( next_word == search_word )
found = true;
// ...
// 缩写, 相当于: if ( found == true )
if ( found )
cout << "ok, we found the word\n";
else cout << "nope, the word was not present.\n"; return 0;
}
while ( cin >> next_word)
这句老是通不过,如何解决?

解决方案 »

  1.   

    string next_word[50];
    给它一个空间,要不你就动态分配
      

  2.   

    兄弟!是不是忘了加break了!!!!!!!!!!!!!!!!!
      

  3.   

    #include <iostream.h>
    #include <string>
    using namespace std;
    int main()
    {
                    // 初始化一个string 对象用来存放搜索的结果
            string search_word = "sunnic";
            // 把一个bool 变量初始化为false
            bool found = false;
            bool founds =true;
            string next_word;
            while ( cin >> next_word){
                    if ( next_word == search_word ){
                            found = true;
                            break;
                    }
            }
            // ...  // 缩写, 相当于: if ( found == true )
            if ( found )
                    cout << "ok, we found the word\n";
            else cout << "nope, the word was not present.\n";
            return 0;
    }
    我是在LINUX下g++编译的!!!!!!!
      

  4.   

    string没有实现插入运算符,所以不能用cin>>....
    可以用
    char next_word[100];
    while(cin>>next_word)
    {
    if(next_word == search_word ) found = true;
    ....
    }
    但cin>>next_word仍然是返回输入流,while(cin>>next_word)将不会终止
    所以应该另外寻找出口。
    另外next_word的缓冲区大小要注意检查...