Question: A user will input an 8-place number, for example: 359-3177 (note that
ID: 3537103 • Letter: Q
Question
Question:
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.
%u2022 8 places
%u2022 It may have numbers, letters, or both.
%u2022 The phone number cannot begin with 555.
%u2022 The phone number cannot begin with 0.
%u2022 The hyphen must be in the 4th position.
%u2022 No other characters (@#$%^&*()_+=|/>)>
%u2022 If a letter is entered, its output will be a number (check your phone pad).
%u2022 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.
%u2022 ERROR - Phone number cannot begin with 555
%u2022 ERROR - Phone number cannot begin with 0
%u2022 ERROR - Hyphen is not in the correct position
%u2022 ERROR - An invalid character was entered
It will then prompt the user to try again.
This should be a lot of fun!
Here are some great things to think about as you begin your program!
Define a function named ReadDials() that reads each digit and letter dialed into 8 separate char variables (DO NOT USE ARRAYS). 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 %u20131 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 %u20132 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 %u20133 in this case.
A phone number always has a hyphen (-) in the 4th position. Make ReadDials() return %u20134 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 uses the toupper function to convert any letters entered to uppercase. All the error messages are to be written to the output file 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.
_______________________________________________________________________________
I have this code, it is not working....
#include
#include
#include
using namespace std;
//Prototypes
int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);
int ToDigit (char &d);
void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
char d = 0;
int main()
{
char d1, d2, d3, d4, d5, d6, d7, d8;
int ReturnValue = 0;
while (ReturnValue != -5)
{ ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8);
switch(ReturnValue)
{ case -1: cout <<"error- an="" invalid="" character="" was="" entered="" endl="" break="" p="" case="" -2:="" cout="" error-="" phone="" number="" can="" t="" begin="" with="" 0="" -3:="" 555="" -4:="" hyphen="" is="" not="" in="" the="" correct="" posistion="" default:="" acknowledgecall="" d1="" d2="" d3="" d4="" d5="" d6="" d7="" d8="" return="" int="" readdials="" char="" enter="" a="" number:="" cin="" if="" q="" -5="" result="=" todigit="" d="9" -2="" 5="" -3="" -="" -4="" -1="" else="" toupper="" switch="" :="" b="" c="" e="" f="" g="" h="" i="" j="" k="" l="" m="" n="" o="" r="" s="" u="" v="" w="" x="" y="" z="" void="" dialed:="">
}
Explanation / Answer
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int i,j,k,l,n;
char phn[8];
cout<<"enter number";
cin>>phn;
cout<<endl;
n=strlen(phn);
if(n!=8)
cout<<"number not 8 digiys"<<endl;
if(phn[7]=='5' && phn[6]=='5' && phn[5]=='5')
cout<<"number cannot start with 555"<<endl;
if(phn[7]=='0')
cout<<"the number cannot start with 0"<<endl;
for(i=0;i<n;i++)
{ if(i==4)
if(phn[i]!='-'){
j=1;
break; }
else if(phn[i]=='-')
{j=1; break;}
}
if(j=1)
cout<<" The hyphen must be in the 4th position."<<endl;
for(i=0;i<n;i++)
{
if(!((phn[i]>47 && phn[i]<58) || phn[i]==45))
{
cout<<" No other characters (@#$%^&*()_+=|/>"<<endl;
}
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.