Can someone help with this program? Standard telephone keypads containe the digi
ID: 3688431 • Letter: C
Question
Can someone help with this program?
Standard telephone keypads containe the digits zero through nine. The numbers two through nine each have three letters associated with them. 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 remember it as "NUMBERS". Each seven-letter word or word combination corresponds to exactly one seven-digit telephone number, but a seven-digit number corresponds to many seven-letter words, most of which are not English words. It is possible, for example, that the owner of a barbershop would be pleased to know that the shop's telephone number 424-7288 corresponds to "HAIRCUT", or a liquor store's number 233-7226 corresponds to "BEERCAN". Objective: Write a program that asks a user to enter a seven-digit phone number and then writes to the file 'numbers.txt1 every possible seven-letter word combination corresponding to that number. There are 2187 (i.e. 3^7) such combinations. Treat the digits 0 & 1 as spaces, and the rest as shownExplanation / Answer
given below are the code with comments, make you easily understanding the working of code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
void combination( comb int * comb };//function declare//
int main()
{
int pNum[ 7 ] = { 0 };
cout << "Enter a phone number ";
//user input//
<< "eg: 333-7777 ";
for ( int m = 0, n = 0; m < 8; ++m ) {
int i = cin.get();
if ( isdigit( i ) )
pNum[ n++ ] = i - '0';
}
combination( pNum );
system("pause");
}
void combination( comb int * comb n )
{// possible combination//
ofstream outFile( "phone.dat",ios::out );
comb char *phoneLetters[ 10 ] = { "", "", "ABC", "DEF", "GHI", "JKL",
"MNO", "PRS", "TUV", "WXY" };
if ( !outFile )
{
cerr << ""phone.dat" could not be opened. ";
exit(1);
}
int count = 0;
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 )
outFile << ' ';
}
outFile << " Phone number is ";
for ( int i = 0; i < 7; ++i ) {
if ( i == 3 )
outFile << '-';
outFile << n[ i ];
}
outFile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.