Need help on my program I can't get it to work properly.

I am having an issue getting my program to output correctly, and having the loop repeat. The program is supposed to have a user input a phone number with letters and convert it to only numbers. I manage to get one number outputted then it seems to stay stuck. Please help, I am not sure what I am missing, or did 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  #include <iostream>
using namespace std;

int main()
{

  char repeat; //Used to start or end loop
  
  cout << "Would you like to convert a phone number with letters into numbers only?"<< endl;
  cout <<"Enter Y for yes or N for no: ";
  cin >> repeat;
  
  while (repeat != 'N' || repeat != 'n')
  {
    char phoneNumber; //variable to hold phone number
    
  int counter;
  int limit = 11;
 
      cout<< "Please enter the phone number: ";
 
        for (counter = 0; counter < limit; counter++)
  		{
  		
            cin>> phoneNumber;  
            
            if(counter< 3)
            cout<<phoneNumber;
            
            if(counter == 2)
            cout<<"-";
            
            
  			if(phoneNumber >= 'A' && phoneNumber <= 'Z' || phoneNumber >='a' && phoneNumber <='z' || phoneNumber >=0 && phoneNumber <=9)
	  		switch(phoneNumber)
             {
             
             case '0':
             cout << "0";
                  break;
             
             case 'A':
              case 'a':
              case 'B':
              case 'b':
              case 'C':
              case 'c':
              case '2':
             cout << "2";
                  break;
             case 'D':
             case 'd':
             case 'E':
             case 'e':
             case 'F':
             case 'f':
             case '3':
             cout << "3";
                  break;
             case 'G':
             case 'g':
             case 'H':
             case 'h':
             case 'I':
             case 'i':
             case '4':
             cout << "4";
                  break;
             
             case 'J':
             case 'j':
             case 'K':
             case 'k':
             case 'L':
             case 'l':
             case '5':
             cout << "5";
                  break;
             case 'M':
             case 'm':
             case 'N':
             case 'n':
             case 'O':
             case 'o':
             case '6':
             cout << "6";
                  break;
             case 'P':
             case 'p':
             case 'Q':
             case 'q':
             case 'R':
             case 'r':
             case 'S':
             case 's':
             case '7':
             cout << "7";
                  break;
             
             case 'T':
             case 't':
             case 'U':
             case 'u':
             case 'V':
             case 'v':
             case '8':
             cout << "8";
                  break;
             
             case 'W':
             case 'w':
             case 'X':
             case 'x':
             case 'Y':
             case 'y':
             case 'Z':
             case 'z':
             case '9':
             cout << "9";
                  break;  
             
           } 
				   
  		} 
          
          cout<< phoneNumber<<endl;
          cout<<"**********"<< endl;   
          cout<<"If you would like to convert another number input Y, if not enter N."<<endl;
          cin>>repeat;     
                
  }  
  
     system ("pause");
     return 0; 
} // End of main function 
Is it an assigment ? If it is did you allow to use array ?
It is an assignment, and for this assignment we are not allowed to use an array.
closed account (48T7M4Gy)
Your program works but because you are recording only one character at a time the input and character conversion are jumbled together.

Why not accept the input as a string and process the string character-by-character to build up a new output string which is the converted number?

As a tip convert all alpha characters to upper case before you do the conversion.
closed account (48T7M4Gy)
You have no case '1'
I edited this program so that it takes in a string of 12 characters and outputs a numerical phone# in the format 123-123-1234. So it works once. I still can't figure out why it won't loop again though.

Also, the for loop will only terminate if you enter 12 characters (i=0, i<limit(11))

I wasn't sure what a couple of your comparisons did so I commented them out and it didn't seem to effect the program.

Cool program, good work.

Here is my edited 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
using namespace std;

