Help with c++ Telephone words plz HELP?!?
-
Please HELP! Here is the assignment: Write a C++ 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. Me and my friend are clueless. Whenever we compile the file doesn't open it just exits. Someone please explain what we might be doin wrong? #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 ofstream outFile( "phone.dat", ios::out ); // letters corresponding to each number const char *phoneLetters[10] = { "\0", "\0", "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"}; // terminate if file could not be opened if( !outFile ) { cerr << "File could not be opened"<<endl; 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++ ) { /* Write a series of cascaded stream insertion operations to output a set of seven letters to outFile, followed by a space */ 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 outFile.close(); } // end function wordGenerator
-
Answer:
I just ran your program and it worked fine. Here's the first line from the phone.dat file. ADGJMPT ADGJMPU ADGJMPV ADGJMRT ADGJMRU ADGJMRV ADGJMST ADGJMSU ADGJMSV I'm not sure what the problem is? put in couts through the program so you can see the progress
Jodanie H. at Yahoo! Answers Visit the source
Related Q & A:
- Can some plz help, i want to get an 11-15 oyster photo card and i dont know how to post it?Best solution by Yahoo! Answers
- Yahoo Messenger Not Working...Plz Help Me?
- What would be the best book to Help me learn web design in C# and asp.net?Best solution by Stack Overflow
- Help with c++ grading program.Best solution by Yahoo! Answers
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.