Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.

(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table:
![14901|284x220](upload://1B0Of3gczrDO3ANjuOpJn5Oq4l0.png)

Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indicated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls.
Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e.,“TAKEOUT”).Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtapositions of letters. It is possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to “PETCARE.”

```
#include
#include
#include
#include
using namespace std;

void wordGenerator( const int * const );

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 until we've read 7 valid values;
// ignore everything not between 2 and 9
for ( int v = 0; v < 7; )
{
int i = cin.get();

// test if i is between 2 and 9
if ( i >= '2' && i <= '9' )
phoneNumber[ v++ ] = i - '0';
} // end for

wordGenerator( phoneNumber ); // form words from phone number
} // end main

// function to form words based on phone number
void wordGenerator( const int * const n )
{
// set output stream and open output file
ofstream outFile( "phone.txt" );

// letters corresponding to each number
const string phoneLetters[ 10 ] = { "", "", "ABC", "DEF", "GHI",
"JKL", "MNO", "PRS", "TUV", "WXY" };

// terminate if file could not be opened
if ( !outFile )
{
cerr << "\"phone.txt\" could not be opened.\n";
exit( 1 );
} // end if

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++ )
{
outFile << phoneLetters[ n[ 0 ] ][ i1 ]
<< phoneLetters[ n[ 1 ] ][ i2 ]
<< phoneLetters[ n[ 2 ] ][ i3 ]
<< phoneLetters[ n[ 3 ] ][ i4 ]
<< phoneLetters[ n[ 4 ] ][ i5 ]
<< phoneLetters[ n[ 5 ] ][ i6 ]
<< phoneLetters[ n[ 6 ] ][ i7 ] << ' ';

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
} // end function wordGenerator
```
Enter a phone number (digits 2 through 9) in the form: xxx-xxxx
568-9876
Contents of phone.txt
JMTWTPM JMTWTPN JMTWTPO JMTWTRM JMTWTRN JMTWTRO JMTWTSM JMTWTSN JMTWTSO
JMTWUPM JMTWUPN JMTWUPO JMTWURM JMTWURN JMTWURO JMTWUSM JMTWUSN JMTWUSO
JMTWVPM JMTWVPN JMTWVPO JMTWVRM JMTWVRN JMTWVRO JMTWVSM JMTWVSN JMTWVSO
JMTXTPM JMTXTPN JMTXTPO JMTXTRM JMTXTRN JMTXTRO JMTXTSM JMTXTSN JMTXTSO
JMTXUPM JMTXUPN JMTXUPO JMTXURM JMTXURN JMTXURO JMTXUSM JMTXUSN JMTXUSO
JMTXVPM JMTXVPN JMTXVPO JMTXVRM JMTXVRN JMTXVRO JMTXVSM JMTXVSN JMTXVSO
...
LOVXVPM LOVXVPN LOVXVPO LOVXVRM LOVXVRN LOVXVRO LOVXVSM LOVXVSN LOVXVSO
LOVYTPM LOVYTPN LOVYTPO LOVYTRM LOVYTRN LOVYTRO LOVYTSM LOVYTSN LOVYTSO
LOVYUPM LOVYUPN LOVYUPO LOVYURM LOVYURN LOVYURO LOVYUSM LOVYUSN LOVYUSO
LOVYVPM LOVYVPN LOVYVPO LOVYVRM LOVYVRN LOVYVRO LOVYVSM LOVYVSN LOVYVSO
Phone number is 568-9876

Computer Science & Information Technology

You might also like to view...

Which of the following functions will convert a string into a decimal value?

A) toUpperCase() B) parseFloat() C) prompt() D) alert()

Computer Science & Information Technology

When you export data from Access to Excel, the records in Access become what in Excel?

A. Rows B. Columns C. Titles D. Sheets

Computer Science & Information Technology