I need help creating a teacher classroom phone book program for c++ and I am con
ID: 3941019 • Letter: I
Question
I need help creating a teacher classroom phone book program for c++ and I am confused where to start. The sample output menu should look like: 'a' --- Add person 'd' --- Delete person 'p' --- Print 'q' --- Quit 'r' --- Reset (which should reread the data file) 's' --- Sort and 'w' ---- Write (which should overwrite the data file).
Also we must have a setw of around 50 and set length of around 5 for the 'a' command
output of 'a' should look like:
Adding person...
enter first name: Joe
enter last name: Smith
enter classroom (please no spaces): SB108
enter classroom phone extension (4 characters): 123
the 'd' command should ask something like :
Please enter a first name, last name, class room or extension
Delete Joe Smith ( Y or N) Y
1 record deleted
The 'p' command should:
print out this:
First Name Last Name Classroom Extension
====================================================
Joe Smith SB108 8888
the spacing inbetween needs to be like 12 characters, then 16, then 12 then 12
the 'q' command:
should just quit the program
the 'r' command:
should reread the data file and say something like:
Phone book reset. Size is: 9
if there is an error in the 'r' command please state so
The 's' command:
creates a submenu that will sort via first name, last name , classroom and extension
Looks like:
Please choose one of:
f Sort by first name
l Sort by last name
e Sort by extension
c Sort by classroom
Sorting complete.
Explanation / Answer
The name of the class is “contact”. There are two data members – name and mob
class contact
{
string name;
string mob;
public:
contact(): name(""), mob("")
{}
bool show();
bool show(string search_term);
bool name_exists(string tname);
bool add(string new_name, string new_mob);
bool edit(string);
bool erase(string new_name);
};
0. Show contacts.
1. Add contact.
2. Edit contact.
3. Delete contact.
4. print contact.
5. sort contact
6. write contact.
7.exit
cout << "0. Show contacts" << endl
<< "1. Add Contact" << endl
<< "2. Edit Contact" << endl
<< "3. Delete Contact" << endl
<< "4. Print" << endl
<< "5. Sort" << endl
<<"6. Write"<<end1
<<"7. Exit"<<end1<<end1
<< "Your choice...";
cin >> choice;
The following code shows all the contacts from the contacts-list. Examine the below code to understand how it works.
//This block of code resides inside the main function
cout << "Showing Contacts" << endl;
printline('-', 50);
for(i=0; i<100; i++)
if(person[i].show())
flag = 1;
if(!flag)
cout << "No contacts found!" << endl;
//This block of code resides inside the class
bool show()
{
if(name != "")
{
cout << name << " " << mob << endl;
return 1; //Indicates success
}
else
return 0; //Indicates failure
}
The following code adds a new contact to the contacts-list of the Phonebook application.
//The following code resides in the main function.
cout << "Add New Contact press $ to cancel" << endl;
printline('-', 20);
counter = 0;
//Loop until correct name and mobile number are entered
do
{
flag = 0;
if(counter)
cout << "Try again press $ to cancel"
<< endl;
//counts how many times the do-while loop executes
counter++;
cout << "Name: "; cin >> temp_name;
//Cancel operation
if(temp_name=="$")
{
cancel_flag = 1;
break;
}
cout << "Mobile No.: "; cin >> temp_mob;
//Cancel operation
if(temp_mob=="$")
{
cancel_flag = 1;
break;
}
//Check whether name exists
for(i=0; i<100; i++)
if(person[i].name_exists(temp_name))
{
cout << "The name you entered is already there"
"in the phonebook, enter a different name."
<< endl;
flag = 1;
break;
}
}while(!name_valid(temp_name) ||
flag ||
!mob_valid(temp_mob));
if(cancel_flag)
{
system("cls");
break;
}
//This code adds the contact to phonebook
for(i=0; i<100; i++)
if(person[i].add(temp_name, temp_mob))
{
cout << "Contact added successfully!" << endl;
flag = 1;
break;
}
if(!flag)
cout << "Memory full! Delete some contacts first."
<< endl;
//The following code resides in the class
bool add(string new_name, string new_mob)
{
if(name=="")
{
name = new_name;
mob = new_mob;
return 1; // Success
}
else
return 0; // Failure
}
The following code edits an existing contact. It edits both – name and mobile number.
//The following code resides in the main function
cout << "Enter a contact name to edit:"
" press $ to cancel ";
cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
for(i=0; i<100; i++)
if(person[i].edit(temp_name))
{
cout << "Edited Successfully!" << endl;
flag = 1;
break;
}
if(!flag)
cout << "Contact name not found!" << endl;
//The following code resides in the class
bool contact :: edit(string new_name)
{
string new_mob;
if(new_name==name)
{
cout << "Enter new name: "; cin >> new_name;
cout << "Enter new mobile no: "; cin >> new_mob;
name = new_name;
mob = new_mob;
return 1;
}
else
return 0;
}
The following code removes a contact from the contacts-list in the Phonebook application.
// The following code resides in the main function.
do
{
if(counter)
cout << "Try again" << endl;
counter++;
cout << "Enter a contact name to delete:"
" press $ to cancel ";
cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
//Final Confirmation
for(i=0; i<100; i++)
if(person[i].name_exists(temp_name))
{
flag = 1;
cout << "Are you sure you want to delete? (1/0)"
<< endl;
int yes;
cin >> yes;
if(!yes)
{
system("cls");
cancel_flag = 1;
}
break;
}
if(!flag)
cout << "Contact name not found!" << endl;
if(cancel_flag)
break;
// This code deletes the contact
if(flag)
{
for(i=0; i<100; i++)
if(person[i].erase(temp_name))
{
cout << "Deleted successfully!" << endl;
break;
}
}
}while(!flag);
// The following code resides in the class
bool erase(string new_name)
{
if(new_name==name)
{
name = "";
mob = "";
return 1;
}
else
return 0;
}
he following code searches for a contact.
// The following code resides in the main function.
do
{
if(counter)
cout << "Try again" << endl;
counter++;
cout << "Search a name: press $ to cancel ";
cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
for(i=0; i<100; i++)
if(person[i].show(temp_name))
{
flag = 1;
break;
}
if(!flag)
cout << "Contact name not found" << endl;
}while(!flag);
// The following code resides in the class
bool show(string search_term)
{
if(search_term == name)
{
cout << name << " " << mob << endl;
return 1;
}
else
return 0;
}
The printline() function prints a line. You can specify the size and the character for drawing the line.
void printline(char ch, int size)
{
for(int i=0; i<size; i++)
cout << ch;
cout << " ";
}
Two functions have been used for validations. One is name_valid(), another is mob_valid. The first one checks whether the name is valid, while the second function checks whether the mobile number is valid.
Code:
bool name_valid(string tname)
{
if(tname.size()>50)
{
cout << "Invalid Name! Enter a name within 20 characters!"
<< endl;
return 0;
}
else if(tname == "")
{
cout << "Invalid Name! Name cannot be blank!" << endl;
return 0;
}
else
return 1;
}
//mobile number validation
bool mob_valid(string tmob)
{
if(tmob.size()>13 || tmob.size()<10)
{
cout << "Invalid mobile no. Enter a no."
"between 10 and 13 digits" << endl;
return 0;
}
else if(tmob == "")
{
cout << "Invalid mobile no. Mobile"
"no cannot be blank" << endl;
return 0;
}
else
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.