Hi everyone , I am doing a question that requires me to write a program that writes telephone numbers as letters and limits them to 7 letters . It must prompt the user to enter another number to be converted and display a goodbye message at the end . My problem is i included all that in my code but it is not displaying it if i enter a word its just displaying the first letter only , its not then asking me if i should enter another number and the goodbye message is not being shown . What am i doing wrong ?
#include <iostream>
#include <cstdlib>
usingnamespace std;
constint NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << " This program displays phone numbers expressed as ";
cout << " letters in digital format ";
// loop while user wants to convert phone numbers
do
{
cout << " please enter the phone number in CAPITAL LETTERS: ";
// prompt for telephone number in capitals
// loop to input 7 letters for phone number
for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
{
cin >> letter;
// print space between third and fourth digit
if (i == 3)
cout << " ";
switch (letter)
{
case'A':
case'B':
case'C':cout << "2" << endl;
break;
case'D':
case'E':
case'F':cout << "3" << endl;
break;
case'G':
case'H':
case'I':cout << "4" << endl;
break;
case'J':
case'K':
case'L':cout << "5" << endl;
break;
case'M':
case'N':
case'O':cout << "6" << endl;
break;
case'P':
case'Q':
case'R':
case'S':cout << "7" << endl;
break;
case'T':
case'U':
case'V':cout << "8" << endl;
break;
case'W':
case'X':
case'Y':
case'Z':cout << "9" << endl;
break;
default:
cout << "*" << endl;
// complete the loop
// display digit corresponding to letter
}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;
}
// prompt user to input another number to convert
cout << "Would you like to convert another number? Indicate by Y or N"<< endl;
cin >> answer;
}//stop when user no longer wants to convert phone numbers
while (answer!= 'N');
cout << "Goodbye!" << endl;
return 0;
}
#include <iostream>
#include <cstdlib>
usingnamespace std;
constint NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << " This program displays phone numbers expressed as ";
cout << " letters in digital format ";
// loop while user wants to convert phone numbers
do
{
cout << " please enter the phone number in CAPITAL LETTERS: ";
// prompt for telephone number in capitals
// loop to input 7 letters for phone number
for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
{
cin >> letter;
// print space between third and fourth digit
if (i == 3)
cout << " ";
switch (letter)
{
case'A':
case'B':
case'C':cout << "2" << endl;
break;
case'D':
case'E':
case'F':cout << "3" << endl;
break;
case'G':
case'H':
case'I':cout << "4" << endl;
break;
case'J':
case'K':
case'L':cout << "5" << endl;
break;
case'M':
case'N':
case'O':cout << "6" << endl;
break;
case'P':
case'Q':
case'R':
case'S':cout << "7" << endl;
break;
case'T':
case'U':
case'V':cout << "8" << endl;
break;
case'W':
case'X':
case'Y':
case'Z':cout << "9" << endl;
break;
default:
cout << "*" << endl;
// complete the loop
// display digit corresponding to letter
}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;
}
// prompt user to input another number to convert
cout << "Would you like to convert another number? Indicate by Y or N"<< endl;
cin >> answer;
}while (answer!= 'N');
cout << "Goodbye!" << endl;
return 0;
}
Makes it much easier to read and figure out what is part of what scope. It could use some more whitespace but that is up to you.
Anyways onto your problem.
The problem and why are only getting 1 letter is because you are storing it in a char variable (IE char letter;). A char variable can only hold one letter so when we try and do cin >> letter; on line 18 it only grab the first letter then gets rid of the rest of them.
Now I would use a std::string instead to hold the whole phone number then iterator through that string and convert the letters to numbers.