Help with switch-case statements

Hello, I'm working on this problem for my C++ class. The question is to write a program that does the following:

Prompts a user to enter Y or y to begin the conversion, or any other input to quit.

Prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits

Processes only the first seven letters if the user enters more than seven letters.

Outputs the – (hyphen) after the third digit.

It allows the user to use both uppercase and lowercase letters as well as spaces between words.

process as many telephone numbers as the user wants while allowing them to quit after each conversion.

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
 #include <iostream>
#include <string>

using namespace std;

 int main()
{
  char run;
  char phoneNumber;

  cout << "If you wish to translate a phone number press 'Y' or 'y', If not press any other key to exit this program." << endl;
  cin >> run;

  while (run == 'Y' || run == 'y')
  {
    cout << "Please enter the phone number in letters." << endl;
    cin >> phoneNumber;

    for (int i = 0; i < 7; i++)
    {
      if (i == 3)
      {
        cout << "-";
      }
      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;
}


I'm 95% sure the problem I'm having is with the switch-case statement or the for
statement, but I'm not sure how to fix it. Any help would be appreciated.
It's the position of your cin >> phoneNumber;
It needs to be inside the for loop, first statement.

I think your code would be more readable if you used toupper so you only need to test uppercase letters, and if you spaced your switch like 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
      switch (toupper(phoneNumber))
      {
        case 'A': case 'B': case 'C':
          cout << '2';
          break;
        case 'D': case 'E': case 'F':
          cout << '3';
          break;
        case 'G': case 'H': case 'I':
          cout << '4';
          break;
        case 'J': case 'K': case 'L':
          cout << '5';
          break;
        case 'M': case 'N': case 'O':
          cout << '6';
          break;
        case 'P': case 'Q': case 'R': case 'S':
          cout << '7';
          break;
        case 'T': case 'U': case 'V':
          cout << '8';
          break;
        case 'W': case 'X': case 'Y': case 'Z':
          cout << '9';
          break;
      }

Or you could ditch the switch and do it something like this:

1
2
3
4
5
6
7
  const char* const Numbers = "22233344455566677778889999";
  for (int i = 0; i < 7; ++i) {
    char letter;
    cin >> letter;
    if (isalpha(letter))
        cout << Numbers[toupper(letter) - 'A'];
  }

Last edited on
Hey Hey, it worked! Thank you so much for your help. on the topic of making the code more readable/efficient. This is my first programming course, so I'm not advanced in C++ at all. I didn't know about toupper, and I have no idea what you did the second patch of code. But that's neither here nor there, thank you for your help
Hello zeck239,

This is very similar to what you are doing and you might find something useful there.

http://www.cplusplus.com/forum/beginner/270217/

What I see for a start is:
1
2
3
4
5
char phoneNumber;

cout << "Please enter the phone number in letters." << endl;
cin >> phoneNumber;

The prompt suggests that more than one letter will be entered, but "phoneNumber" as defined will only hold one character. A better choice is to define "phoneNumber" as a "std::string" and use "std::getline" for the input, in case there is a space in the input.

If "run == 'Y' or run == 'y'" you will have an endless loop for the while loop because "run" is never changed in side the while loop.

With out testing I get the feeling the for loop will be a problem, but I have not gotten to that point yet.

The switch look OK. I believe it is the other 5% that is causing the problem.

Do not say that you think the problem is here. Explain what it is doing and what it should do. This helps to narrow down the problem.

Anything more that I can do will have to wait until tomorrow.

Andy
Hello zeck239,

Let us start with:

Hello, I'm working on this problem for my C++ class. The question is to write a program that does the following:

1. Prompts a user to enter Y or y to begin the conversion, or any other input to quit. Covered.

2. Prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone
   number in digits Covered. Partly

3. Processes only the first seven letters if the user enters more than seven letters.  Not covered.
Also you will need t account for a space or "-".

4. Outputs the – (hyphen) after the third digit. Covered.

5. It allows the user to use both uppercase and lowercase letters as well as spaces between words. Covered.

6. process as many telephone numbers as the user wants while allowing them to quit after each conversion. Not covered.


For point 2, like I said, you are trying to enter a string into a single character. The variable type and how you input the data needs to be changed. That is why point is only partly correct.

Point 3 is not dealt with at all. You need to check if the entered number contains a space or a "-". This will let you know if the final string should be 7 or 8 characters long. This needs to be done before you enter the for loop. I used the "string" function .find_first_of(" -") for this. Next I used the .resize(8) to eliminate anything beyond 7 or 8 characters.

For point 6 yo need to ask the user if she/he would like to continue to either keep the value of "run" or change it to end the while loop. As is the while loop is an endless loop.

Based on the instructions, if I understand them correctly, I have done 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
#include <cctype>  // <--- std::tolower() and std::toupper() and others. http://www.cplusplus.com/reference/cctype/
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char run{ 'Y' };
	//std::string phoneNumber;

	// <--- Change comments for testing. Comment all or delete when finished.
	//std::string phoneNumber{ "abcdefg" };
	std::string phoneNumber{ "abc defg" };
	//std::string phoneNumber{ "abc-defg" };
	//std::string phoneNumber{ "jklmnop" };
	//std::string phoneNumber{ "abc defghij" };

	//cout << "\n If you wish to translate a phone number press 'Y' or 'y',\n If not press any other key to exit this program. ";
	//cin >> run;

	while (std::toupper(run) == 'Y')
	{
		//cout << "Please enter the phone number in letters.: ";
		//cin >> phoneNumber;

		cout << "\n Please enter the phone number in letters.: " << phoneNumber << endl;  // <--- Added for testing. Comment or remove when done.

		size_t pos = phoneNumber.find_first_of(" -");

		if (pos != std::string::npos)
			phoneNumber.resize(8);
		else
			phoneNumber.resize(7);

		std::cout << "\n   The number is: ";  // <--- Added.

		for (size_t i = 0; i < phoneNumber.size(); i++)

Some of the code that is commented out is done for testing. This way you do not have to enter something each time you test the program. It will not hurt to uncomment the prompt and input lines 24 and 25 as it will over wright the initialized variable. Line 27 would then need a comment.

Lines 29 - 34 can be changes to just an if statement if you are not familiar with the ".find" functions.

In Dutch's example for the switch using the "std::toupper()" function is just another way of dealing with lower case letters. Your original switch or his example will work.

I did not find anything wrong with your switch. It works when you have the right input to work with. What I used is switch (std::toupper(phoneNumber[i]))

Andy
@Handy Andy,

there is one issue with your code.
Line 29 size_t pos = phoneNumber.find_first_of(" -");If you use cin >> phoneNumber it will never contain a space.
I copied code examples from just about everyone up here, but this works I think...

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
#include <cctype>  // <--- std::tolower() and std::toupper() and others. http://www.cplusplus.com/reference/cctype/
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char run{ 'Y' };


	std::string phoneNumber{ "abc defg" };


	while (std::toupper(run) == 'Y')
	{
		cout << "Please enter the phone number in letters.: ";
		getline(cin, phoneNumber);

	    int place = 0;
        char c;
	    const char* const Numbers = "22233344455566677778889999";
	
		for (auto it = std::begin(phoneNumber); it!=std::end(phoneNumber); ++it) {
		  c = *it;
		  if(c != ' ' && c != '-' && isalpha(c)) {
		     cout << Numbers[toupper(c) - 'A'];
		     place++;
		     if(place == 3) {
		        cout << "-";   
		     }
		     if(place == 7) {
		        break;   
		     }
		  }
		}
		
		cout << endl << "Do another? ";
		cin >> run;
		cin.ignore();
	}
	
	return 0;
}
Last edited on
@Thomas1965,

This is true, but I did say that OP should use "std""getline()" and not "cin >> phoneNumber".

Andy
@Handy Andy,
you are right. It was in a previous post and I missed it.
Topic archived. No new replies allowed.