Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:24:14+00:00 2026-06-02T06:24:14+00:00

I have some homework that I was flying through until I got to this

  • 0

I have some homework that I was flying through until I got to this final step and I’m now stumped, I would really appreciate some help.

The premise of the project is to create a file of possible words given a telephone number. The user is meant to input a number with the format ‘###-####’. The code then pulls the hyphen out and sends the phone number to the method wordGenerator. I know that everything works up until this point. When the time comes to output the different possibilities of words is where I am having my issue. Here’s what my method looks like:

// 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

Unfortunately I was given a skeleton of code and told to fill in the blanks to complete the assignment. Everywhere where the comments are blocked (/* */) is where I have to fill in code.

I’m not sure what I need to do to output the proper format of the possible words. I tried searching google, but all the results I found use a much simpler (in my opinion) switch statement to achieve this and I’m constrained to the template 🙁

All help is appreciated, even a nudge in the right direction.

Edit:
I just thought of one other thing. I feel that if someone could even help me figure out how to iterate through the characters of phoneLetters[] individually instead of a block, that would be a significant step forward.
Example:
when reading in the number ‘2’ of a phone number instead of printing “ABC”, print “A” for all possible combinations, then move on to “B”.

Edit:
Here is my main():

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:24:15+00:00Added an answer on June 2, 2026 at 6:24 am

    If you can’t get rid of the horrific nested for statements then you can use the following line:

    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]
        << ' ';
    

    Some other notes on the code:

    • Avoid using exit in C++ as is prevents destructors from running

    • Avoid namespace pollution with the using namespace statement (I assume there is an using namespace std; statement above)

    • If you didn’t use exit and removed the pause (or added a scope), then the explicit close of fstream is redundant

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a homework problem that I have. I have been doing some research
I have to run some student-made code that they made as homework, I'm running
I have some code below. This code is a basic push/pop stack class that
I need some ideas for a homework assignment that I have. Consider the following
I am stumped on this homework problem. I think I have the right answer
(This is not homework) We have some extra exercices we can do, and i
So, given that I have some type of data structure like this within a
This is for homework but please know that I have looked online for help
Before I start, yes this is a homework. I would not have posted here
I'm doing some homework and while I have some experience with SML, Haskell has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.