In this project you will be creating a contact list. The contact list will inclu
ID: 3581161 • Letter: I
Question
In this project you will be creating a contact list. The contact list will include the name of the contact and their 10-digit phone number. 1. Begin by creating a project with a source code file named contactList.cpp. 2. Apply all the coding standards: such as a leading comment with name, date, and description, and comments that describe your function with the function declarations; 3. 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). [HINT: a 10-digit number will not fit into a single integer data type. Use three, one for each part: area code, exchange, and line number] 4. 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. 5. Declare and define a function, outputContacts, 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] NAME PHONE NUMBER John 913-555-1212 6. The main function should do the following: a. Add a contact b. Output the contact. c. Repeat these steps until the user indicates that they are finished entering data 7. 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.txt" 8. Create a function to validate the phone number. This function will have 3 pointers as parameters: for the area code, exchange, and line number. b. 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 d. A valid line number for our purposes will be values from 1000 to 9999 f the number is not valid, the function should return a value of false 9. The function to validate the phone number (#8) should be called within the addContact function, and 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 10. Make sure you use contact structure in the addContact and outputcontact functions and that the validate function parameters are passed as pointers.Explanation / Answer
1) For 1-6 refer below code
#include<iostream>
#include<string>
#include<vector>
using namespace std;
typedef struct Contact
{
string name ;
int area_code,exchange,line_number;
}Contact;
void outputContacts(Contact &c, std::vector<Contact>& vect)
{
cout<<c.name<<c.area_code<<c.exchange<<c.line_number<<endl;
vect.push_back(c);
cout<<"NAME PHONE NUMBER"<<endl;
for(size_t i = 0; i < vect.size(); i++ )
{
cout<<vect[i].name<<" "<<vect[i].area_code<<"-"<<vect[i].exchange<<"-"<<vect[i].line_number<<endl;
}
}
void addContact(Contact &c)
{
cout<<"Enter Name : ";
cin>>c.name;
cout<<"Enter phone number separated by space : ";
cin>>c.area_code>>c.exchange>>c.line_number;
}
int main()
{
char ch = 'y';
std::vector<Contact> vect;
do
{
Contact c;
addContact(c);
outputContacts(c,vect);
cout<<"Do you want to add contact y/n : ";
cin>>ch;
}while(ch == 'y');
return 0;
}
2) for whole solution refer below code
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<cstring>
using namespace std;
typedef struct Contact
{
string name ;
int area_code,exchange,line_number;
}Contact;
void outputContacts(Contact &c, std::ofstream& myfile)
{
cout<<"Contact adding to file......"<<endl;
myfile<<c.name<<" "<<c.area_code<<"-"<<c.exchange<<"-"<<c.line_number<<endl;
}
bool validate_number(int *ac, int *ex, int *ln)
{
if((*ac >= 200 && *ac <= 999) && (*ex >= 200 && *ex <= 999) && (*ln >= 1000 && *ln <= 9999))
return true;
else
return false;
}
bool addContact(Contact &c)
{
cout<<"Enter Name : ";
cin>>c.name;
cout<<"Enter phone number separated by space : ";
cin>>c.area_code>>c.exchange>>c.line_number;
if(validate_number(&c.area_code, &c.exchange, &c.line_number))
return true;
else
return false;
}
int main()
{
char ch = 'y';
std::vector<Contact> vect;
char filename[50];
char ext[]=".txt";
cout<<"Please enter filename for storing contacts : ";
cin>>filename; //getting file name from user
char *final_name = new char[std::strlen(filename) + strlen(ext) + 1];
strcpy(final_name,filename);
strcat(final_name,ext);//adding .ext at end of input string entered by user
//cout<<final_name<<endl;
std::ofstream myfile(final_name);
//Indefinite loop controlled by user choice
do
{
Contact c;
if(!addContact(c)) //If entered number is not following user choice then we are again asking to enter details
continue;
outputContacts(c,myfile); //adding contact into file
cout<<"Do you want to add contact y/n : ";
cin>>ch;
}while(ch == 'y');
myfile.close();
return 0;
}
Please refer below output for refernce
Please enter filename for storing contacts : Chegg
Enter Name : John
Enter phone number separated by space : 935 555 1212
Contact adding to file......
Do you want to add contact y/n : y
Enter Name : Donald
Enter phone number separated by space : 232 345 6788
Contact adding to file......
Do you want to add contact y/n : y
Enter Name : Hilary
Enter phone number separated by space : 123 3456 3455
Enter Name : Hilary
Enter phone number separated by space : 123 345 3455
Enter Name : Hilary
Enter phone number separated by space : 234 345 3455
Contact adding to file......
Do you want to add contact y/n : n
Process returned 0 (0x0) execution time : 98.806 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.