Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <locale>
  4.  
  5. /*
  6. *Author: Matthew Zelenka
  7. *Projects: ZeldaMProjects5-1.cpp
  8. *Purpose: Rewrite the Telephone Digits program to accept both upper and lowercase letters.
  9. */
  10.  
  11. std::string Telephone(std::string text);
  12.  
  13. int main(int argc, char *argv)
  14. {
  15. std::cout << "Please enter a phone number's text: ";
  16. std::string s;
  17. std::cin >> s;
  18.  
  19. Telephone(s);
  20.  
  21. std::cout << "\n\n\nPlease press enter to exit the program.";
  22. std::cin.sync();
  23. std::cin.clear();
  24. std::cin.ignore();
  25. std::cin.clear();
  26. }
  27.  
  28. std::string Telephone(std::string text)
  29. {
  30. //String to be returned.
  31. std::string phoneNum = " ";
  32. //Locale.
  33. std::locale loc;
  34.  
  35. try
  36. {
  37. for(int i = 0; i < text.length() - 1; i++)
  38. {
  39. char c = toupper(text[i]);
  40. if(isdigit(c, loc))
  41. {
  42. if(c >= 'A' && c <= 'C') { std::cout << "2"; }
  43. else if(c >= 'D' && c <= 'F') { phoneNum += "3"; }
  44. else if(c >= 'G' && c <= 'I') { phoneNum += "4"; }
  45. else if(c >= 'J' && c <= 'L') { phoneNum += "5"; }
  46. else if(c >= 'M' && c <= 'O') { phoneNum += "6"; }
  47. else if(c >= 'P' && c <= 'R') { phoneNum += "7"; }
  48. else if(c >= 'S' && c <= 'V') { phoneNum += "8"; }
  49. else if(c >= 'W' && c <= 'Z') { phoneNum += "9"; }
  50. }
  51. }
  52. }
  53.  
  54. catch(...)
  55. {
  56. std::cout << "An error occured.";
  57. }
  58. cout << "Phone number: " + phoneNum;
  59. return phoneNum;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement