我正在尝试计算我的一系列推文中有多少特定的主题标签。我越来越疯狂了。这是我的代码:

// returns the number of tweets with the given hashtag
int MicroBlog::GetNumHashtag(const string Hashtag)
{
   string Choice;
   int NumHashtag;

   cout << "Which hashtag would you like to check for in the tweets? " << endl;
   cin >> Choice;
   cout << "The hashtag you have chosen is: " << Choice << endl;
   for (int i = 0; i < MAX_TWEETS; i++)
   {
      if (blog[i].GetHashtag() == Choice)
         NumHashtag++;
   }
   cout << "There are " << NumHashtag << " hashtags in the blog. " << endl;
   return NumHashtag;

}

最佳答案

这行:

int NumHashtag;

导致不确定的行为。

这包括位于分配给NumHashtag的内存对象中的随机数。您唯一要做的就是初始化对象:
int NumHashtag = 0;

关于c++ - 在C++数组中检查主题标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23419438/

10-11 12:43