Need some help with last programming assignment

I am officially very stuck on this assignment. This is the last assignment for my c++ programming assignment. These are the statements I have to fill in.

/* Write a declaration for an ofstream object called outFile to open the file "phone.dat" */

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

/* Write code to check if the file was opened successfully, and terminate if not */

/* Write a series of cascaded stream insertion operations to output a set of seven letters to outFile, followed by a space */

/* Write a statement to close the ouput file */

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <cstdlib>
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 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
} // end main

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

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

  // terminate if file could not be opened
  /* Write code to check if the file was opened successfully, and terminate if not */

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

                if ( ++count % 9 == 0 ) // form rows
                  outFile << '\n';
              }
            }
          }
        }
      }
    }
  }

  // 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 */
} // end function wordGenerator 
Ofstream - http://www.cplusplus.com/reference/iostream/ofstream/

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


Clarify.

Write code to check if the file was opened successfully, and terminate if not.


1
2
3
4
if (inFile)
    // do something useful
else
    return 1;  // or use another form of ending the main function 



Write a series of cascaded stream insertion operations to output a set of seven letters to outFile, followed by a space.


Wah?


Write a statement to close the ouput file
 
outFile.close();



Why don't you ask your classmates first? They should be able to give you more in-depth answers.
1
2
  char a[11][] = {"0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"};
const unsigned int phoneLetters = sizeof(a)


it is giving me error c2117 a array bounds overflow. what does this mean?
Try this instead:
 
const char * a[] = {"0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"};

Write a series of cascaded stream insertion operations to output a set of seven letters to outFile, followed by a space.


It's a series of these;

1
2
char c;
outfile >> c >> " ";



Why do you have a 7-nested loop? They're asking for cascaded, not nested, as in side by side.
Last edited on
Topic archived. No new replies allowed.