如何打印以某个字符开头的单词数?

时间:2017-08-04 10:04:01

标签: c++ computer-science

这是一个介绍c ++类,提示符为:

打印以特定字符开头的单词数。让用户输入该字符。

虽然,我不知道该怎么做。

我是否使用解析字符串?我试过这个,因为他们检查字符串数据类型,但我不断收到错误,所以我把它拿出来并改成字符。我想学习如何处理“total_num”(以用户选择的字母开头的单词总数),我还需要一些帮助我的for循环。

所需输出的示例

  

用户输入:a   输出:“找到以”

开头的1270个单词      

用户输入:E
  输出:“找到以E开头的16个单词”

     

用户输入:#
  输出:“找到以#开头的0个单词”

(我认为这部分是非字母的)

数据来自名为dict.txt的文件,它是许多单词的列表。

以下是其中包含的一小部分示例:

D
d
D.A.
dab
dabble
dachshund
dad
daddy
daffodil
dagger
daily
daintily
dainty
dairy
dairy cattle
dairy farm
daisy
dally
Dalmatian
dam
damage
damages
damaging
dame

我的节目:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int NUM_WORD = 21880;//amount of words in file
struct dictionary { string word; };

void load_file(dictionary blank_array[])
{
  ifstream data_store;
  data_store.open("dict.txt");

  if (!data_store)
  {
    cout << "could not open file" << endl;
    exit(0);
  }

}

int main()
{
  dictionary file_array[NUM_WORD];
  char user_input;
  int total_num = 0;

  load_file(file_array);

  cout << "Enter a character" << endl;
  cin >> user_input;

  if (!isalpha(user_input))
  {
    cout << "Found 0 that begin with " << user_input << endl;
    return 0;
  }

  for (int counter = 0; counter< NUM_WORD; counter++)

  {
    if (toupper(user_input) == toupper(file_array[counter].word[0]));
    //toupper is used to make a case insensitive search
    {
      cout << "Found " << total_num << " that begin                                   with " << user_input << endl;
      //total_num needs to be the total number of words that start with that letter
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您可以采取一些措施来简化生活,例如:使用vector作为评论建议。

让我们来看看你的for循环。有一些明显的语法问题。

int main()
{
      dictionary file_array[NUM_WORD];
      char user_input;
      int total_num = 0;

      load_file(file_array);

      cout << "Enter a character" << endl;
      cin>>user_input;

      if(!isalpha(user_input))
      {
         cout << "Found 0 that begin with " << user_input << endl;
         return 0;
      }

      for(int counter = 0;counter< NUM_WORD; counter++)
      {
          if (toupper(user_input) == toupper(file_array[counter].word[0]));
           //                                                             ^no semi-colon here!
          //toupper is used to make a case insensitive search
          {
            cout<< "Found " << total_num << " that begin  with "<<
                                               user_input       << endl;
        //total_num needs to be the total number of words that start with that letter
          }

      }//<<< needed to end the for loop
}

让我们获得for循环吧。您希望在循环中计算匹配,然后在完成循环后进行报告。

      int total_num = 0;

      //get character and file

      for(int counter = 0;counter< NUM_WORD; counter++)
      {
           if (toupper(user_input) == toupper(file_array[counter].word[0]))
                              ^^^no semi-colon here!
          {
               ++total_num;
          }
      }
      cout<< "Found " << total_num << " that begin  with "<<                                                                                          user_input       << endl;