Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <locale>
- /*
- *Author: Matthew Zelenka
- *Projects: ZeldaMProjects5-1.cpp
- *Purpose: Rewrite the Telephone Digits program to accept both upper and lowercase letters.
- */
- std::string Telephone(std::string text);
- int main(int argc, char *argv)
- {
- std::cout << "Please enter a phone number's text: ";
- std::string s;
- std::cin >> s;
- Telephone(s);
- std::cout << "\n\n\nPlease press enter to exit the program.";
- std::cin.sync();
- std::cin.clear();
- std::cin.ignore();
- std::cin.clear();
- }
- std::string Telephone(std::string text)
- {
- //String to be returned.
- std::string phoneNum = " ";
- //Locale.
- std::locale loc;
- try
- {
- for(int i = 0; i < text.length() - 1; i++)
- {
- char c = toupper(text[i]);
- if(isdigit(c, loc))
- {
- if(c >= 'A' && c <= 'C') { std::cout << "2"; }
- else if(c >= 'D' && c <= 'F') { phoneNum += "3"; }
- else if(c >= 'G' && c <= 'I') { phoneNum += "4"; }
- else if(c >= 'J' && c <= 'L') { phoneNum += "5"; }
- else if(c >= 'M' && c <= 'O') { phoneNum += "6"; }
- else if(c >= 'P' && c <= 'R') { phoneNum += "7"; }
- else if(c >= 'S' && c <= 'V') { phoneNum += "8"; }
- else if(c >= 'W' && c <= 'Z') { phoneNum += "9"; }
- }
- }
- }
- catch(...)
- {
- std::cout << "An error occured.";
- }
- cout << "Phone number: " + phoneNum;
- return phoneNum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement