Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++;can';无法获取strtok的用户输入_C++_Input_Strtok_Sentence - Fatal编程技术网

C++ C++;can';无法获取strtok的用户输入

C++ C++;can';无法获取strtok的用户输入,c++,input,strtok,sentence,C++,Input,Strtok,Sentence,我在C++中不断遇到这段代码的问题: #include <stdio.h> #include <string.h> #include <iostream> #include <string> using namespace std; int main () { string words[25]; int i = 0; char * word; cout << "Input a phrase, no capit

我在C++中不断遇到这段代码的问题:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string words[25];
   int i = 0;
   char * word;
   cout << "Input a phrase, no capital letters please.";
   char phrase[100] = "this is a phrase";
   word = strtok (phrase, " ,.");

   while (word != NULL)
   {
      i++;
      words[i] = word;
      cout << words[i] << " ";
      word = strtok (NULL, " ,.-");
      int g = 0;
   }
   cout << endl << endl;

   int g = 0;
   while (g < i)
   {
      g++;
      char f = words[g].at(0);
      if ((f == 'a') || (f == 'e') || (f == 'i') || (f == 'o') || (f == 'u') || (f == 'y'))
      {
         words[g].append("way");
         cout << words[g] << " ";
      }
      else
      {
         words[g].erase (0,1);
         cout << words[g] << f << "ay" << " ";
      }

   }
   cout << endl;
   system("PAUSE");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串[25];
int i=0;
字符*字;

CUT< P>在C++中进行终端I/O的首选方法是流。使用<代码> STD::CIN < /C> >和<代码> STD::GETLION<代码>函数,从输入输出读取字符串。
std::string input;
std::getline(std::cin, input);

之后,您可能想摆脱<代码> Strtok> /Cord>,并在这里了解如何在C++中进行字符串标记。

< P>在C++中进行终端I/O的首选方式是流。使用<代码> STD::CIN < /代码>和 STD::GETLION<代码>函数从输入输出读取字符串。
std::string input;
std::getline(std::cin, input);

之后,你可能想摆脱<代码> Strtok> /Cord>,看看这个如何理解C++中的字符串标记化。

< P>你想要的是:

char phrase[100];
fgets(phrase, 100, stdin);

虽然在注释和其他答案中所说的是,C++中使用C字符串函数,但这非常奇怪。除非指定了任务或其他东西,否则不应该这样做。 而是使用:

string input;
getline(cin, input);
要标记化,可以执行以下操作:

string token;
size_t spacePos;
...
while(input.size() != 0)
{
    spacePos = input.find(" ");
    if(spacePos != npos)
    {
        token = input.substr(0, spacePos );
        input = input.substr(spacePos  + 1);
    }
    else
    {
        token = input;
        input = "";
    }

    // do token stuff
}
或者,跳过所有的爵士乐:

string token;

while(cin >> token)
{
    // Do stuff to token
    // User exits by pressing ctrl + d, or you add something to break (like if(token == "quit") or if(token == "."))
}
你想要的是:

char phrase[100];
fgets(phrase, 100, stdin);

虽然在注释和其他答案中所说的是,C++中使用C字符串函数,但这非常奇怪。除非指定了任务或其他东西,否则不应该这样做。 而是使用:

string input;
getline(cin, input);
要标记化,可以执行以下操作:

string token;
size_t spacePos;
...
while(input.size() != 0)
{
    spacePos = input.find(" ");
    if(spacePos != npos)
    {
        token = input.substr(0, spacePos );
        input = input.substr(spacePos  + 1);
    }
    else
    {
        token = input;
        input = "";
    }

    // do token stuff
}
或者,跳过所有的爵士乐:

string token;

while(cin >> token)
{
    // Do stuff to token
    // User exits by pressing ctrl + d, or you add something to break (like if(token == "quit") or if(token == "."))
}

什么是代码的最小片段会导致问题?请发布,这样我们就不必阅读整个程序。换句话说,我想你的问题是,“我如何读取用户输入到一个字符数组在C++中?”如果你正在编写C++,使用<代码> Strutok>代码,你几乎肯定会出错。什么是代码的最小片段会导致问题?请发布,这样我们就不必阅读整个程序。换句话说,我想你的问题是,“我如何在C++中读取用户输入到字符数组中?”如果你正在编写C++,并且使用<代码> Strtok 你几乎肯定会做错事。你的语句不正确地指出<代码> GETLION>代码>是 STD::String 的成员,但是你的代码正确地显示它不是。@ BejayLimDyLee谢谢。Field.你的语句不正确地表明<代码> GETLION>代码>是一个成员。
std::string
,但您的代码正确地显示它不是。@BenjaminLindley谢谢。修复了。很好的解释和示例!+1Ty,现在我终于没有把需要编辑的东西弄糟了:py您的上一个示例有点不正确:(注意与输出相比的输入)--这更好:fgets真的很好用,我只需要从最后一个单词中删除最后一个字符,因为它存储了回车符。而且我的老师也不知道stdtok的每个人都有什么问题。谢谢你的帮助。你可以计算
input.find(“”)
如果
if语句
为真,则执行3次。也许你有很好的理由这样做,但对我来说,这似乎是一种浪费。很好的解释和示例!+1今天,我终于没有把需要编辑的内容搞糟了:p我们的上一个示例有点不完整:(注意与输出相比的输入)--这更好:fgets真的很好用,我只需要从最后一个单词中删除最后一个字符,因为它存储了回车符。而且我的老师也不知道stdtok的每个人都有什么问题。谢谢你的帮助。你可以计算
input.find(“”)如果
if语句是真的,则执行
3次。也许你有理由这样做,但对我来说,这似乎是一种浪费。