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;
}