Please tweak my code

Hey whats up you guys. Please help me!!.

* My program execution should look like:

Enter a string : 1-800-FLOWERS <<< My program does this one fine
1 - 800 - 3569377

Want to process another phone? y=yes, n=no : y

Enter a string : 1800flowers (My program just repeats back "1800flowers"..)
18003569377

Want to process another phone? y=yes, n=no : y

Enter a string : 1-800-GOTJUNK <<< My program does this one fine too
1-800-4685865

Want to process another phone? y=yes, n=no : n

Have a nice day!



So my question is... how can I find the 1800flowers one to make it output numbers and not just give me "1800flowers" back. And how can I have use user process another phone? (Ex: Want to process another phone? y=yes, n=no : ) please help me !!!!!!!!!

Thank you





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
 #include <iostream>
  6 using namespace std;
  7
  8 char translateLetter (char ch)
  9 {
 10         switch (ch) {
 11
 12         case 'A':
 13         case 'a':
 14         case 'B':
 15         case 'b':
 16         case 'C':
 17         case 'c':
 18                  return '2';
 19         case 'D':
 20         case 'd':
 21         case 'E':
 22         case 'e':
 23         case 'F':
 24         case 'f':
 25                  return '3';
 26         case 'G':
 27         case 'g':
 28         case 'H':
 29         case 'h':
 30         case 'I':
 31         case 'i':
 32                  return '4';
 33         case 'J':
 34         case 'j':
 35         case 'K':
 36         case 'k':
 37         case 'L':
 38         case 'l':
 39                  return '5';
  40         case 'M':
 41         case 'm':
 42         case 'N':
 43         case 'n':
 44         case 'O':
 45         case 'o':
 46                   return '6';
 47         case 'P':
 48         case 'p':
 49         case 'Q':
 50         case 'q':
 51         case 'R':
 52         case 'r':
 53         case 'S':
 54         case 's':
 55                  return '7';
 56         case 'T':
 57         case 't':
 58         case 'U':
 59         case 'u':
 60         case 'V':
 61         case 'v':
 62                  return '8';
 63         case 'W':
 64         case 'w':
 65         case 'X':
 66         case 'x':
 67         case 'Y':
 68         case 'y':
 69         case 'Z':
 70         case 'z':
 71                 return '9';
 72         break;
 73         }
 74 }
 75
 76 int main ()
 77 {
 78         // variables
  79         string phone_number;
 80         char ch;
 81
 82         cout << "Enter a string : ";
 83         cin  >> phone_number;
 84
 85         int length = phone_number.length();
 86
 87         for (int i = 0; i < length; i++)
 88         {
 89             ch = phone_number.at(i);
 90
 91             if (ch >= 'A' && ch <= 'Z')
 92             {
 93                ch = translateLetter(ch);
 94                cout << ch;
 95             }
 96             else
 97             {
 98                cout << ch;
 99             }
100
101         }
102
103
104
105
106         //string phone_number;
107         //cout << "Want to process another phone? y=yes, n=no :";
108         //cin >> userInput;
109
110
111
112         return 0;
113 }

                                                       


Hello ryan157,

At line 87 your line 91 the if statement is only checking for uppercase letter.

I would include the header file <cctype> and in the if state change it to:
if (std::toupper(ch) >= 'A' && std::toupper(ch) <= 'Z').

Hope that helps,

Andy
Topic archived. No new replies allowed.