Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Language Write a program that simulates the dialing of a phone number. A use

ID: 3775760 • Letter: C

Question

C++ Language

Write a program that simulates the dialing of a phone number.

A user will input an 8-place number, for example: 359-3177 (note that the hyphen is considered a digit). The rules for entering phone numbers follow.

8 places

It may have numbers, letters, or both.

The phone number cannot begin with 555.

The phone number cannot begin with 0.

The hyphen must be in the 4th position.

No other characters (@#$%^&*()_+=|/>

If a letter is entered, its output will be a number (check your phone pad).

Enter Q to Quit.

If all of the rules are met, you will output a message to the console that reads like the following.
Phone Number Dialed: UN9-3177 *the number entered

If all of the rules are not met, then you output one of the following error messages to the console.

ERROR - Phone number cannot begin with 555

ERROR - Phone number cannot begin with 0

ERROR - Hyphen is not in the correct position

ERROR - An invalid character was entered

It will then prompt the user to try again.

Define a function named ReadDials() that reads each digit and letter dialed into 8 separate char variables. All the digits are sent back through parameters by reference.

Then, for each digit, the program will use a function named ToDigit(), which receives a single char argument (pass by reference) that may be a number or a letter of one of the digits dialed.

If it is a number, then return 0 by value indicating that it is a valid digit. If the digit is a letter, then the number corresponding to the letter is returned by reference, and return 0 by value indicating that it is a valid digit. Here are the letters associated with each digit.

0

5

J K L

1

6

M N O

2

A B C

7

P Q R S

3

D E F

8

T U V

4

G H I

9

W X Y Z

If the digit entered is not one of the valid digits or one of the valid letters, return –1 by value indicating that you have an invalid digit.

A phone number never begins with a 0, so the program should flag an error if such a number is entered. Make ReadDials() return –2 in this case.

A phone number never begins with 555, so the program should flag an error if such a number is entered. Make ReadDials() return –3 in this case.

A phone number always has a hyphen (-) in the 4th position. Make ReadDials() return –4 in this case (if it doesn't have a hyphen in the 4th position). If a hyphen is in any other position, it is considered an invalid digit.

If the phone number is valid, the main calls the AcknowledgeCall function to write the converted number to the output file.

All the logic of the program should be put in functions that are called from Main(): ReadDials() and AcknowledgeCall().

The ToDigits() function is called from the ReadDials() function and is used to convert each letter entered individually into a digit and to verify that the user has entered a valid phone number. Have the program work for any number of phone numbers.

In the ToDigits() function, use the toupper function to convert any letters entered to uppercase. All the error messages are to be written to the output from main() based on the return value from the functions.

Continue processing until the user enters a Q.

You will set up the 8 char variables to hold the digits of the phone number in main() and pass the variables to the functions by reference.

Using the pseudocode below, write the code that will meet the requirements.

Main Function
    Declare the char variables for the 8 digits of the phone number

    while true
        Call the ReadDials function passing the 8 digits
        by reference. ReadDials returns an error code by
        value.

        If the return value is -5, exit the do while loop

        If the error code is -1, display the
          error message "ERROR - An invalid character was entered".
        If the error code is -2, display the
          error message "ERROR - Phone number cannot begin with 0".
        If the error code is -3, display the
          error message "ERROR - Phone number cannot begin with 555".
        If the error code is -4, display the
          error message "ERROR - Hyphen is not in the correct position".
        Otherwise, call the AcknowledgeCall function
    End-While

ReadDials function

    Input the first digit
    If a Q was entered, return -5.
    Input the rest of the phone number

    Call the ToDigit function for each of the 7 digits
      not for digit 4
    If ToDigit returns -1, return -1
    If digit 4 is not a hyphen, return -4.
    If digit 1 is 0, return -2.
    If digits 1 - 3 are 5, return -3
    Otherwise, return 0

ToDigit function
    Convert the digit to upper case
    Use a switch statement to determine if the digit is valid
      and convert the letters to digits

    If the digit is invalid, return -1.
    If the digit is valid, return 0.

AcknowledgeCall function
    Display the Phone Number.

0

5

J K L

1

6

M N O

2

A B C

7

P Q R S

3

D E F

8

T U V

4

G H I

9

W X Y Z

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

int readDials();

void toDigit();

void acknowledgeCall();

int main()

{

// Declaring variables

string num;

char one, two, three, four, five, six, seven, eight;

cout << "Enter a phone number: ";

cin >> num;

one = num[0];

two = num[1];

three = num[2];

four = num[3];

five = num[4];

six = num[5]

seven = num[6]

eight = num[7];

readDials(one, two, three, four, five, six, seven, eight);

return;

int readDials(char &one, char &two, char &three, char &four, char &five, char &six, char&seven, char &eight)

{

if(one == 0)

return -2;

else if (one == 5 && two == 5 && three == 5)

return -3;

else if (four != '-')

return - 4;

else

toDigit();

}

void toDigit(int & d)

{

toupper(d);

switch(d)

{ case 'A': case 'B': case 'C':

d = '2'; break;

case 'D': case 'E': case 'F':

d = '3'; break;

case 'G': case 'H': case 'I':

d = '4'; break;

case 'J': case 'K': case 'L':

d = '5'; break;

case 'M': case 'N': case 'O':

d = '6'; break;

case 'P': case 'Q': case 'R': case 'S':

d = '7'; break;

case 'T': case 'U': case 'V':

d = '8'; break;

case 'W': case 'X': case 'Y': case 'Z':

d = '9'; break;

}

void acknowledgeCall();

{

char phoneNumber;

cout << "Phone number dialed: " << phoneNumber << endl;

}

}

Pseudo code:

Main Function
Declare the char variables for the 8 digits of the phone number
while true
Call the ReadDials function passing the 8 digits
by reference. ReadDials returns an error code by
value.
If the return value is -5, exit the do while loop
If the error code is -1, display the
error message "ERROR - An invalid character was entered".
If the error code is -2, display the
error message "ERROR - Phone number cannot begin with 0".
If the error code is -3, display the
error message "ERROR - Phone number cannot begin with 555".
If the error code is -4, display the
error message "ERROR - Hyphen is not in the correct position".
Otherwise, call the AcknowledgeCall function
End-While
ReadDials function
Input the first digit
If a Q was entered, return -5.
Input the rest of the phone number
Call the ToDigit function for each of the 7 digits
not for digit 4
If ToDigit returns -1, return -1
If digit 4 is not a hyphen, return -4.
If digit 1 is 0, return -2.
If digits 1 - 3 are 5, return -3
Otherwise, return 0
ToDigit function
Convert the digit to upper case
Use a switch statement to determine if the digit is valid
and convert the letters to digits
If the digit is invalid, return -1.
If the digit is valid, return 0.
AcknowledgeCall function
Display the Phone Number.

#include <iostream>

#include <string>

#include <cctype>

#include <iomanip>

using namespace std;

char readDials(int, char);

int toDigit(char &d);

int acknowledgeCall();

int main()

{

char num1, num2, num3, num4, num5, num6, num7, num8;

}

char readDials(int number, char)

{

number;

               cout << "Enter phone number (or Q to quit): ";

               cin >> number;

               if (number == 'Q')

                               return -5;

}

int toDigit()

{

}

int acknowledgeCall()

{ cout << "The phone number dialed: " <<

}

#include <iostream>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote