Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在句子c+中找到一个单词+;_C++_String_Getline - Fatal编程技术网

C++ 在句子c+中找到一个单词+;

C++ 在句子c+中找到一个单词+;,c++,string,getline,C++,String,Getline,这个代码应该说明一个单词是否出现在一个句子中。当我在声明字符串的地方插入句子和单词时(例如:string s=“猫在桌子上”string p=“table”程序说单词在句子中),代码可以工作,但是,对于getline,for循环永远不会开始,它总是说单词不在句子中 请帮忙,我不知道该怎么办 #include <iostream> #include <string> using namespace std; int main () { string s; strin

这个代码应该说明一个单词是否出现在一个句子中。当我在声明字符串的地方插入句子和单词时(例如:string s=“猫在桌子上”string p=“table”程序说单词在句子中),代码可以工作,但是,对于getline,for循环永远不会开始,它总是说单词不在句子中

请帮忙,我不知道该怎么办

#include <iostream>
#include <string>

using namespace std;

int main () {

string s;

string p;

string word;

bool found = false;

int sl = s.length();

int beg = 0;

int pl = p.length();

cout << "sentence: ";

getline(cin, s);

cout << "word: ";

getline(cin, p);

for(int a = 0; a<sl; a++)
{
    if(s[a]== ' ')
{
    word = s.substr(beg, a-beg);
    if (word== p)
    {
      found = true;
      break;
    }
    beg = a+1;
    }
 }

 if (found== true)
 {
    cout <<"word " << p << " is in a sentence " << s;
 }

 else
{
 word = s.substr(beg);

 if (word== p)
    {
      found = true;
    }

if(found == true)
{
    cout <<"the word " << p << " is in the sentence " << s;
}

     else
   {
   cout <<"the word " << p << " isn't in the sentence " << s;
   }

  }
}
#包括
#包括
使用名称空间std;
int main(){
字符串s;
字符串p;
字符串字;
bool-found=false;
int sl=s.length();
int=0;
int pl=p.length();

cout获取输入字符串后,然后使用
length()
查找长度,否则您不会获取字符串的实际大小

getline(cin, s);
getline(cin, p);

int sl = s.length();

int pl = p.length();

在输入代码由“代码> GETLILN())/代码>拆分单词后,可以使用<代码> String Strue >,这是一个内置C++函数,如:

#include <sstream>
#include <iostream>
using namespace std;

int main(){
    string arr;
    getline(cin, arr);

    stringstream ss(arr);
    string word;
     
    while(ss >> word){
       // your desired strings are in `word` one by one
       cout << word << "\n";
    }
}
#包括
#包括
使用名称空间std;
int main(){
字符串arr;
getline(cin,arr);
stringstream ss(arr);
字符串字;
while(ss>>word){
//您所需的字符串逐个出现在'word'中

您是否需要在
getline
之后计算
sl
字符串
s
。否则
sl
只是默认字符串的大小,即0,因此您永远不会进入循环。