Programming Langauge C++: Assignment Select a location where you can find it lat
ID: 3841707 • Letter: P
Question
Programming Langauge C++:
Assignment
Select a location where you can find it later.
Choose the default application settings.
Demonstrate the ability to create a class definition.
Demonstrate the ability to create an array of objects and manipulate the methods and members of the objects. Instructions Using Microsoft Visual Studio create a C++ program that solves the following problem.
1. Read the problem.
Create a program that keeps track of up to 8 contacts. The user should be able to add and display entries by selecting which operation to perform from a menu.
The program should perform the following operations:
1. A menu should appear prompting the user to enter A to add an entry; P to print the entries; Q to quit.
The menu should repeat continuously until the user enters Q (upper or lower case). 2. When the user enters A, they should be prompted for the name and email address of a contact.
If 8 contacts are already being stored, display an error message and redisplay the menu.
Name and email address should be strings. Do not format either entry (any input should be acceptable).
After collecting and storing the name and email address redisplay the menu. 3. When the user enters P, all of the stored names and email addresses should display.
After the names and email addresses display, the menu should redisplay. 4. When the user enters something other than A, P, or Q, the menu should redisplay. 5. Create a class that holds the name and email address.
Create an array of 8 objects of the class type to hold the data.
The class should have a member that prompts for, and collects the name and email address.
The class should have a member that displays the name and email address.
2.
Select a location where you can find it later.
Choose the default application settings.
Write code that solves the problem described in item 1.
The program should immediately terminate when Q is entered (do not pause)
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Contact { //class to store a contact
public:
void setName() { //to promt and accept name from user
cout<<"Enter name: ";
cin >> name;
}
void setEmail() { //promt for email and accept email
cout << "Enter email: ";
cin >> email;
}
string getName() { //to retun name
return name;
}
string getEmail() { //similarly to return email
return email;
}
private: //private members to hold name and email
string email;
string name;
};
char menu() { //method to display menu
char ch;
cout << "A Create a new contact ";
cout << "P Print all contacts ";
cout << "Q Quit ";
cin >> ch; // and finally accept input from user
return ch;
}
int main() {
//declare variables
int count = 0;
Contact *list = new Contact[8];
char input;
while ((input = menu())!='Q') { //Loop till user asks to QUIT
switch(input) {
case 'A': //to create a new contact
if (count == 8) { //opps memory full
cout << "Contact list full. Cannot create contacts. ";
break;
}
list[count] = Contact(); //create object
list[count].setName(); //accept name
list[count].setEmail(); // accept email;
count ++; //update count
break;
case 'P': //prints all name
cout << "Name Email "; //print header
for (int i = 0; i < count; i++)
cout << list[i].getName() << " " << list[i].getEmail() << endl; //print each entry
break;
default: //for wrong enties
cout << "Wrong entry" << endl;
}
}
}
I tried my best to keep the code as simple as possible. I have also commented the code to make your life good. If incase you are still facing difficulties with the code, please feel free to comment below. I shall try my best to resolve all your issues.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.