Nested Loops

Hi everyone , I am doing a question that requires me to write a program that writes telephone numbers as letters and limits them to 7 letters . It must prompt the user to enter another number to be converted and display a goodbye message at the end . My problem is i included all that in my code but it is not displaying it if i enter a word its just displaying the first letter only , its not then asking me if i should enter another number and the goodbye message is not being shown . 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
  #include <iostream>
#include <cstdlib>
using namespace std;
const int NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << " This program displays phone numbers expressed as ";
cout << " letters in digital format ";
// loop while user wants to convert phone numbers
do
{
cout << " please enter the phone number in CAPITAL LETTERS: ";
// prompt for telephone number in capitals
// loop to input 7 letters for phone number
for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
{
cin >> letter;
// print space between third and fourth digit
if (i == 3)
cout << " ";

switch (letter)
{
case 'A':
case 'B':
case 'C':cout << "2" << endl;
break;
case 'D':
case 'E':
case 'F':cout << "3" << endl;
break;
case 'G':
case 'H':
case 'I':cout << "4" << endl;
break;
case 'J':
case 'K':
case 'L':cout << "5" << endl;
break;
case 'M':
case 'N':
case 'O':cout << "6" << endl;
break;
case 'P':
case 'Q':
case 'R':
case 'S':cout << "7" << endl;
break;
case 'T':
case 'U':
case 'V':cout << "8" << endl;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':cout << "9" << endl;
break;
default:
cout << "*" << endl;
// complete the loop
// display digit corresponding to letter
}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;
}
// prompt user to input another number to convert
cout << "Would you like to convert another number? Indicate by Y or N"<< endl;
cin >> answer;
}//stop when user no longer wants to convert phone numbers
while (answer!= 'N');
cout << "Goodbye!" << endl;
return 0;
}
closed account (30X1hbRD)
I think it's because it's char letter. I'm a beginner myself so I can't tell for sure.
closed account (3qX21hU5)
First of all I would highly suggest you do indentation and not just have everything on the left. For example here is your program with indentation.


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

using namespace std;

const int NUMBERLETTERS = 7;

int main()
{
    char letter, answer;
    cout << " This program displays phone numbers expressed as ";
    cout << " letters in digital format ";
    // loop while user wants to convert phone numbers
    do
    {
        cout << " please enter the phone number in CAPITAL LETTERS: ";
        // prompt for telephone number in capitals
        // loop to input 7 letters for phone number
        for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
        {
            cin >> letter;
            // print space between third and fourth digit
            if (i == 3)
                cout << " ";

            switch (letter)
            {
                case 'A':
                case 'B':
                case 'C':cout << "2" << endl;
                break;
                case 'D':
                case 'E':
                case 'F':cout << "3" << endl;
                break;
                case 'G':
                case 'H':
                case 'I':cout << "4" << endl;
                break;
                case 'J':
                case 'K':
                case 'L':cout << "5" << endl;
                break;
                case 'M':
                case 'N':
                case 'O':cout << "6" << endl;
                break;
                case 'P':
                case 'Q':
                case 'R':
                case 'S':cout << "7" << endl;
                break;
                case 'T':
                case 'U':
                case 'V':cout << "8" << endl;
                break;
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':cout << "9" << endl;
                break;
                default:
                cout << "*" << endl;
                // complete the loop
                // display digit corresponding to letter
            }
            //skip letters more than 7
            string skip;
            getline(cin, skip, '\n');
            cout << endl;
        }
        // prompt user to input another number to convert
        cout << "Would you like to convert another number? Indicate by Y or N"<< endl;
        cin >> answer;
    }while (answer!= 'N');
    
    cout << "Goodbye!" << endl;
    return 0;
}


Makes it much easier to read and figure out what is part of what scope. It could use some more whitespace but that is up to you.

Anyways onto your problem.

The problem and why are only getting 1 letter is because you are storing it in a char variable (IE char letter;). A char variable can only hold one letter so when we try and do cin >> letter; on line 18 it only grab the first letter then gets rid of the rest of them.

Now I would use a std::string instead to hold the whole phone number then iterator through that string and convert the letters to numbers.

For example this should give you some ideas.

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

using namespace std;

int main()
{
    string letters = "AAADEFGGGG";
    string phoneNumber;

    for (int i = 0; i != letters.size(); ++i)
    {
        switch (letters[i])
        {
            case 'A':
            case 'B':
            case 'C':
                cout << "2" << endl;
                break;
                
            case 'D':
            case 'E':
            case 'F':
                cout << "3" << endl;
                break;
                
            case 'G':
            case 'H':
            case 'I':
                cout << "4" << endl;
                break;
                
            case 'J':
            case 'K':
            case 'L':
                cout << "5" << endl;
                break;
                
            case 'M':
            case 'N':
            case 'O':
                cout << "6" << endl;
                break;
                
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
                cout << "7" << endl;
                break;
                
            case 'T':
            case 'U':
            case 'V':
                cout << "8" << endl;
                break;
                
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                cout << "9" << endl;
                break;
                
            default:
                cout << "*" << endl;
        }
    }

}



Thank you Zereo and about indentation it really does look better and easier to follow . let me try what you 've just suggested
Hi , getting an error line 26 switch quantity not an integer ,26 .switch(letters)

line 76 getline ( cin, letters, phoneNumber, '\n' );

76|error: no matching function for call to 'getline(std::istream&, std::string&,




Topic archived. No new replies allowed.