I have been staring at this all day and I am having a difficult time with this code. I need this program to convert a string of letters, such as Call Cash, into a 7 digit telephone number. I get either one of two outcomes. Either the program does not output any numbers at all or it gets stuck in an infinite loop. Any assistance would be greatly appreciated.
// Program to convert letters to numbers in a phone number.
//Header file
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//Declare variables
char input;
char letters;
int number;
int counter;
counter = 0;
//Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
cout << "Enter telephone number using letters: ";
cin >> letters;
cout << endl;
cout << "The corresponding phone number is: ";
while (counter <= 7);
{
cout << number;
if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
switch (letters)
{
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 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << "8";
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << "9";
break;
if (counter == 3)
cout << "-";
}
}
}
return 0;
}
edit: This is the new code I have. The problem I am hitting now is that it is only returning a value for the first letter and it is returning each value on a separate line. I do appreciate the assistance:
//Declare variables
char input;
char letters;
int number;
int i;
//Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
cout << "Enter telephone number using letters: ";
cin >> letters;
cout << endl;
cout << "The corresponding phone number is: ";
for(int i = 0; i < 7; i++)
{
if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
switch (letters)
{
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 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << "8";
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << "9";
break;
cout << number;
}
if (i == 2)
cout << "-";
}
cout << "To process another telephone number, enter Y/y." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
}
return 0;
}