telephone

Hello guys. I'm new to this site. I just want to know why can't I end the loop in my code. It's basically a telephone conversion code in which it prompts the user to enter words and it converts it to a phone number.


#include <iostream>
using namespace std;

int main()
{

int counter;
char phoneNumber;
char prompt = '\0';


cout << "Enter a telephone number expressed in letters: ";
while(prompt != 'N' || prompt != 'n')
{
for(counter = 0; counter < 7; counter ++)
{
cin >> phoneNumber;
if(counter == 3)
cout << "-";

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;
}
}
cout << endl;
cout << "Would you like to enter another number (Y/N)? ";
cin >> prompt;
}
cout <<endl<<endl;
system("pause");
return 0;
}
Look at the condition for the while statement:

prompt != 'N' || prompt != 'n'

Think about it- if prompt is n, then prompt != 'N'. Ergo, the loop will never stop, because prompt is never both n and N. Replace it with &&.

Also, use a do-while.
hello

i fixed your code !!

here you go

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>
using namespace std;

int main()
{

int counter;
char phoneNumber;
char prompt = '\0';
string str="";



do{
    cout << "Enter a telephone number expressed in letters [ 7 letters maximum ]: ";
for(counter = 0; counter < 7; counter++){

    cin >> phoneNumber;
    if(counter == 3)
        str+="-";

    if( (phoneNumber >= 'A' && phoneNumber <= 'Z')||( phoneNumber >= 'a' && phoneNumber <= 'z') ){ 
                                                        
        switch(phoneNumber)
        {
            case 'A':
            case 'a':
            case 'B':
            case 'b':
            case 'C':
            case 'c':
                str+="2";
                break;
            case 'D':
            case 'd':
            case 'E':
            case 'e':
            case 'F':
            case 'f':
                str+="3";
                break;
            case 'G':
            case 'g':
            case 'H':
            case 'h':
            case 'I':
            case 'i':
                str+="4";
                break;

            case 'J':
            case 'j':
            case 'K':
            case 'k':
            case 'L':
            case 'l':
                str+="5";
                break;
            case 'M':
            case 'm':
            case 'N':
            case 'n':
            case 'O':
            case 'o':
                str+="6";
                break;
            case 'P':
            case 'p':
            case 'Q':
            case 'q':
            case 'R':
            case 'r':
            case 'S':
            case 's':
                str+="7";
                break;

            case 'T':
            case 't':
            case 'U':
            case 'u':
            case 'V':
            case 'v':
                str+="8";
                break;

            case 'W':
            case 'w':
            case 'X':
            case 'x':
            case 'Y':
            case 'y':
            case 'Z':
            case 'z':
            str+="9";
                break;
        }//switch
    }// if 
}// for

cout <<"your number is : "<<str<<"\n\n";
str="";
cout << "Would you like to enter another number (Y/N)? ";
cin >> prompt;
system("cls");
}while(prompt == 'y' || prompt=='Y');


cout <<endl<<endl;
system("pause");
return 0;
}


Enter a telephone number expressed in letters [ 7 letters maximum ]: rahmatr 
your number is : 724-6287 

Would you like to enter another number (Y/N)? y 
Enter a telephone number expressed in letters [ 7 letters maximum ]: rahmatw 
your number is : 724-6289 

Would you like to enter another number (Y/N)? n 

In this situation it'd be easier to use an if statement imo.

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
switch(phoneNumber)
        {
            case 'A':
            case 'a':
            case 'B':
            case 'b':
            case 'C':
            case 'c':
                str+="2";
                break;
            case 'D':
            case 'd':
            case 'E':
            case 'e':
            case 'F':
            case 'f':
                str+="3";
                break;
            case 'G':
            case 'g':
            case 'H':
            case 'h':
            case 'I':
            case 'i':
                str+="4";
                break;

            case 'J':
            case 'j':
            case 'K':
            case 'k':
            case 'L':
            case 'l':
                str+="5";
                break;
            case 'M':
            case 'm':
            case 'N':
            case 'n':
            case 'O':
            case 'o':
                str+="6";
                break;
            case 'P':
            case 'p':
            case 'Q':
            case 'q':
            case 'R':
            case 'r':
            case 'S':
            case 's':
                str+="7";
                break;

            case 'T':
            case 't':
            case 'U':
            case 'u':
            case 'V':
            case 'v':
                str+="8";
                break;

            case 'W':
            case 'w':
            case 'X':
            case 'x':
            case 'Y':
            case 'y':
            case 'Z':
            case 'z':
            str+="9";
                break;
        }//switch 
