Sample shold show as: Enter a phone number (digits 2 through 9) in the form: xxx
ID: 664929 • Letter: S
Question
Sample shold show as:
Enter a phone number (digits 2 through 9) in the form: xxx-xxxx 568-9876 (The actual output will be stored in the output file.) Contents of phone.dat 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
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
void wordGenerator( const int * const, std::string );
int main()
{
std::string strOutFileName;
int phoneNumber[ 7 ] = { 0 }; // holds phone number
std::cout << "Enter the output file to store the telephone words: ";
std::cin >> strOutFileName;
// prompt user to enter phone number
std::cout << "Enter a phone number (digits 2 through 9) "
<< "in the form: xxx-xxxx ";
// loop until we've read 7 valid values;
// ignore everything not between 2 and 9
for ( int v = 0; v < 7; )
{
int i = std::cin.get();
// test if i is between 2 and 9
if ( i >= '2' && i <= '9' )
phoneNumber[ v++ ] = i - '0';
} // end for
wordGenerator( phoneNumber, strOutFileName );
// form words from phone number and store in output file
} // end main
// function to form words based on phone number
void wordGenerator( const int * const n, std::string strOutFileName )
{
// set output stream and open output file
/* 1. Write a declaration for an ofstream object called outFile to
open the file entered by the user */
// letters corresponding to each number
/* 3. 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 an empty string for 0 and 1 */
char *s[10];
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 << ' ';
} // end for
} // end for
} // end for
} // end
} // end for
} // end for
} // end for
// output phone number
outFile << " Phone number is ";
for ( int i = 0; i < 7; ++i )
{
if ( i == 3 )
outFile << '-';
outFile << n[ i ];
} // end for
/* 4. Write a statement to close the output file */
ofs.close();
return 0;
} // end function wordGenerator
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.