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