How do I convert telephone phrases to numbers?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

How do I convert telephone phrases to numbers?

 
0
  #1
Sep 29th, 2008
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:


  1. #include <iostream>
  2. #include <cmath>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char word;
  10. int count = 0;
  11.  
  12.  
  13. cout << "To stop this program enter %." << endl;
  14.  
  15. cout << "Enter a telephone phrase followed by the '$' sign: ";
  16. cin >> word;
  17. cout << endl;
  18. word = static_cast<char>(toupper(word));
  19.  
  20.  
  21. while (word != '%')
  22. {
  23. cout << "The corresponding telephone number is: ";
  24.  
  25. while (word != '$')
  26. {
  27. for (count = 1; count <= 7; count++)
  28. {
  29. switch (word)
  30. {
  31. case 'A':
  32. case 'B':
  33. case 'C':
  34. cout << "2";
  35. break;
  36. case 'D':
  37. case 'E':
  38. case 'F':
  39. cout << "3";
  40. break;
  41. case 'G':
  42. case 'H':
  43. case 'I':
  44. cout << "4";
  45. break;
  46. case 'J':
  47. case 'K':
  48. case 'L':
  49. cout << "5";
  50. break;
  51. case 'M':
  52. case 'N':
  53. case 'O':
  54. cout << "6";
  55. break;
  56. case 'P':
  57. case 'Q':
  58. case 'R':
  59. case 'S':
  60. cout << "7";
  61. break;
  62. case 'T':
  63. case 'U':
  64. case 'V':
  65. cout << "8";
  66. break;
  67. case 'W':
  68. case 'X':
  69. case 'Y':
  70. case 'Z':
  71. cout << "9";
  72. break;
  73. case ' ':
  74. cout << "";
  75. break;
  76. default:
  77. cout << "Invalid input." << endl;
  78. }
  79. if (count == 3)
  80. cout << "-";
  81. cin >> word;
  82. word = static_cast<char>(toupper(word));
  83. }
  84. }
  85. cout << endl;
  86.  
  87. cout << "Enter a telephone phrase followed by the '$' sign: ";
  88. cin >> word;
  89. word = static_cast<char>(toupper(word));
  90. cout << endl;
  91. }
  92.  
  93. return 0;
  94. }

Thanks for the help
Last edited by Narue; Sep 29th, 2008 at 1:53 pm. Reason: changed quote to code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I convert telephone phrases to numbers?

 
1
  #2
Sep 29th, 2008
You run the counting loop for every iteration of the loop that searches for '$'. Try merging the two loops into one:
  1. while (word != '%')
  2. {
  3. //...
  4.  
  5. for (count = 1; word != '$' && count <= 7; count++)
  6. {
  7. //...
  8. }
  9.  
  10. //...
  11. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert telephone phrases to numbers?

 
0
  #3
Sep 29th, 2008
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:

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:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I convert telephone phrases to numbers?

 
1
  #4
Sep 29th, 2008
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:
  1. while ( cin.get() != '\n' )
  2. ;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert telephone phrases to numbers?

 
0
  #5
Sep 29th, 2008
Thanks for the help! The program's running properly now
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC