Prekė.h

#include <string>
using namespace std;

class Prekė
{
private:
    string pavadinimas;     
    double pagaminimodata;    
    int    kiekis;             
    double kaina;           
    double vartojimolaikas;     
public:
    Prekė();
    Prekė(string pavad, double pagam_dat, int kiek, double kain, double vart_laik);
    ~Prekė();
    string  ImtiPavadinimą()               { return pavadinimas;       }
    double  ImtiPagaminimoDatą()           { return pagaminimodata;    }
    int     ImtiKiekį()                    { return kiekis;            }
    double  ImtiKainą()                    { return kaina;             }
    double  ImtiVartojimoLaiką()           { return vartojimolaikas;   }
    bool operator >(const Prekė & kitas);
};

Galiojimas.h

#include "Prekė.h"                   

class Galiojimas
{
public: 
    static const int CMax = 100;     
private:
    Prekė      P[CMax];                 
    int        n;                       
public:
    Galiojimas();
    ~Galiojimas();
    void DėtiPrekę(Prekė pr, int ind)       { P[ind] =   pr;   }
    Prekė ImtiPrekę(int ind)                { return P[ind];   }
    void DėtiNaująPrekę(Prekė pr)           { P[n]   =   pr;
                                              n++;             }
    int  ImtiN()                            { return n;        }
    void Rikiuoti();
};

Prekė.cpp

#include "Prekė.h"

Prekė::Prekė(): pavadinimas(""), pagaminimodata(0.0), kiekis(0),
                kaina(0.0), vartojimolaikas(0.0)
{
}

Prekė::Prekė(string pavad, double pagam_dat, int kiek, double kain, double vart_laik):
       pavadinimas(pavad), pagaminimodata(pagam_dat), kiekis(kiek),
       kaina(kain), vartojimolaikas(vart_laik)
{
}

Prekė::~Prekė()
{
}

bool Prekė::operator >(const Prekė & kitas)
{
  return vartojimolaikas > kitas.vartojimolaikas;
}

Galiojimas.cpp

#include "Galiojimas.h"

Galiojimas::Galiojimas(): n(0)
{
}

Galiojimas::~Galiojimas()
{
}

void Galiojimas::Rikiuoti()
{
  for (int i = 0; i < n-1; i++) {
    int mind = i;
    for (int j = i+1; j < n; j++)
      if (P[j] > P[mind])
        mind = j;
    Prekė pr     = P[mind];
    P[mind]     = P[i];
    P[i]        = pr;
  }
}

main.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
#include "Galiojimas.h"    
using namespace std;
//----------------------------------------------------------
const char CDfv[] = "Duomenys.txt";
const char CRfv[] = "Rezultatai.txt";
//----------------------------------------------------------
void Įvesti(const char fv[], Galiojimas & prekės);
void Spausdinti(const char fv[], Galiojimas & prekės, string komentarai);
void Atrinkti(Galiojimas & prekės, Galiojimas & nebegaliojančiosPrekės, double data);
//----------------------------------------------------------
int main()
{
    setlocale(LC_ALL, "Lithuanian");
    ofstream fr(CRfv); fr.close();

    Galiojimas visosPrekės,                    
               nebegaliojančiosPrekės; 
double data; 
double nuostolis; 
cout << "Programa darbą pradėjo" << endl; 
cout << "Įveskite šiandienos datą (pvz.: 2011.11.28): " << endl; 
cin >> data >> endl; 
cout << "Gautas nuostolis sunaikinus nebegaliojančias prekes: " << nuostolis << endl;

    Įvesti(CDfv, visosPrekės);
    Spausdinti(CRfv, visosPrekės, " Visų prekių sąrašas");
    Atrinkti(visosPrekės, nebegaliojančiosPrekės, data);
    nebegaliojančiosPrekės.Rikiuoti();
    Spausdinti(CRfv, nebegaliojančiosPrekės, " Sąrašas prekių, kurių galiojimo laikas pasibaigęs");

    return 0;
}
//----------------------------------------------------------
void Įvesti(const char fv[], Galiojimas & prekės)
{
  string pavadinimas;
  double pagaminimodata;
  int    kiekis;
  double kaina;
  double vartojimolaikas;
  ifstream fd(fv);
  while (!fd.eof() && prekės.ImtiN() < prekės.CMax) {
    fd >> pavadinimas >> pagaminimodata >> kiekis >> kaina >> vartojimolaikas;
    fd.ignore();
    Prekė P(pavadinimas, pagaminimodata, kiekis, kaina, vartojimolaikas);
    prekės.DėtiNaująPrekę(P);
  }
  fd.close();
}
//----------------------------------------------------------
void Spausdinti(const char fv[], Galiojimas & prekės, string komentarai)
{
  ofstream fr(fv, ios::app);
  fr << komentarai << endl;
  fr << "----------------------------------------------------------------------" << endl;
  fr << "Pavadinimas     Pagaminimo data  Kiekis    Kaina   Vartojimo laikas" << endl;
  fr << "----------------------------------------------------------------------" << endl;
  for (int i = 0; i < prekės.ImtiN(); i++) {
    Prekė P = prekės.ImtiPrekę(i);
    fr << left << setw(12) << P.ImtiPavadinimą() << "  "
       << right << setw(4) << setprecision(8) << fixed << P.ImtiPagaminimoDatą()   << "   "
       << P.ImtiKiekį() << "  "
       << setw(7)<< setprecision(2) << fixed 
       << P.ImtiKainą() << "  "
       << setw(4) << setprecision(2) << fixed << P.ImtiVartojimoLaiką() << endl;
  }
  fr << "------------------------------------------------" << endl << endl;
  fr.close(); 
}
//----------------------------------------------------------
void Atrinkti(Galiojimas & prekės, Galiojimas & nebegaliojančiosPrekės, double data)
{
    for (int i = 0; i < prekės.ImtiN(); i++) {
    Prekė P = prekės.ImtiPrekę(i);
    if ((P.ImtiPagaminimoDatą() + P.ImtiVartojimoLaiką() >= data))
    nebegaliojančiosPrekės.DėtiNaująPrekę(P);
}
}
//----------------------------------------------------------
double Nuostolis(Galiojimas & prekės, Galiojimas & nebegaliojančiosPrekės, int n)
{
	double nuostolis = 0;
	for (int i = 0; i < prekės.ImtiN(); i++) {
    Prekė P = prekės.ImtiPrekę(i);
    nuostolis += (P.ImtiKiekį() * P.ImtiKainą());
	n++;
	return nuostolis;
}
}

