minta tolong otai2 syok semua. saya ada try buat satu c++ programming ni. sample output seperti di bawah :
masalah yang saya hadapi sekarang ialah, saya tak berapa nak tahu la macam mana nak dapat kan GRAND TOTAL tu, ada siapa-siapa yang baik hati lagi permurah boleh tolong saya x? ni coding yang saya dah pun buat:
Code:
#include <iostream>
#include <string>
using namespace std;
class contributor
{
protected:
string name, cname;
float payment, paper, metal, plastic;
public:
contributor()
{
cout << "";
//cout << " = RECYCLE WEEK = " << endl;
//cout << "==========================================================" << endl;
}
};
class recycle : public contributor
{
private:
float total;
public:
recycle() : total(0) {}
void getname()
{
cout << "\nEnter name : ";
fflush(stdin);
getline(cin, name);
cout << "Enter company name : ";
getline(cin, cname);
}
void getkilo()
{
cout << "\nEnter " << name << " contributions (kilo) under category :" << endl;
cout << " Paper : ";
cin >> paper;
cout << " Metal : ";
cin >> metal;
cout << " Plastics : ";
cin >> plastic;
}
void calc()
{
payment = (paper * 0.20) + (metal * 0.50) + (plastic * 0.30);
}
float calc_grandtotal(float tot)
{
total = tot;
return total = total + payment;
}
void display()
{
cout << name << " " << cname << " " << paper << " " << metal << " " << plastic << " " << payment << endl;
}
void display_grandtotal()
{
cout << "\nGRAND TOTAL: RM " << total << endl;
}
};
class recycleweek : public recycle
{
public:
recycleweek()
{
cout << "==============================================================" << endl;
cout << " = RECYCLE WEEK = " << endl;
cout << "==============================================================" << endl;
}
};
class test : public recycle
{
public:
test()
{
cout << "==============================================================" << endl;
cout << "= RECEIPT =" << endl;
cout << "= Name Company Paper Metal Plastics Payment(RM) =" << endl;
cout << "==============================================================" << endl;
}
};
int main()
{
float total;
/*int no;
cout << "How many contributor(s)? ";
cin >> no;
contributor *c;
c = new contributor[no];
for(int i = 0; i <= no - 1; i++)
{
(*(c+ +i)).getname();
(*(c+ +i)).getkilo();
}
delete [] c;*/
recycleweek c;
int no;
cout << "How many contributor(s)? ";
cin >> no;
contributor cont;
recycle *r;
r = new recycle[no];
for(int i = 0; i <= no - 1; i++)
{
cout << "\nContributor #" << i+1 << endl;
cout << "--------------" << endl;
(*(r+ +i)).getname();
(*(r+ +i)).getkilo();
(*(r+ +i)).calc();
//(*(r+ +i)).calc_grandtotal();
}
test t;
for(int j = 0; j <= no - 1; j++)
{
(*(r+ +j)).display();
(*(r+ +j)).calc_grandtotal(total);
}
(*(r)).display_grandtotal();
//recycle tot;
//tot.display_grandtotal();
delete [] r;
system("PAUSE");
return 0;
}