私はこの最後のステップに到達するまで私は飛んでいたいくつかの宿題を持っています、そして今私は困惑しています、私は本当にいくつかの助けに感謝します。電話のワードジェネレータのヘルプC++
プロジェクトの前提は、電話番号を与えられた可能な単語のファイルを作成することです。ユーザーは '### - ####'の形式で番号を入力することになります。その後、コードはハイフンを引き出し、電話番号をwordGeneratorに送信します。私はこの点まですべてが機能することを知っています。時間が出てきたら、言葉のさまざまな可能性が私の問題を抱えています。ここに私の方法は次のようになります。
// function to form words based on phone number
void wordGenerator(const int * const n)
{
// set output stream and open output file
/* Write a declaration for an ofstream object called
outFile to open the file "phone.dat" */
ofstream outFile("phone.dat");
// letters corresponding to each number
/* Write a declaration for an array of 10 const char *'s
called phoneLetters. Use an initializer list to assign
each element of the array the corresponding string of
three letters. Use dummy characters for 0 and 1 */
const char * phoneLetters[] = {"###", "###", "ABC", "DEF", "GHI",
"JKL", "MNO", "PRS", "TUV", "WXY"};
// terminate if file could not be opened
/* Write code to check if the file was opened successfully,
and terminate if not */
if(!outFile)
{
cerr << "The file could not be opened";
exit(1);
}
int count = 0; // number of words found
// output all possible combinations
for (int i1 = 0; i1 <= 2; i1++)
{
for (int i2 = 0; i2 <= 2; i2++)
{
for (int i3 = 0; i3 <= 2; i3++)
{
for (int i4 = 0; i4 <= 2; i4++)
{
for (int i5 = 0; i5 <= 2; i5++)
{
for (int i6 = 0; i6 <= 2; i6++)
{
for (int i7 = 0; i7 <= 2; i7++)
{
/* Write a series of cascaded stream insertion
operations to output a set of seven letters
to outFile, followed by a space */
outFile << phoneLetters[i7 + 2] << phoneLetters[i6 + 2] << phoneLetters[i5 + 2] << phoneLetters[i4 + 2] << phoneLetters[i3 + 2] << phoneLetters[i2 + 2]
<< phoneLetters[i1 + 2] << ' ';
if (++count % 9 == 0) // form rows
outFile << '\n';
} // end for
} // end for
} // end for
} // end for
} // end for
} // end for
} // end for
// output phone number
outFile << "\nPhone number is ";
for (int i = 0; i < 7; i++)
{
if (i == 3)
outFile << '-';
outFile << n[ i ];
} // end for
/* Write a statement to close the ouput file */
outFile.close();
system("pause");
} // end function wordGenerator
残念ながら、私は、コードの骨格を与えられ、割り当てを完了するために空白を埋めるように言われました。コメントがブロックされている場所(/ * * /)はどこにでも記入してください。
私は可能な単語の正しいフォーマットを出力するために何をする必要があるのか分かりません。私はGoogleを検索しようとしましたが、私が見つけたすべての結果は、これを達成するためにはるかに単純な(私の意見では)switch文を使用しており、テンプレートに制約されています:(
すべてのヘルプは、
編集: 私はちょうど他のものを考えました。誰かがブロックの代わりにphoneLetters []の文字を反復する方法を理解する助けをすることさえできたら、それは重要なステップです 例: 「ABC」を印刷する代わりに電話番号の番号「2」を読み取った場合、すべての組み合わせについて「A」を印刷し、「B」に移動してから「B」に移動します。
編集:すべての
int main()
{
int phoneNumber[ 7 ] = { 0 }; // holds phone number
// prompt user to enter phone number
cout << "Enter a phone number (digits 2 through 9) "
<< "in the form: xxx-xxxx\n";
// loop 8 times: 7 digits plus hyphen;
// hyphen is not placed in phoneNumber
for (int u = 0, v = 0; u < 8; u++)
{
int i = cin.get();
// test if i is between 0 and 9
if (i >= '0' && i <= '9')
phoneNumber[ v++ ] = i - '0';
} // end for
wordGenerator(phoneNumber); // form words from phone number
return 0;
} // end main
、それが簡単になりますループのネストされた各文字 –
7をループへのあなたのため通常は、タスクを簡素化するために... – AJG85
@Msonicを愚かな何かをやっている良い兆候です。 –