==
1
2
3
4
if(tolower(phoneNumber) >= 'a' && tolower(phoneNumber) <= 'c')
{
    str += "2";
}
though you could also use some trickery for the number since you have 3 letters in each case (excluding the last part).
Thanks for the help guys. I have a question. Is it possible that the user can input more than 8 letters? The book says that if the program inputs "CALL HOME", it would produce the output for "CALL HOM" then ask the user if he/she wants to enter a number again. And lastly, is it possible to omit the system("cls") line? We haven't talked about it in class yet. Again thanks for the help guys.
its easier and more better giblit , thank you.

your welcome takatz28

yes you can omit system("cls") , but it cleans the console and attracts user.
however , choise is with you.

you can ask user that " your number contains how many digits ??? ".
then set that to the condition of loop.
like this.

1
2
3
4
5
6
7

  int digits;
  std::cout<<"your number contains how many digits ???";
  std::cin>>digits;
  // for(counter = 0; counter < 7; counter++){ // last loop
  for(counter = 0; counter < digits; counter++){// new loop
Last edited on
To make telephone numbers easier to remember, some companies use letters
to show their telephone number. For example, using letters, the telephone
number 438-5626 can be shown as GET LOAN. In some cases, to make a
telephone number meaningful, companies might use more than seven letters.
For example, 225-5466 can be displayed as CALL HOME, which uses eight
letters. Write a program that prompts the user to enter a telephone number
expressed in letters and outputs the corresponding telephone number in digits. If
the user enters more than seven letters, then process only the first seven letters.
Also output the – (hyphen) after the third digit. Allow the user to use both
uppercase and lowercase letters as wellas spaces between words. Moreover, your
program should process as many telephone numbers as the user wants.

That's the problem given in the book. Is it possible that the user will just input a number more than 8 without asking them? And also, I would like to not use the system("cls") since we have not studied it yet but I don't know how to replace it with the right code.
So you would simply ignore the excess digits. In other words
1
2
for(int letter = 0; letter < 7; ++letter) 
//7 should be a constant variable such as "max letters" or "max digits" 


As far as the assignment does it explicitly say to append the digits to a string?

If not I would just do something like cout << (tolower(phoneNumber[digit]) - 'a') / 3 % 9; untested but in theory it should work.

*edit formula is off. working on a new one. Too exhausted to come up with a cleaver solution but you could do something like this (though not very pretty)
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
letter = tolower(letter);
if(isalpha(letter) && letter < 'd')
{
    std::cout << 2;
}
else if(letter < 'g')
{
    std::cout << 3;
}
else if(letter < 'j')
{
    std::cout << 4;
}
else if(letter < 'm')
{
    std::cout << 5;
}
else if(letter < 'p')
{
    std::cout << 6;
}
else if(letter < 't')
{
    std::cout << 7;
}
else if(letter < 'w')
{
    std::cout << 8;
}
else
{
    std::cout << 9;
}
It's half the lines as before but I feel as if it could be majorly improved if I wasn't as tired :P I'm sure someone else can find it unless you need more help tomorrow.
Last edited on
The assignment is basically do a program out of the problem. However, this is under chapter 5 which is control structures (repetition). It's a beginning C++ class, so some terms are quite unfamiliar to me.
Thanks for the help. I really appreciate it.
wow! im a beginner. real talk i dont understand all of these. hahaha. But i can make a program that can convert attendance, quiz, project, exam in final grade.
Topic archived. No new replies allowed.