do ... while (not looping) char to telephone

Homework summery below.
Iv been working on this for days and oddly cannot get this code to work correctly, taking from things i found online, in my book, in my lectures and so on.
The 1st part of code whether Y or N or other will continue anyway (but still technically passes the mindtap 1st part of coding but would still like it to work correctly for learning purposes)

The number works fine as well, but then i cant get the do.. while loop to work correctly prompting me to (CIN) yes or no to repeat a new number

Would like some simplified help im a c++ dummy but still learning, so if you can help me fix the code and explain why




Summary
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.

Instructions
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 well as spaces between words.

Moreover, your program should process as many telephone numbers as the user wants.

The program should accept input and produce output similar to the example program execution below.

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

int main() {

char tele = 'y';
do
{ 
cout << "Enter Y/y to convert a telephone number from letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl; 
cin >> tele;
} while (tele == 'n' || tele == 'N');

//----------------------------------------------------  
cout << "Enter a telephone number using letters:" << endl;
//----------------------------------------------------   
do
{
char letter;
int number = 0;
while (cin.get(letter) && number < 7 ) 
{
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
number++;
if (letter > 'Z') {
letter = (int)letter-32; 
}
if (number == 4) {
cout << "-";
}
switch (letter) {
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;
default:
break;
}
}  
}
cout << endl;
cout << "Enter another number? Y or N?" << endl;
cin >> tele;
} while (tele == 'n' || tele == 'N');
    
    return 0;
}
Last edited on
Your program is a good start, but you need to tweak it.
This condition:
1
2
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
    ++number;

causes “number” to increment even when those characters which, in the ASCII table, are in the range between 'Z' and 'a' are read (i.e. ‘[’, ‘\’, ‘]’, ‘^’, ‘_’, ‘`’).

Example output:
Enter Y/y to convert a telephone number from letters to digits.
Enter any other letter to terminate the program.
hdf^[]__gh
Enter a telephone number using letters:
33-
Enter another number? Y or N?


- - -

i cant get the do.. while loop to work correctly prompting me to (CIN) yes or no to repeat a new number

You don’t need two do-while loops:

1
2
3
4
5
do {
    - ask for letters
    - output the result
    - ask if the user wants to input another number or not
} while (repeat if the user wants to input another number)


Marginal note: if you want to output a single character, you can use the single inverted commas '' (instead of the double inverted commas ""). E.g.: std::cout << '3';
BTW thank you so far for helping, was up till 2am still working on this
i redid the code some yet the do... while loop still seems to mess up.
mabye bc i got another (while in the code??)
1
2
3
while (cin.get(letter) && number < 7 ) 
{
if (letter != ' ' && letter >= 'A' && letter <= 'z') {


if i enter the wrong Y/N the 1st one continues anyways
1
2
3
4
5
6
char tele = 'y';
do
{
cout << "Enter Y/y to convert a telephone number from letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl; 
cin >> tele;


And at the second prompt, i don't get the choice to enter Y or N it just stops working
1
2
3
4
5
6
7
cout << endl;
cout << "Enter another number? Y or N?" << endl;
cin >> tele;
} while (tele == 'y' || tele == 'Y');
    
    return 0;
}




All the new code
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
#include <iostream>
using namespace std;

int main() {

char tele = 'y';
do
{
cout << "Enter Y/y to convert a telephone number from letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl; 
cin >> tele;

//----------------------------------------------------  
cout << "Enter a telephone number using letters:" << endl;
//----------------------------------------------------   

char letter;
int number = 0;
while (cin.get(letter) && number < 7 ) 
{
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
++number;
if (letter > 'Z') {
letter = (int)letter-32; 
}
if (number == 4) {
cout << "-";
}
switch (letter) {
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;
default:
break;
}
}  
}
cout << endl;
cout << "Enter another number? Y or N?" << endl;
cin >> tele;
} while (tele == 'y' || tele == 'Y');
    
    return 0;
}

1) Nobody is taking your post into any consideration because you did not care to indent you code properly.
Moral of the story: if you don’t care about the others, the others will ignore you.

2) Read the comments in your (prettified) code (read them!):

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


int main()
{
    char tele = 'y';
    do
    {
        // You don't need to ask this question here, because, if the user runs
        // your program, you can safely assume s/he wants to make at least one
        // conversion (otherwise you shouldn’t have choosen the do-while loop):
        std::cout << "Enter Y/y to convert a telephone number from letters to"
                     " digits.\nEnter any other letter to terminate the program: ";
        std::cin >> tele;

        // Your code should start here:
        std::cout << "Enter a telephone number using letters: ";
        char letter;
        int number = 0;
        while (std::cin.get(letter) && number < 7 )
        {
            // You have an issue here, since this condition let a bunch of
            // unwanted characters ([ \ ] ^ _ `) get through:
            if (letter != ' ' && letter >= 'A' && letter <= 'z') {
                ++number;
                if (letter > 'Z') {
                    // C-style cast: do prefer explicit C++ style casts.
                    letter = (int)letter-32;
                }
                if (number == 4) {
                    std::cout << "-";
                }
                switch (letter) {
                case 'A': case 'B': case 'C':
                    // Do use single inverted commas when you deal with
                    // single characters (same below):
                    std::cout << "2";
                    break;
                case 'D': case 'E': case 'F':
                    std::cout << "3";
                    break;
                case 'G': case 'H': case 'I':
                    std::cout << "4";
                    break;
                case 'J': case 'K': case 'L':
                    std::cout << "5";
                    break;
                case 'M': case 'N': case 'O':
                    std::cout << "6";
                    break;
                case 'P': case 'Q': case 'R': case 'S':
                    std::cout << "7";
                    break;
                case 'T': case 'U': case 'V':
                    std::cout << "8";
                    break;
                case 'W': case 'X': case 'Y': case 'Z':
                    std::cout << "9";
                    break;
                default:
                    break;
                }
            }
        }

        // Move the above wrongly positioned question here:
        std::cout << "\nEnter another number? Y or N? ";
        std::cin >> tele;
    } while (tele == 'y' || tele == 'Y');

    return 0;
}

mmmm idk, changed based on your comments and it broke even more inside the MINDTAP program and now just repeatedly loops jumbled numbers
Topic archived. No new replies allowed.