943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1728
  • C++ RSS
Sep 29th, 2008
0

How do I convert telephone phrases to numbers?

Expand Post »
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:


C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
1

Re: How do I convert telephone phrases to numbers?

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)
  1. while (word != '%')
  2. {
  3. //...
  4.  
  5. for (count = 1; word != '$' && count <= 7; count++)
  6. {
  7. //...
  8. }
  9.  
  10. //...
  11. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 29th, 2008
0

Re: How do I convert telephone phrases to numbers?

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:

Quote ...
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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
1

Re: How do I convert telephone phrases to numbers?

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)
  1. while ( cin.get() != '\n' )
  2. ;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 29th, 2008
0

Re: How do I convert telephone phrases to numbers?

Thanks for the help! The program's running properly now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Highscore - figuring out compiling errors and what to do about them
Next Thread in C++ Forum Timeline: StreamReader





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC