[ create a new paste ] login | about

Link: http://codepad.org/wEwys7ZY    [ raw code | output | fork ]

C++, pasted on Sep 23:
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
using namespace std;
class data {
  string name;
  int age;
  double height;
public:
  string get_name();
  friend bool operator<(const data &, const data &);
  friend ostream &operator<<(ostream &, const data &);
  friend istream &operator>>(istream &, data &);
};
typedef list<data> CONTAINER;

string data::get_name() {
  return name;
}

bool operator<(const data &a, const data &b) {
  return a.name < b.name;
}
ostream &operator<<(ostream &os, const data &a) {
  os << a.name << " " << a.age << " " << a.height;
  return os;
}
istream &operator>>(istream &is, data &a) {
  is >> a.name >> a.age >> a.height;
  return is;
}

void finput(istream &is, CONTAINER &l) {
  for (;;) {
    data d;
    is >> d;
    if (is.eof())
      break;
    l.push_back(d);
  }
}
void foutput(ostream &os, CONTAINER l) {
  for (CONTAINER::iterator p = l.begin(); p != l.end(); p++)
    os << *p << endl;
}
void myfind(string s, CONTAINER l) {
  for (CONTAINER::iterator p = l.begin(); p != l.end(); p++)
    if (p->get_name() == s)
      cout << *p << endl;
}

#define INPUTFILE "input.dat"
#define OUTPUTFILE "output.dat"
int main() {
  CONTAINER vlist;
  ifstream fin(INPUTFILE);
  if (!fin) {
    cerr << "cannot open the file: " << INPUTFILE << " for input." << endl;
    return -1;
  }
  finput(fin, vlist);
  fin.close();

  vlist.sort();

  ofstream fout(OUTPUTFILE);
  if (!fout) {
    cerr << "cannot open the file: " << OUTPUTFILE << " for output." << endl;
    return -1;
  }
  foutput(fout, vlist);
  fout.close();

  for (;;) {
    string kin;
    cout << "name : ";
    cin >> kin;
    if (kin == "")
      break;
    myfind(kin, vlist);
  }
  return 0;
}
/* end */


Output:
1
2
3
cannot open the file: input.dat for input.

Exited: ExitFailure 255


Create a new paste based on this one


Comments: