can anybody help me?currently i had wrote a coding for trash recycle system.to make thing more interesting,I wish to add simulation in that coding,however,I failed to do it,since my visual C++ is just still basic only.Hope U all can lend me a hand,please.thanks

Well what is the recycling system? If you post some/all of that we can see what we can suggest. If you post your attempt then normally this forum will suggest what they think is would improve it.

A few iterations later you will then have really nice code....

Don't forget to use code tags, because that really really annoys everyone when you forget. [use the "go advanced button" to see effect.]

Are you programming console C++ or WinAPI/MFC? It makes a big difference in how we would help you.

I would like to post up my coding,But what's the best way?can please guide me?thanks a lot.As i m new here.thanks

Well what is the recycling system? If you post some/all of that we can see what we can suggest. If you post your attempt then normally this forum will suggest what they think is would improve it.

A few iterations later you will then have really nice code....

Don't forget to use code tags, because that really really annoys everyone when you forget. [use the "go advanced button" to see effect.]

I would like to do the trash recycling system.I hope U guys/gals can help me here.I will very appreciate it.thanks ya

Are you programming console C++ or WinAPI/MFC? It makes a big difference in how we would help you.

halo,I used Win32 console C++..

can anybody tell me how to post up my coding to forum?thanks

1. PATIENCE. Yours is not the only post on DaniWeb.
2. Do not PM for help. It wont get you any more attention. If anything, it will make people ignore you.
3. Copy (ctrl+c) your entire code, then paste (ctrl+v) it inside code tags.

1. PATIENCE. Yours is not the only post on DaniWeb.
2. Do not PM for help. It wont get you any more attention. If anything, it will make people ignore you.
3. Copy (ctrl+c) your entire code, then paste (ctrl+v) it inside code tags.

SOrry

I would like to do the trash recycling system.I hope U guys/gals can help me here.I will very appreciate it.thanks ya

/*Trash Recycle System*/
#ifndef purge
#define purge
#ifndef generate_n
#define generate_n
#include <iostream>
#include <fstream>   
#include <vector>    
#include <typeinfo>  
#include <cstdlib>   
#include <ctime> 
using namespace std;
ofstream out("Trash Recycle System.out");   
class Trash         
{
protected:
float _weight;
static int count;   
    Trash(float wt): _weight(wt){count++;}
    Trash() {count++;}          
    virtual ~Trash(){count--;}      
    virtual float value() const = 0;
    static int sorter(){return count;}
    float weight() const {return _weight;}
};
int Trash::count = 0;               

class Aluminium:public Trash        
{
    void operator=(const Aluminium&);   
protected:
    static float val;
    static int count;
public:
    Aluminium(float wt): Trash(wt){}
    Aluminium() {count++;}
    Aluminium (const Aluminium&) {count++;}
    ~Aluminium() {count--;}         
    float value() const{return val;}
    static void value(int newval) {val=newval;}
    static int sorter() {return count;}
};
float Aluminium::val =1.67<2.50;
int Aluminium::count = 0;

class Paper:public Trash
{
    void operator=(const Paper&);
protected:
    static float val;
    static int count;
public:
    Paper(float wt): Trash(wt){}
    Paper() {count++;}
    Paper (const Paper&) {count++;}
    ~Paper() {count--;}
    float value() const{return val;}
    static void value(int newval) {val=newval;}
    static int sorter() {return count;}
};
float Paper::val = 0.10<1.00;
int Paper::count = 0;

class Glass:public Trash
{
    void operator=(const Glass&);
protected:
    static float val;
    static int count;
public:
    Glass(float wt): Trash(wt){}
    Glass() {count++;}
    Glass (const Glass&) {count++;}
    ~Glass() {count--;}
    float value() const{return val;}
    static void value(int newval) {val=newval;}
    static int sorter() {return count;}
};
float Glass::val = 2.56<3.00;
int Glass::count = 0;

class TrashGen
{
public:
    TrashGen(){srand(time(0));}
    Trash* operator()()
    { 
        for(int i=0;i<30;i++)
            switch (rand() % 3)     
        {
            case 0:
                return new Aluminium (rand()%100);
            case 1: 
                return new Paper (rand()%100);
            case 2: 
                return new Glass (rand()%100);
        }
        return new Aluminium(0);    
    }
};


int main()
{

    vector<Trash*>bin;       
    srand(time(0));         
    const int mod = 12;
    generate_n(back_inserter(bin),bin.size(),TrashGen());

    for(int i = 0; i < rand() % mod; i++)
        bin.push_back(new Aluminium);   
    for(int j = 0; j < rand() % mod; j++)
        bin.push_back(new Paper);
    for(int k = 0; k < rand() % mod; k++)
        bin.push_back(new Glass);

    int nGlass = 0;
    int nPaper = 0;
    int nAluminium = 0;

    for(int u = 0; u < bin.size(); u++) 
    {
        if(dynamic_cast<Aluminium*>(bin[u]))
            nAluminium++;
        if(dynamic_cast<Paper*>(bin[u]))
            nPaper++;
        if(dynamic_cast<Glass*>(bin[u]))
            nGlass++;
  }

  vector<Aluminium*> alBin;
  vector<Paper*> paperBin;
  vector<Glass*> glassBin;
  vector<Trash*>::iterator sorter = bin.begin();

  while(sorter != bin.end())     
  {
    Aluminium* ap = dynamic_cast<Aluminium*>(*sorter);
    Paper* pp = dynamic_cast<Paper*>(*sorter);
    Glass* gp = dynamic_cast<Glass*>(*sorter);

    if(ap)
        cout<<"alBin.push_back(ap)"<<endl;
    else if(pp) 
        cout<<"paperBin.push_back(pp)"<<endl;
    else if(gp)
        cout<<"glassBin.push_back(gp)"<<endl;
    ++sorter;
  }

    cout << "Aluminium = " << nAluminium << endl;
    cout<< "Paper = " << nPaper << endl;
    cout<< "Glass = " << nGlass << endl;
    cout << "total created = "<<nAluminium + nPaper + nGlass<< endl;

purge(bin); 

}
#endif      
#endif

