Phone Number

I am doing a program for homework and am having trouble asking the user if they want to enter another number. Here is the question:

make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, some companies use letters to show their telephone number. For example, using letters, the telephone number 438 – 5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225 – 5466 can be displayed as CALL HOME, which use eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than seven letters, then process only the first seven letters. Also output the – (hyphen) after the third digit. Allow the user to use both uppercase and lowercase letters as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants.

I have an if (digit == 0) at the end. This is my attempt at asking the user to enter another number but it ends up messing with the whole program. Everything worked fine until I enter that piece of code.

Any suggestions?

#include <iostream>

using namespace std;

int main()
{
char letter, anotherNumber = 0;
int digit, num, counter;

cout << "Program to convert telepone expressed in letters to numbers "
<< endl;

cout << "Enter a phone number in uppercase or lower case letters: ";
cin >> letter;
cout << endl;

counter = 0;

while (counter < 7)
{




if (counter == 3)
cout << "-";

if (65 <= letter && letter < 91)
{
num = static_cast<int>(letter)
- static_cast<int>('A');

digit = (num / 3) + 2;

if (((num / 3 == 6 ) || (num / 3 == 7))
&& (num % 3 == 0))
digit = digit - 1;

if (digit > 9)
digit = 9;



cout << digit;
}
else if (97 <= letter && letter < 123)
{
num = static_cast<int>(letter)
- static_cast<int>('a');

digit = (num / 3) + 2;

if (((num / 3 == 6 ) || (num / 3 == 7))
&& (num % 3 == 0))
digit = digit - 1;

if (digit > 9)
digit = 9;


cout << digit;


}

else
cout << "Invalid input." << endl;


cin >> letter;

counter++;

if (digit == 0)
cout << "Would you like to enter another number? (Y for yes and N for no)" << endl;

switch (anotherNumber)
{
case 'y':
case 'Y':
counter = 0;
cout << "Enter another number: ";
cin >> letter;
break;
case 'n':
case 'N':
cout << "Close the program." << endl;
default:
cout << "Invalid entry: " << endl;
}

} //end while

return 0;
}
Last edited on
Topic archived. No new replies allowed.