| | |
How do I convert telephone phrases to numbers?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
I'm working on this as an assignment for Computer Science 121. I thought I had the code working, but I couldn't figure out one crucial part. The assignment is to convert phrases (such as 'GET LOAN' or 'CALL HOME') into telephone numbers (438-5626 and 225-5466 respectively). The program has to translate whatever the user types in, including spaces, uppercase, lowercase, and phrases longer than 7 letters. If it's longer than seven, only the first seven are processed. Also, a hyphen needs to be inserted after the 3rd number. Mine does everything but evaluates only the first seven. Mine will produce a number for every letter inputted. How can I get it to work properly? Here's what I have so far:
Thanks for the help
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> #include <cctype> using namespace std; int main() { char word; int count = 0; cout << "To stop this program enter %." << endl; cout << "Enter a telephone phrase followed by the '$' sign: "; cin >> word; cout << endl; word = static_cast<char>(toupper(word)); while (word != '%') { cout << "The corresponding telephone number is: "; while (word != '$') { for (count = 1; count <= 7; count++) { switch (word) { case 'A': case 'B': case 'C': cout << "2"; break; case 'D': case 'E': case 'F': cout << "3"; break; case 'G': case 'H': case 'I': cout << "4"; break; case 'J': case 'K': case 'L': cout << "5"; break; case 'M': case 'N': case 'O': cout << "6"; break; case 'P': case 'Q': case 'R': case 'S': cout << "7"; break; case 'T': case 'U': case 'V': cout << "8"; break; case 'W': case 'X': case 'Y': case 'Z': cout << "9"; break; case ' ': cout << ""; break; default: cout << "Invalid input." << endl; } if (count == 3) cout << "-"; cin >> word; word = static_cast<char>(toupper(word)); } } cout << endl; cout << "Enter a telephone phrase followed by the '$' sign: "; cin >> word; word = static_cast<char>(toupper(word)); cout << endl; } return 0; }
Thanks for the help
Last edited by Narue; Sep 29th, 2008 at 1:53 pm. Reason: changed quote to code tags
You run the counting loop for every iteration of the loop that searches for '$'. Try merging the two loops into one:
C++ Syntax (Toggle Plain Text)
while (word != '%') { //... for (count = 1; word != '$' && count <= 7; count++) { //... } //... }
I'm here to prove you wrong.
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
Thanks, that helped a lot 
It'll end a telephone number at the seventh digit now, but it'll continue on a new line. Here's the execution:

It'll end a telephone number at the seventh digit now, but it'll continue on a new line. Here's the execution:
•
•
•
•
To stop this program enter %.
Enter a telephone phrase followed by the '$' sign: big deal$
The corresponding telephone number is: 244-3325
Enter a telephone phrase followed by the '$' sign: big deals$
The corresponding telephone number is: 244-3325
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: hello world$
The corresponding telephone number is: 435-5696
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 53
Enter a telephone phrase followed by the '$' sign: call home$
The corresponding telephone number is: 225-5466
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: mississippi$
The corresponding telephone number is: 647-7477
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 774-
Enter a telephone phrase followed by the '$' sign: call home$
The corresponding telephone number is: 225-5466
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: hello World$
The corresponding telephone number is: 435-5696
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 53
Enter a telephone phrase followed by the '$' sign:
Well, when you type characters, they don't just vanish magically when your code is done reading. They stay in the stream waiting for the next input call. This loop will clear out the input stream for you, try to find a suitable place in your code to put it:
C++ Syntax (Toggle Plain Text)
while ( cin.get() != '\n' ) ;
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help with Highscore - figuring out compiling errors and what to do about them
- Next Thread: StreamReader
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






