-1

I am trying to convert a set of letters into a 7 digit phone number with a "-" after the third digit. The function needs to allow the user to enter more than 7 letters and then allow them to enter another phone number. So far this is what I have.

#include <iostream>

using namespace std;

int main()
{
    char letter;
    int digit, num;

    cout << "Program to convert letters to "
        << "corresponding phone numbers."
        << endl;

    cout << "Enter # to stop the program." << endl;
    cout << "Enter phone number using letters: ";
    cin >> letter;
    cout << endl;


    while (letter != '#')
    {
        cout << "Phone number = ";

        num = static_cast<int>(letter)
            - static_cast<int>('a'),('A');

        if (0 <= num && num < 26)
        {
            digit = (num / 3) + 2;

            if (((num / 3 == 6) || (num / 3 == 7)) && (num % 3 == 0))
                digit = digit - 1;

            if (digit > 9)
                digit = 9;
            cout << digit << endl;
        }

        cout << "\nEnter another number." << endl;
        cin >> letter;
        cout << endl;
    }

    system("pause");
    return 0;
}

When I enter "Get Loan" my result is this

Program to convert letters to corresponding phone numbers. Enter # to stop the program. Enter phone number using letters: Get Loan

Phone number =

Enter another number.

Phone number = 3

Enter another number.

Phone number = 8

Enter another number.

Phone number =

Enter another number.

Phone number = 6

Enter another number.

Phone number = 2

Enter another number.

Phone number = 6

Enter another number.

It is skipping the uppercase letters and not stringing the number together. I have tried to use a for loop which added a count up to 7 and then added a "-" when the count reached 3. but that had even worse results.

2 Answers2

1

You can use toupper or tolower to force the letter to change to uppercase or lowercase. Use std::string to run the operation on the whole string at once. Use isalpha to skip letters which are not letters of the alphabet.

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string word;
    getline(std::cin, word);
    for(size_t i = 0; i < word.size(); i++)
    {
        int letter = std::tolower(word[i]);
        switch(letter) {
            case 'a':case 'b':case 'c': std::cout << 2;  break;
            case 'd':case 'e':case 'f': std::cout << 3;  break;
            case 'g':case 'h':case 'i': std::cout << 4;  break;
            case 'j':case 'k':case 'l': std::cout << 5;  break;
            case 'm':case 'n':case 'o': std::cout << 6;  break;
            case 'p':case 'q':case 'r': case 's': std::cout << 7;  break;
            case 't':case 'u':case 'v': std::cout << 8;  break;
            case 'w':case 'x':case 'y': case 'z': std::cout << 9; break;
            case ' ': std::cout << '-'; break;
            default: std::cout << letter; break;
        }
    }
    std::cout << "\n";
    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    This does not necessarily process Q and Z correctly. It is unclear what it does with accented or other qualified characters. It does not output a hyphen as required. – wallyk Jan 22 '18 at 02:07
  • Much better. +1. – wallyk Jan 22 '18 at 04:25
0

Today, I got this question in coding test to allow user to input a string of characters and converts the first seven characters into a telephone number format, where the fourth character is replaced with a hyphen. The program should display the telephone number after conversion and ask the user if they want to continue or not. The program should continue to run as long as the user wants it to. And this is my solution.I hope it helps.

    #include <iostream>
    #include <string>
    #include <cctype>

    using namespace std;

    int main()
    {
       string a,b;
       int i;
       char response = 'y';
       while(tolower(response)=='y')
       {
        cout<<"Enter telephone number characters : ";
        getline(cin,a);
     
        for(i=0; i < a.size(); i++)
        {
          int letter = tolower(a[i]);
          switch(letter)
          {
             case 'a':case 'b':case 'c': b +="2";   break;
             case 'd':case 'e':case 'f': b +="3";   break;
             case 'g':case 'h':case 'i': b +="4";   break;
             case 'j':case 'k':case 'l': b +="5";   break;
             case 'm':case 'n':case 'o': b +="6";   break;
             case 'p':case 'q':case 'r': case 's': b +="7";   break;
             case 't':case 'u':case 'v': b +="8";   break;
             case 'w':case 'x':case 'y': case 'z': b +="9"; break;
             case ' ': break;
             default:cout<<"1";
          }
       }
       
       string part1 = b.substr(0,3);
       string part2 = b.substr(3,4);
       cout<<"Telephone Number is : "<<part1<<"-"<<part2<<endl;
       
       b="";
       cout<<"Do you Want to continue? (Y/N)";
       cin>>response;
       cout<<endl;
       cin.ignore();
       
    }
    return 0;
    }
SebastianH
  • 734
  • 5
  • 23
  • In this code are several problems: b.substr(3,4); can throw an exception, if the phone number is smaller than 4 digits. It uses using namespace std, what is a bad practice, see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – SebastianH May 10 '23 at 09:17