Can you tell me where my problem is?

I'm sure you've all seen it a hundred times - Get a phone number in letters, convert it to numbers, have a hyphen after the third number, and ignore spaces... Here's what I've got so far - it loops fine, displays nearly everything correctly, but it's not pulling the numbers the way I need it to - what am I doing wrong?

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>

using namespace std;

int main ()
{
    char doContinue, tempLetter;
    int tempNum, numOne, numTwo, numThree, numFour, numFive, numSix, numSeven;
    int numCount;
        
    numCount = 1;
    tempNum = 0;
    
    cout << "Enter Y/y to convert a telephone number from letters to digits." ;
    cout << endl;
    cout << "Enter any other letter to terminate the program." << endl;
    cin >> doContinue;
    
    while (doContinue == 'Y' || doContinue == 'y')
        {
          cout << endl << "Enter a telephone number using letters: ";
         
          while (numCount <= 7)
              {
                 cin >> tempLetter;
                                     
                      switch (tempLetter)                       
                         {
                            case 'A': 
                            case 'B': 
                            case 'C': 
                            case 'a': 
                            case 'b': 
                            case 'c':
                                tempNum = tempNum + 2;               
                                break;                            
                            case 'D': 
                            case 'E': 
                            case 'F':
                            case 'd': 
                            case 'e': 
                            case 'f':
                                tempNum = tempNum + 3;            
                                break;                         
                            case 'G': 
                            case 'H': 
                            case 'I':
                            case 'g': 
                            case 'h': 
                            case 'i':
                                tempNum = tempNum + 4;             
                                break;                            
                            case 'J': 
                            case 'K': 
                            case 'L':			
                            case 'j': 
                            case 'k': 
                            case 'l': 
                                tempNum = tempNum + 5;            
                                break;                          
                            case 'M': 
                            case 'N': 
                            case 'O':
                            case 'm': 
                            case 'n': 
                            case 'o':
                                tempNum = tempNum + 6;             
                                break;                           
                            case 'P': 
                            case 'Q': 
                            case 'R': 
                            case 'S': 
                            case 'p': 
                            case 'q': 
                            case 'r': 
                            case 's': 
                                tempNum = tempNum + 7;             
                                break;                            
                            case 'T': 
                            case 'U': 
                            case 'V':
                            case 't': 
                            case 'u': 
                            case 'v':
                                tempNum = tempNum + 8;             
                                break;                          
                            case 'W': 
                            case 'X': 
                            case 'Y':
                            case 'Z': 
                            case 'w': 
                            case 'x': 
                            case 'y': 
                            case 'z': 
                                tempNum = tempNum + 9;           
                                break;
                            case 1:
                                tempNum = tempNum + 1;
                                break;
                            case 2:
                                tempNum = tempNum + 2;
                                break;
                            case 3:
                                tempNum = tempNum + 3;
                                break;
                            case 4:
                                tempNum = tempNum + 4;
                                break;
                            case 5:
                                tempNum = tempNum + 5;
                                break;
                            case 6:
                                tempNum = tempNum + 6;
                                break;
                            case 7:
                                tempNum = tempNum + 7;
                                break;
                            case 8:
                                tempNum = tempNum + 8;
                                break;
                            case 9:
                                tempNum = tempNum + 9;
                                break;
                            case 0:
                                tempNum = tempNum - 1;
                                break;
                            default:
                                cout << "Invalid Entry. Please Try Again.";
                                break;
                    }
                    
                    if (numCount == 1)
                       numOne = tempNum;
                    else if (numCount == 2)
                         numTwo = tempNum;
                    else if (numCount == 3)
                         numThree = tempNum;
                    else if (numCount == 4)
                         numFour = tempNum;
                    else if (numCount == 5)
                         numFive = tempNum;
                    else if (numCount == 6)
                         numSix = tempNum;
                    else if (numCount == 7)
                         numSeven = tempNum;
                         
                    numCount = ++numCount;
               }    
                  
         cout << "The corresponding telephone number is:" << endl;         
         cout << numOne << numTwo << numThree << "-";
         cout << numFour << numFive << numSix << numSeven;
         cout << endl << endl;
               
         cout << "To process another telephone number, enter Y/y." << endl;
         cout << "Enter any other letter to terminate the program." << endl;
         cin >> doContinue;
         }
         
         return 0;
         
}
1
2
3
4
5
6
case '1':
     tempNum = tempNum + 1;
     break;
case '2':
     tempNum = tempNum + 2;
//etc... 

The cases 1 through 0 are there in case the telephone number entered is similar to 555-CALL, etc. Are those breaking the rest of my program? or is it just the "tempNum = tempNum + 1" parts?
All I wanted to say is that 1 is not the same as '1'. You write 1, 2, ... in your code, while it should be '1', '2', ...
take advantage of the fact that '0'-48=0, 'a'-32='A', and that 'A'+1=66, 'B'+1 =67 and 'C'+1 = 68 and that any of these divided by 3 is equal to 22. This logic works for converting the rest of the alphabet, up through and including 'R'. You would have to a subtract 0 or 1 for numbers after that. This is assuming that you are using ASCII; however, I believe that unicode is backward compatible with ASCII, so this shouldn't be too much of a problem.
FYI, tempNum = tempNum + 1; is the same as tempNum += 1;

Also, you don't have to use both uppercase and lowercase letters in your cases. You can use tolower() or toupper() on the char before the switch statement, and only use either upper or lower case letters. Kills off a lot of redundancy that way.

and I'm not totally positive, but I believe numCount = ++numCount; is equivelent to numCount++;

And also, if you wanted to, you could do tempNum++ instead of tempNum += 1 or tempNum-- instead of tempNum -= 1

They just increment or decrement the number by 1.
Last edited on
Topic archived. No new replies allowed.