Telephone Numbers

Hello again coders, I have another project to do. So far so good I would say. Just one issue with it. It's asking me to make the program accept input and produce output similar to the example program execution. Enter Y/y to convert a telephone number from letters to digits. Enter any other letter to terminate the program "Y". The corresponding telephone number is: 435-5696 to process another telephone number, enter Y/y to enter any other letter to terminate the program "z".

After plugging in the code, it checks to see if the code works. As you can see, it comes out as:

Input: Y
Programming
z

Output: Enter a phone number in letters only.
Y
9Programming
77-6472z

The result needs to be 776-4726. Crazy how this sounds, I thought I had this part down. I would appreciate it if you coders can explain how this works. Thank you coders for taking your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int counter;
    char phoneNumber;

    cout << "Enter a phone number in letters only." << endl;

    for(counter = 0; counter < 7; counter ++)
    {
        cin >> phoneNumber;
        cout << endl;

        if(counter == 3)
            cout << "-";
            cout << endl;

        if(phoneNumber >= 'A' && phoneNumber <= 'Z' || phoneNumber >= 'a' && phoneNumber <= 'z')
            switch(phoneNumber)
            {
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                    cout << 2;
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                    cout << 3;
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                    cout << 4;
                    break;
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                    cout << 5;
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                    cout << 6;
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                    cout << 7;
                    break;
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                    cout << 8;
                    break;
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                    cout << 9;
                    break;
            }
    }
    return 0;
}
Last edited on
Why are you entering 'Y' when the program expects the "phone number"?

Why are you entering more than the 7 letters that the program is expecting?

This is what I got when I ran the code:

Enter a phone number in letters only.
Programming


7

7

6
-
4

7

2

6 


Which is what I would expect.

You should also be getting a warning:
In function 'int main()':
22:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]


You may want to fix this potential problem.



ok, its "weird" to do it this way, but
if you remove lines 16 and 20, it will do what you want.

that looks like:

Enter a phone number in letters only.
programming
776-4726

its weird because you rely on the cin buffer as a string, but are really reading 1 char at a time. Its fine to do that, but 'unusual' IMHO.
Last edited on
Your program is missing a loop that would enable it to convert multiple phone numbers. I know you said it only needs to produce output similar to the example, but being able to run multiple times strikes me as an important requirement.

You have a good approach, but you might want to check if you would get points taken off for your program behaving oddly if someone doesn't enter a full phone number before hitting Enter. If you want to be sure you won't, then… have you read about std::ostringstream? An std::string would work fine as well, but ostringstreams are useful in that you could replace many of your couts with them, and not change much else.

http://www.cplusplus.com/reference/sstream/ostringstream/ostringstream/#example

-Albatross
Last edited on
Hello Frank5093,

Working with your original code consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    //int counter;
    std::string phoneNumber;  // <--- Changed.

    cout << "Enter a phone number in letters only.> ";  // <--- Changed.
    cin >> phoneNumber;

    // <--- Could check here for length of string being 7.

    // <--- A possible choice to change the letters case before use. Needs <algorithm> header file.
    //std::transform(phoneNumber.begin(), phoneNumber.end(), phoneNumber.begin(),
    //               [](unsigned char c) -> unsigned char { return std::toupper(c); });

    for (size_t counter = 0; counter < phoneNumber.size(); counter++)
    {
        //cout << endl;

        if (counter == 3)
            cout << "-";
        //cout << endl;

        if (std::toupper(phoneNumber[counter]) >= 'A' && std::toupper(phoneNumber[counter]) <= 'Z'/*) || (phoneNumber >= 'a' && phoneNumber <= 'z')*/)
            switch (std::toupper(phoneNumber[counter]))
            {
                case 'A':
                case 'a':  // <--- Can remove the lower case choices.
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                    cout << 2;
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                    cout << 3;
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                    cout << 4;
                    break;
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                    cout << 5;
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                    cout << 6;
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                    cout << 7;
                    break;
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                    cout << 8;
                    break;
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                    cout << 9;
                    break;
            }
    }

    return 0;
}

Starting at line 23 you might consider changing "counter" to "idx" or "index" for better readability. Also you should define "counter" or "idx" in the for loop because that is the only place it is used and it will be destroyed when the loop ends.

Andy
This can be drastically simplified by using a char string to store the numbers with an index of letter - 'A'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <limits>

int main()
{
	const char* const telnos = "22233344455566677778889999";

	for (char ans {}; std::cout << "Convert letters to phone number (Y/y/N/n): " && std::cin >> ans && (ans == 'Y' || ans == 'y'); ) {
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << "Enter letters to convert: ";
		std::string letts;
		std::getline(std::cin, letts);

		for (size_t l = 0; l < std::min(7U, letts.size()); ++l) {
			const char let = std::toupper(letts[l]);

			std::cout << (l == 3 ? "-" : "") << (std::isalpha(let) ? telnos[let - 'A'] : '*');
		}

		std::cout << '\n';
	}
}



Convert letters to phone number (Y/y/N/n): y
Enter letters to convert: program
776-4726
Convert letters to phone number (Y/y/N/n): n

Last edited on
thank you coders for your hard work. Everything works great!
Registered users can post here. Sign in or register to post.