Edit your post and put &#91;code=c++&#93; before your code and &#91;/code&#93; after it. Or, if you use the advanced editor, there is a button that will do it for you.

There are many things in this code that are very strange.

For example: what is the loop in Trash* operator()() doing. i.e. for(int i=0;i<30;i++) doesn't do anything because no matter what the switch returns on the first loop.

You REALLY need to have virtual destructors. generator_n does nothing because bin always has size zero. [note: it does execute one call to TrashGen with reinitializes the random number seed]

Don't uses rand(time(0)) more than onces in the code without a very good reason.

Clean up your memory before the program exits.

If I was adding a simulation, I would want to
(a) have a amount of each type specifiable.
(b) compact all the similar objects into one.
e.g. Aluminium with 10 units, and other with 12 can become 1 aluminium with 22.
(c) then have some code to evaluate the total weight and the total value of all waste.
(d) allow some to be removed/processed.
(e) have items come in with different types in different orders.

hai,stuXYZ.I wrote this coding by refer several examples from the web,as my C++ not really good actually.
then,
1.that meant i have to cancel the generate_n?
2.can i replace rand(time(0)) with frand? but what is 'frand' meant? I viewed it from the web.
3.how can i do a simulation like what U mentioned in the post?Can U help me?I really need someone help me.Thanks a lot stuXYZ.

how about this below coding?Thanks a lot

#include "../purge.h"
#include <fstream>
#include <vector>
#include <typeinfo>
#include <cstdlib>
#include <ctime>
using namespace std;
ofstream out("recycle2.out");

class Trash {
  float _weight;
public:
  Trash(float wt) : _weight(wt) {}
  virtual float value() const = 0;
  float weight() const { return _weight; }
  virtual ~Trash() { out << "~Trash()\n"; }
};

class Aluminum : public Trash {
  static float val;
public:
  Aluminum(float wt) : Trash(wt) {}
  float value() const { return val; }
  static void value(int newval) {
    val = newval;
  }
};

float Aluminum::val = 1.67;

class Paper : public Trash {
  static float val;
public:
  Paper(float wt) : Trash(wt) {}
  float value() const { return val; }
  static void value(int newval) {
    val = newval;
  }
};

float Paper::val = 0.10;

class Glass : public Trash {
  static float val;
public:
  Glass(float wt) : Trash(wt) {}
  float value() const { return val; }
  static void value(int newval) {
    val = newval;
  }
};

float Glass::val = 0.23;


template<class Container> void
sumValue(Container& bin, ostream& os) {
  typename Container::iterator tally = 
    bin.begin();
  float val = 0;
  while(tally != bin.end()) {
    val += (*tally)->weight() * (*tally)->value();
    os << "weight of "
        << typeid(*tally).name()
        << " = " << (*tally)->weight() << endl;
    tally++;
  }
  os << "Total value = " << val << endl;
}

int main() {
  srand(time(0)); 
  vector<Trash*> bin;

  for(int i = 0; i < 30; i++)
    switch(rand() % 3) {
      case 0 :
        bin.push_back(new Aluminum(rand() % 100));
        break;
      case 1 :
        bin.push_back(new Paper(rand() % 100));
        break;
      case 2 :
        bin.push_back(new Glass(rand() % 100));
        break;
    }

  vector<Glass*> glassBin;
  vector<Paper*> paperBin;
  vector<Aluminum*> alBin;
  vector<Trash*>::iterator sorter = bin.begin();

  while(sorter != bin.end()) {
    Aluminum* ap =
      dynamic_cast<Aluminum*>(*sorter);
    Paper* pp =
      dynamic_cast<Paper*>(*sorter);
    Glass* gp =
      dynamic_cast<Glass*>(*sorter);
    if(ap) alBin.push_back(ap);
    if(pp) paperBin.push_back(pp);
    if(gp) glassBin.push_back(gp);
    sorter++;
  }
  sumValue(alBin, out);
  sumValue(paperBin, out);
  sumValue(glassBin, out);
  sumValue(bin, out);
  purge(bin);
} ///:~
commented: You were told to use CODE-tags 4 times in this thread.... -3

Sorry but it has been spelt out several times USE CODE TAGS.
Let me put it this way. If I see another post by you that doesn't do this I am simply going to add negative reputation.

As for the rest, your code it an improvement.

Now spend some time and tidy the code up. Make it look presentable, get ride of code that doesn't nothing. Look at it with the debugger. If you want to use this in a simulation, this code has to be 100% bullet proof. The you can think about writing a recycling simulation from population.

Now imagen that

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.