fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cctype>
  4. #include <iomanip>
  5. #include <cstring>
  6.  
  7. const int MAX = 100;
  8.  
  9. using namespace std;
  10.  
  11. int getInt(char);
  12. bool isValidChar(char);
  13.  
  14. int main()
  15. {
  16. int num;
  17. int j = 0;
  18. char name, conti;
  19. char alpha[MAX];
  20.  
  21. do {
  22. cout << "Enter a phone symbols: " << endl;
  23. cin.getline(alpha, MAX, ' ');
  24. while (alpha[j] != '\0')
  25. {
  26. name = alpha[j];
  27.  
  28. if (isValidChar(name) == true)
  29. {
  30. num = getInt(name);
  31.  
  32. if (num == -1)
  33. {
  34. cout << "-";
  35. }
  36. else
  37. {
  38. cout << num;
  39. }
  40. }
  41. else
  42. {
  43. cout << " - Invalid Char " << name << " found - rejected";
  44. }
  45.  
  46. j++;
  47. } // end while
  48. j = 0;
  49. cout << endl;
  50.  
  51. cout << "\nContinue (Y/y): ";
  52. cin >> conti;
  53. cin.ignore();
  54.  
  55. } while (conti == 'y');
  56. }
  57.  
  58. int getInt(char input)
  59. {
  60. int result;
  61. char value;
  62. value = tolower(input);
  63. if ((value >= 'a' && value <= 'c'))
  64. {
  65. result = 2;
  66. }
  67. else if ((value >= 'd' && value <= 'f'))
  68. {
  69. result = 3;
  70. }
  71. else if ((value >= 'g' && value <= 'i'))
  72. {
  73. result = 4;
  74. }
  75. else if ((value >= 'j' && value <= 'l'))
  76. {
  77. result = 5;
  78. }
  79. else if ((value >= 'm' && value <= 'o'))
  80. {
  81. result = 6;
  82. }
  83. else if ((value >= 'p' && value <= 's'))
  84. {
  85. result = 7;
  86. }
  87. else if ((value >= 't' && value <= 'v'))
  88. {
  89. result = 8;
  90. }
  91. else if ((value >= 'w' && value <= 'z'))
  92. {
  93. result = 9;
  94. }
  95. else
  96. {
  97. result = -1;
  98. }
  99. return result;
  100. }
  101.  
  102. bool isValidChar(char value)
  103. {
  104. if (isalpha(value) || value == ' ')
  105. {
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
Success #stdin #stdout 0s 3300KB
stdin
abc y xyz n
stdout
Enter a phone symbols: 
222

Continue (Y/y): Enter a phone symbols: 
999

Continue (Y/y):