2017-08-04 4 views
0

イントロC++クラスの場合、プロンプトは次のように表示されます。特定の文字で始まる単語の数を印刷するにはどうすればよいですか?

特定の文字で始まる単語の数を表示します。ユーザーにその文字を入力させます。

しかし、私はこれを行う方法がわかりません。

解析文字列を使用しますか?私は文字列のデータ型を検査したのでこれを試しましたが、エラーが発生しているので、取り出して文字に変更しました。私は "total_num"(ユーザが選んだ文字で始まる単語の総数)のやり方を学びたいと思っています。また、私のforループの助けが必要です。
出力:で所望の出力の

ユーザタイプE
出力:「16ワードことを発見に

ユーザタイプ "で始まる1270個の単語を発見" Eで始まる」で

ユーザーのタイプ:#
出力: "#で始まる0言葉"

発見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 
    } 
    } 
} 
+0

load_fileがstd :: vector ()を返すことをお勧めしますか?あなたは複数の奉仕のためにユーザーに尋ねないので、私はキャラクターを最初に尋ね、その後ファイルを通過してカウントする。 – UKMonkey

答えて

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; 
関連する問題