Error

cout << "Įveskite šiandienos datą (pvz.: 2011.11.28): " << endl; 
cin >> data >> endl; 
cout << "Gautas nuostolis sunaikinus nebegaliojančias prekes: " << nuostolis << endl;


------ Build started: Project: , Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\.cpp(25) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
        c:\program files\microsoft visual studio 9.0\vc\include\istream(1144): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(1146): or       'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(1148): or       'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(1150): or       'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(155): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(161): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(168): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(175): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(194): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(228): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(247): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(273): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(291): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(309): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__w64 unsigned long &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(329): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(348): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(367): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(386): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(404): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(422): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\program files\microsoft visual studio 9.0\vc\include\istream(441): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        while trying to match the argument list '(std::basic_istream<_Elem,_Traits>, overloaded-function)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
Build log was saved at "file://c:\.htm"
 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What's the problem?

Member Avatar for jmichae3

line 25 of main: you can't cin endl. I didn't know you couldn't do this because I have never tried before, but I know now...

if you want to do cin.getline() that may be a substitute, then you can process it using the following:
http://www.cplusplus.com/reference/iostream/istream/getline/

#include <sstream>
using namespace std;
stringstream ss;
string sLine;
char line[128000];
if (cin.good()) {
    cin.getline(line, 128000);
    s=line;
    ss.str(sLine);
    ss>>data;
} else {
    cin.clear(ios_base::badbit|ios_base::failbit);
}

this will give you the ability to do whole line input and
you can also use this code to overload the shift-in or >> shift-out operator in a friend function within the class so your class can output itself and input itself to streams.

//shift-in and shift-out I/O operators. must define as friends and not direct members.
        friend istream& operator>>(istream& is, class CEmployee& ce) {
            char line[65000+1];
            is.getline(line, 65000);
            line[64999]='\0';
            stringstream ss;
            string sLine=line, lname, fname, mname, address;
            int id, hours;
            double hRate;
            ss.str(sLine); //convert line to stringstream
            //input the record
            if (is.good() && ss.good()) {
                ss >> id
                   >> lname
                   >> fname
                   >> mname
                   >> address
                   >> hours
                   >> hRate;
                if (!ss.bad()) {
                    stripunderscores(lname);
                    stripunderscores(fname);
                    stripunderscores(mname);
                    stripunderscores(address);

                    //set the object's values using the interface provided
                    ce.setid(id);
                    ce.setlname(lname);
                    ce.setfname(fname);
                    ce.setmname(mname);
                    ce.setaddress(address);
                    ce.sethours(hours);
                    ce.sethRate(hRate);
                } else {
                    is.clear(ios_base::badbit|ios_base::failbit);
                }
            } else {
                is.clear(ios_base::badbit|ios_base::failbit);
            }
            return is;
        }
        friend ostream& operator<<(ostream& out, class CEmployee& ce) {
            out << ce.getid()      << " "
                << ce.getlname()   << " "
                << ce.getfname()   << " "
                << ce.getmname()   << " "
                << ce.getaddress() << " "
                << ce.gethours()   << " "
                << ce.gethRate()   << endl;
            return out;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.