int main()
{
    
    char repeat; //Used to start or end loop
    
    cout << "Would you like to convert a phone number with letters into numbers only?"<< endl;
    cout <<"Enter Y for yes or N for no: ";
    cin >> repeat;
    
    while ((repeat == 'Y') || (repeat == 'y'))
    {
        char phoneNumber; //variable to hold phone number
        
        int counter;
        int limit = 11;
        
        cout<< "Please enter the phone number: ";
        
        for (counter = 0; counter < limit; counter++)
        {
            
            cin>> phoneNumber;
            
            //if(counter< 3){
                //cout<<phoneNumber;
            
            
            if((counter == 3) || (counter == 6)) { //the dashes make it look formatted like a real phone#
                cout<<"-";
            }
            
            
            // not sure what this is for/what it does if((phoneNumber >= 'A' && phoneNumber <= 'Z') || (phoneNumber >='a' && phoneNumber <='z') || (phoneNumber >=0 && phoneNumber <=9))
                
                switch(phoneNumber)
            {
                    
                case '0':
                    cout << "0";
                    break;
                    
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                case '2':
                    cout << "2";
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                case '3':
                    cout << "3";
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                case '4':
                    cout << "4";
                    break;
                    
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                case '5':
                    cout << "5";
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                case '6':
                    cout << "6";
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                case '7':
                    cout << "7";
                    break;
                    
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                case '8':
                    cout << "8";
                    break;
                    
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                case '9':
                    cout << "9";
                    break;  
                    
            }
         }
        cout<<endl;
        cout<<"**********"<< endl;
        cout<<"If you would like to convert another number input Y, if not enter N."<<endl;
        cin>>repeat;
    }
    
    return 0; 
} // End of main function 
Sorry for the late reply, but I ended up getting the program to function the way the assignment called for.

The final version of the code is this.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
using namespace std;

int main()
{
 	
  char phoneNumber;//Holds user's wanted number for converstion
  char repeat; //Used to enter the first loop
      
  cout << "Would you like to convert a phone number with letters into numbers only?"<< endl;
  cout <<"Enter Y for yes or N for no: ";
  cin >> repeat;
  

  while (repeat == 'Y' || repeat == 'y')
  {
      
  	int counter;
  	int limit = 10;
  	

      cout<< "Please enter the phone number: ";
 	
 	  
        for (counter = 0; counter < limit; counter++)
  		{
  		
            cin>> phoneNumber;  
            
  			if(phoneNumber >= 'A' && phoneNumber <= 'Z' || phoneNumber >='a' && phoneNumber <='z' || phoneNumber >=0 && phoneNumber <=9)
	  		switch(phoneNumber)
             {
             case '1':
    		 cout << "1";
    		 	  break;   
   		     	  case '0':
             cout << "0";
                  break;
             case 'A':
              case 'a':
              case 'B':
              case 'b':
              case 'C':
              case 'c':
              case '2':
             cout << "2";
                  break;
             case 'D':
             case 'd':
             case 'E':
             case 'e':
             case 'F':
             case 'f':
             case '3':
             cout << "3";
                  break;
             case 'G':
             case 'g':
             case 'H':
             case 'h':
             case 'I':
             case 'i':
             case '4':
             cout << "4";
                  break;
             
             case 'J':
             case 'j':
             case 'K':
             case 'k':
             case 'L':
             case 'l':
             case '5':
             cout << "5";
                  break;
             case 'M':
             case 'm':
             case 'N':
             case 'n':
             case 'O':
             case 'o':
             case '6':
             cout << "6";
                  break;
             case 'P':
             case 'p':
             case 'Q':
             case 'q':
             case 'R':
             case 'r':
             case 'S':
             case 's':
             case '7':
             cout << "7";
                  break;
             
             case 'T':
             case 't':
             case 'U':
             case 'u':
             case 'V':
             case 'v':
             case '8':
             cout << "8";
                  break;
             
             case 'W':
             case 'w':
             case 'X':
             case 'x':
             case 'Y':
             case 'y':
             case 'Z':
             case 'z':
             case '9':
             cout << "9";
                  break;  
             default:
                     cout<<"Done converting "<<endl;
                     break;       
             
           } 
          
           if(counter< 3)
            cout<<phoneNumber; //Outputs user's converted number
            
            if(counter == 2 || counter == 5) //Used for formatting of number to be ***-***-****
            cout<<"-";
				   
  		} 
  		
          cout<<"\n"<<"*****************************************************"<< endl;//Used to seperate different numbers the user wishes to have converted
          cout<<"If you would like to convert another number input Y, if not enter N: ";//User input in order to convert another number
          cin>>repeat;     
                
  }                                                   
 
     system ("pause");
     return 0; 
} // End of main function 
Topic archived. No new replies allowed.