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

Begin by creating a project with a source code file named contoctUst.cpp. Apply

ID: 3581165 • Letter: B

Question

Begin by creating a project with a source code file named contoctUst.cpp. Apply alt the coding standards such that a leading comment with name, date, and description; and com that describe your function with the function declarations; Define a structure, called Contact, with the name of the contact as a string and the phone number as three integer values: area code (first 3 digits), exchange (next 3 digits) and the line number (last 4 digits). Declare and define a function, addContact. This method will prompt the user to enter the name and phone number for a contact. These values should be passed as a structure using call-by-reference parameters. Declare and define a function, output Contacts, to add your contact Information for a new contact. The new contact values should be passed as a structure using call-by-reference parameter. The output the names and phone numbers in your contact list in the following format: [The heading should be displayed only once for each run of your program] The main function should do the following: Add a contact. Output the contact. Repeat these steps until the user indicates that they are finished entering data. Make the changes necessary to cause the outputContacts function to write the contact list to a file. The name of the output file will be provided by the user at execution time - using a C-string for the filename. Your program will prompt the user for the name and you will append ".txt" to the name. That is: If the user enters "MyContacts", you will append "txt" making the output file name "MyContacts". Create a function to validate the phone number. This function will have 3 pointers as parameters: for the area code, exchange, and line number. It should have a Boolean return value to indicate if the number is a valid phone number. A valid area code and exchange will be values between 200 and 999. A valid line number for our purposes will be values from 1000 to 9999. If the number is not valid, the function should return a value of false The function to validate the phone number (tt8) should be called within the addContact function, and if the phone number is not valid the user should be notified and prompted to reenter the name and phone number until a valid phone number is entered or the user decides to exit the application. Make sure you use Contact structure in the addContact and outputContact functions and that the validate function parameters are passed as pointers.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

struct Contact{
string name;
int areaCode, exchange, lineNumber;
};

bool validate(int areaCode, int exchange, int *lineNumber){
if(*areaCode <= 200 || *areaCode >= 999){
return false;
}
if(*exchange <= 200 || *exchange >= 999){
return false;
}
if(*lineNumber <= 1000 || *lineNumber >= 9999){
return false;
}
return true;
}

void addContact(Contact *c){
string n;
int a, e, l;
do{
cout << "Enter the name: ";
cin >> n;
cout << "Enter the phone number: ";
cin >> a >> e >> l;
}while(validate(&a, &e, &l) == false);
c->areaCode = a;
c->exchange = e;
c->lineNumber = l;
}

void outputContacts(ofstream outFile, Contact c){
outFile << c->name << " " << c->areaCode << "-" << c->exchange << "-" << c->lineNumber << " ";
}

int main(){
char fileName[20];
cout << "Enter the file name: ";
cin >> fileName;
strcat(fileName, ".txt");
ofstream outFile(fileName);
Contact c;
while(true){
cout << "Enter details of the contact(end to end): ";
addContact(&c);
if(c.name.compare("end") == 0) break;
outputContacts(outFile, &c);
}
outFile.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote