I'm trying to do a program where it converts letters into numbers in a telephone number. For example, For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. This is what came up with:
You need to use a character array instead of character variable. Character variable can only store one character at a time.
Use this instead,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
char input;
char letters[7];
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,getline(letters, 7);
cout << endl;
And then apply the for loop for every index of the array
Mixing methods to get data from the input stream (std::cin >> and std::cin.getline) can pollute the input stream. You need to ignore any possible leftover characters still in the stream using std::cin.ignore.
std::numeric_limits<std::streamsize>::max() ignores as many characters as the input stream can hold up to a '\n' character or the end of the stream is reached. (<limits>)
Your char array needs to be created as 8 characters in length, getline will make the final character the null character ('\0').
You can also convert any lower case character to upper case using ::toupper (<cctype>).
Good use of whitespace can be helpful, you can put all the relevant case labels on one line instead of each on a separate line:
#include <iostream>
#include <limits>
#include <cctype>
int main()
{
char letters[8];
std::cout << "Enter Y/y to convert a telephone number form letters to digits.\n";
std::cout << "Enter any other letter to terminate the program.\n";
char input;
std::cin >> input;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
input = ::toupper(input);
while (input == 'Y')
{
std::cout << "Enter telephone number using letters: ";
std::cin.getline(letters, 8);
std::cout << '\n';
std::cout << "The corresponding phone number is: ";
for (int i = 0; i < 7; i++)
{
letters[i] = ::toupper(letters[i]);
if (letters[i] >= 'A' && letters[i] <= 'Z')
{
switch (letters[i])
{
case'A': case'B':case'C':
std::cout << "2";
break;
case'D': case'E': case'F':
std::cout << "3";
break;
case'G':case'H':case'I':
std::cout << "4";
break;
case'J': case'K': case'L':
std::cout << "5";
break;
case'M': case'N': case'O':
std::cout << "6";
break;
case'P': case'Q': case'R': case'S':
std::cout << "7";
break;
case'T': case'U': case'V':
std::cout << "8";
break;
case'W': case'X': case'Y':case'Z':
std::cout << "9";
break;
}
}
if (i == 2) // why 2? arrays are zero-based. letters[2] is the third element
{
std::cout << '-';
}
}
std::cout << "\n\nTo process another telephone number, enter Y/y.\n";
std::cout << "Enter any other letter to terminate the program.\n";
std::cin >> input;
input = ::toupper(input);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
Enter Y/y to convert a telephone number form letters to digits.
Enter any other letter to terminate the program.
y
Enter telephone number using letters: abcdefg
The corresponding phone number is: 222-3334
To process another telephone number, enter Y/y.
Enter any other letter to terminate the program.
y
Enter telephone number using letters: ghwznop
The corresponding phone number is: 449-9667
To process another telephone number, enter Y/y.
Enter any other letter to terminate the program.
n