Question

In: Computer Science

#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "Contact.h"

using namespace std;
class Contact
{
public:
Contact(string init_name = "", string init_phone = "000-000-0000");
void setName(string name);
void setPhone(string phone);
string getName()const;
string getPhone()const;
friend ostream& operator << (ostream& os, const Contact& c);
friend bool operator == (const Contact& c1, const Contact& c2);
friend bool operator != (const Contact& c1, const Contact& c2);
private:
string name, phone;
};
Contact::Contact(string init_name, string init_phone)
{
name = init_name;
phone = init_phone;
}
void Contact::setName(string name)
{
this->name = name;
}
void Contact::setPhone(string phone)
{
this->phone = phone;
}
string Contact::getName() const
{
return name;
}
string Contact::getPhone() const
{
return phone;
}
ostream& operator << (ostream& os, const Contact& c)
{
os << "Name: " << c.name << endl;
os << "Phone: " << c.phone << endl;
return os;
}
bool operator == (const Contact& c1, const Contact& c2)
{
return c1.name == c2.name;
}
bool operator != (const Contact& c1, const Contact& c2)
{
return c1.name != c2.name;
}

int main()
{

}
==================================================================================

sample run

Name: John Casey
Phone: 831-234-2323

Name: Amy Doe
Phone: 831-534-2890

Name: Julie Anderson
Phone: 731-234-6543

Name: Nebula Walker
Phone: 831-565-1010

Name: Paul Rod
Phone: 831-654-0090

Display the contacts from first to last and then in reverse order. Ask the user for a full name and display the Contact information for that person.

Sample Driver Program Run

List of contacts:
Name: John Casey
Phone: 831-234-2323

Name: Amy Doe
Phone: 831-534-2890

Name: Julie Anderson
Phone: 731-234-6543

Name: Nebula Walker
Phone: 831-565-1010

Name: Paul Rod
Phone: 831-654-0090

Contacts printed backwards:
Name: Paul Rod
Phone: 831-654-0090

Name: Nebula Walker
Phone: 831-565-1010

Name: Julie Anderson
Phone: 731-234-6543

Name: Amy Doe
Phone: 831-534-2890

Name: John Casey
Phone: 831-234-2323

Enter a full name:
Amy Doe

Their contact info is:
Name: Amy Doe
Phone: 831-534-2890

Solutions

Expert Solution

Program Code [C++]

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;
class Contact
{
    
    public:
    Contact(string init_name = "", string init_phone = "000-000-0000");
        void setName(string name);
        void setPhone(string phone);
        string getName()const;
        string getPhone()const;
        friend ostream& operator << (ostream& os, const Contact& c);
        friend bool operator == (const Contact& c1, const Contact& c2);
        friend bool operator != (const Contact& c1, const Contact& c2);
    private:
        string name, phone;
};

Contact::Contact(string init_name, string init_phone)
{
    name = init_name;
    phone = init_phone;
}
void Contact::setName(string name)
{
    this->name = name;
}
void Contact::setPhone(string phone)
{
    this->phone = phone;
}
string Contact::getName() const
{
    return name;
}
string Contact::getPhone() const
{
    return phone;
}
ostream& operator << (ostream& os, const Contact& c)
{
    os << "Name: " << c.name << endl;
    os << "Phone: " << c.phone << endl;
    return os;
}
bool operator == (const Contact& c1, const Contact& c2)
{
    return c1.name == c2.name;
}
bool operator != (const Contact& c1, const Contact& c2)
{
    return c1.name != c2.name;
}

int main() {
    
    // Creating an array of size 5 of Contacts
    
    Contact contacts[5];
    
    // Asking user for info as given in sample run
    
    for (int i=0; i<5; i++) {
        
        string name, phone;
        cout << "Name: ";
        getline(cin >> ws, name);
        cout << "Phone: ";
        getline(cin >> ws, phone);
        
        cout << endl;
        
        contacts[i] = Contact(name, phone);
    }
    
    // Now Showing list of contacts
    
    cout << "List of contacts:" << endl;
    
    for (int i=0; i<5; i++)
        cout << contacts[i] << endl;
        
    // Now printing list in reverse order
    
    cout << "Contacts printed backwards: " << endl;
    
    for (int i=4; i>=0; i--)
        cout << contacts[i] << endl;
        
    // Ask user for a name and displays its information
    
    string n;
    
    cout << "Enter a full name: " << endl;
    getline(cin >> ws, n);
    
    // Searchinng for name in array and if found, printing info
    
    int index = -1;
    
    for (int i=0; i<5; i++) {
        
        if(contacts[i].getName() == n)
            index = i;
    }

    if(index == -1)
        cout << "Information not found";
    else {
        cout << "Their contact info is:" << endl;
        cout << contacts[index] << endl;
    }
    
    return 0;
}



Sample Output:-

-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled          &nbsp
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled             float hours;   //time in hours             float milesPerHour; //calculated miles per hour             cout << "Please input the Miles traveled" << endl;             cin >> miles;             cout << "Please input the hours traveled" << endl;             cin >> hours;                         milesHours = miles / hours; cout << fixed << showpoint << setprecision(2);             cout << "Your speed is " <<...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT