Help with Homework assignment

I have been looking at this til my eyes are crossed. The assignment is to convert an "alpha" phone number into the corresponding digits allowing the user to enter more than once and putting the hyphen in the correct place. If I remove the while statement the phone number comes out but when I add it, it does not work. Can anyone point me in the right direction?
thanks!

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

using namespace std;

int main()
{
    //declare variable(s)
    string phoneNumber;
    char play;

    //input

cout <<"would you like to convert a phone number?"<<endl;
cin>>play;
    
               while (play == 'y' ||play =='Y')
               {
                  cout<< "Please enter the letters to convert to a phone number:";
                  getline(cin, phoneNumber);

                      for( int count=0; count <= 7 ; count++)
                      { if(count==3)
                            cout<<'-';
                        switch(phoneNumber[count])
                        {
                            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 'V':
                            case 't':
                            case 'T':
                            case 'u':
                            case 'U':
                            case 'v':
                                cout<<8;
                                break;
                            case 'w':
                            case 'W':
                            case 'x':
                            case 'X':
                            case 'y':
                            case 'Y':
                            case 'z':
                            case 'Z':
                                cout<<9;
                                break;
                            default:
                                break;
                                        }
                              }

               }
               cout<< "convert another number?";
                cin>>play;  
  

  


    return 0;
}
You need to add a cin.ignore() statement before your getline(cin, phoneNumber) line. It's reading the previous "y" that you entered and therefore won't allow you enter another string. After doing this your program runs fine. (although I would suggest adding an endl after the user enters a string)


Also, don't forget to add your #include<string> header
Alternatively, change line 19 to
getline(cin >> ws, phoneNumber);.

What's happening right now is that your cin >> play; is leaving a newline in the input buffer which gets eaten by getline instead of your input.
Adding cin >> ws will make cin gobble up the whitespace first so that the getline will work as expected.
OH..thank you!!!..I was really getting a headache.
Topic archived. No new replies allowed.