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