0

Hello everyone. This is my first post on here so please forgive if there are mistakes. I have been searching the forum and other sites to get some answer, but not luck yet.

Below is a program that basically asks for names, ids, alias, etc of agents and then writes them to a file (also encrypts the id). I need to get the output to the file to sort in alphabetical order by the name. I have tried vectors, sorts and a couple other things but nothing seems to work. Any direction would be very helpful...

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
ofstream outfile ("agents_list");
class agent
{
    private:
        string name;
        int id;
        int num [4];
        string alias;
        int live;
        string food;
    public:
        void collect()
        {
            cout << "Please enter the agents first name: " << endl;
            cin >> name;
            cout << "Please enter the agents alias: " << endl;
            cin >> alias;
            cout << "Please enter the agents 4 digit id number: " << endl;
            cin >> id;          
            cout << "How many years has the agent been alive? " << endl;
            cin >> live;
            cout << "What is the agents favorite midnight snack? " << endl;
            cin >> food;
        }
        void encrpyt()
        {
            int temp;
            int temp2;
            int sort;

            num[0] = id / 1000;
            temp = id % 1000;
            num[1] = temp / 100;
            temp2 = temp % 100;
            num[2] = temp2 / 10;
            temp = temp2 % 10;
            num[3] = temp;

            for (int j=0; j<4; j++)
                num[j] = ((num[j]+7) % 10);

            sort = num[0];
            num[0] = num[2];
            num[2] = sort;

            sort = num[1];
            num[1] = num[3];
            num[3] = sort;
        }
        void write()
        {
            outfile << "Name: "  << name  << endl;
            outfile << "Alias: " << alias << endl;
            outfile << "Id: "    << id    << endl;
            outfile << "Lived: " << live  << endl;
            outfile << "Snack: " << food  << endl;
            outfile << endl;
        }
        void Print()
        {
            cout << "Agent " << alias << " encryption code is: ";
            for (int j=0; j<4; j++)
            {
                cout << num[j];
            }
            cout << endl;
        }

};

int main ()
{
    agent a1, a2, a3, a4, a5;

    cout << "Agent 1: " << endl;
    a1.collect();
    cout << "Agent 2: " << endl;
    a2.collect();
    cout << "Agent 3: " << endl;
    a3.collect();
    cout << "Agent 4: " << endl;
    a4.collect();
    cout << "Agent 5: " << endl;
    a5.collect();

    agent arr[5]= {a1, a2, a3, a4, a5}; 

    for (int k=0; k<5; k++)
    {
        arr[k].write();
    }

    for (int k=0; k<5; k++)
    {
        arr[k].encrpyt();
    }

    for (int k=0; k<5; k++)
    {
        arr[k].Print();
    }

    return 0;
}

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by daviddoria
0

Welcome to the forum. You need to close your code tag with '/code', instead of 'icode' for it to display properly.

I recommend you do the following: Make as simple as an example as you can. Do not take input, but rather hard code some values. Make a 10 line example that tries to sort some values, that should get you started. Report back here when you have a < 20 line compilable example that you are stuck with a specific problem, you will definitely get much more help that way.

Good luck,

Dave

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.