60 709
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #49678 in C++ for Mohammad

Question #49678

Three salesmen work for a pharmaceutical company. Each salesman has an ID, salary and phone number as shown in the table below.

ID salary phone number
11 $100 2388888
12 $150 2377777
13 $250 2355555

Declare the arrays necessary to enter the information of 20 salesmen, and initialize them with the information of the three salesmen above. (i.e. the first three cells of each array are initialized as shown in the table above).

Then, the program should display the following list of tasks:
1. Add a salesman
2. Delete a salesman
3. Search for a salesman by his ID
4. Exit

The user chooses a task by entering the task number (1, 2, 3 or 4).

Write the 3 functions necessary to perform the previous tasks (add, delete and search), then call these functions to perform the tasks chosen by the user.

The program should keep on displaying the previous list of tasks and performing the chosen task until the user enters 4. (Hint: the main loop is ended when 4 is entered).

If the numbe
Expert's answer

it's better to keep all salesman info in structrather class, because it only keeps information
 and doesn't invoke some operations - no any methods. Iwrite all code below, so if you want you'll may distribute all code parts on
several files.
Here the code:

/////////////////////////////////////////////////////////
struct salesman
{
    int id;
    double salary;
    string telephone; //or you can use char*type
};
//Our salesman collection
class Salesmen
{
private:
    const int LIST_SIZE = 20;
    salesman* array[LIST_SIZE];
    int top;
public:
    //Constructor. Here three salesmen areinitialized
    Salesmen()
    {
       top = 0;
        addSalesman(11, 100,"2388888");
        addSalesman(12, 150,"2377777");
        addSalesman(13, 250,"2355555");
    }
    //Destructor
    ~Salesmen()
    {
        while (top != 0) //asour array keep pointers to objects, we must clear all memory
        {
           delete();
        }
    }
 
    void addSalesman(salesman man)
    {
        if (top == LIST_SIZE)
        {
           cout << "List is full\n";
           return;
        }
        array[top] = &man;
        top++;
    }
    void addSalesman(int id, double salary,string telephone)
    {
        salesman man;
        man.id= id;
        man.salary = salary;
        man.telephone =telephone;
        addSalesman(man);
    }
    //deletes last added
    void delete()
    {
        if (top == 0)
        {
           cout << "List is empty\n";
           return;
        }
        top--;
        delete array[top];
    }
    salesman* search(int id)
    {       
        for (int index = 0;;index < top; index++)
        {
            if(array[index]->id == id)
            {
               return array[index];
            }
        }
        return NULL; //if nosalesmen are founded; we use pointers there to be able to return NULL
    }
    void show()
    {
        cout << "IDSalary Telephone number\n";
        for (int i = 0; i <top; i++)
        {
           cout << array[i]->id << " " <<
array[i]->salary << " " << array[i]->telephone
<< endl;
        }
    }
};
void addSalesmanTest(Salesmen* men);
void deleteSalesmanTest(Salesmen* men);
void searchSalesmanTest(Salesmen* men);
void menu(int choise, Salesmen* men);
 
void main()
{
    int choise;
    Salesmen salesmen;
    while(true)
    {
        cout <<"Choose one of these points:\n";
        cout << "1to add a salesman\n";
        cout << "2to delete salesman\n";
        cout << "3to search salesman by his ID\n";
       cout << "4 to exit";
        cin >> choise;
        menu(choise,&salesmen);
        //clear screen.system() function executes any specified in parameter console command
       system("cls"); //it works only for Windows - for Linux -> system("clear");
    }
}
void menu(int choise, Salesmen* men)
{
    switch(choise)
    {
        case 1:
           addSalesmanTest(men);
           break;
        case 2:
           deleteSalesmanTest(men);
           break;
        case 3:
           searchSalesmanTest(men);
           break;
        case 4:
           cout << "Goodbye\n";
           exit(0);
        default:
           cout << "No such menu point!\n";
           break;
    }
    cout << "Press any key";
    getch();
}
void addSalesmanTest(Salesmen* men)
{
    int id;
    double salary;
    stringtelephone;
    string prompt = "Enter salesman";
    cout << prompt + "id: ";
    cin >> id;
    cout << prompt + "salary:";
    cin >> salary;
    cout << prompt + "telephonenumber: ";
    getline(cin,telephone); //we can't write cin >> telephone, so we use there standard
function
    men->add(id, salary, telephone);
    cout << "\nCurrentsalesmen:\n\n";
    men->show();
}
void deleteSalesmanTest(Salesmen* men)
{
    men->delete();
    cout << "\nCurrent salesmen:\n\n";
    men->show();
}
void searchSalesmanTest(Salesmen* men)
{
    cout << "Enter needed id:";
    int id;
    cin >> id;
    salesman* man = men->search(id);
    if(man == NULL) // or if(!man)
    {
        cout << "Nosalesman with this id!\n";
        return;
    }
    cout << "ID => "<< man->id <<
               "Salary => " << man->salary <<
               "Telephone number => " <<
man->telephone << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions