Jump to content
Sign in to follow this  
mamakima

Tolong Periksa Kenapa Output Tidak Jadi Seperti Yang Dia Nak?

Recommended Posts

Question -

Pustaka SM supplies books and stationaries for customers. Write a program that will help Pustaka SM to do these operation:

i. Input the number of item and the price for each item.
- Method inputPrice()
ii. Count the total price of item for customer. The total of item is depend on customer sales.
- Method countPrice()
iii. Count 20% discount from total price if it is more than RM150.
- Non-member Method countDiscount() - create as function friend

The example of output with 20%discount

WELCOME TO PUSTAKA SM
--------------------------------------------------------

ENTER THE NUMBER OF ITEM : 3

PRICE FOR ITEM 1 : RM 100

PRICE FOR ITEM 2 : RM 40

PRICE FOR ITEM 3 : RM 50


THE TOTAL PRICE : RM 190

20% DISCOUNT : RM 38

THE TOTAL PRICE AFTER DISCOUNT : RM 152
Press any key to continue....


The example of output with no discount:

WELCOME TO PUSTAKA SM
---------------------------------------------------
ENTER THE NUMBER OF ITEM : 5

PRICE FOR ITEM 1 : RM 20

PRICE FOR ITEM 2 : RM 20

PRICE FOR ITEM 3 : RM 10

PRICE FOR ITEM 4 : RM 40

PRICE FOR ITEM 5 : RM 20

THE TOTAL PRICE : RM 110

NO DISCOUNT!

THE TOTAL PRICE THAT HAVE TO PAY : RM 110
Press any key to continue.....

**************************************************************************

BAWAH NI CONTOH CODING AKU SUDAH BUAT

**************************************************************************

#include <iostream.h>
class item{
private:

int item_1;
int item_2;
int item_3;
int item_4;
int item_5;
double total;

public:

item(int,int,int,int,int);
friend void calculate_item(item a,item b );

};

item::item(int a,int b,int c,int d,int e){
item_1=a;
item_2=b;
item_3=c;
item_4=d;
item_5=e;

}

void calculate_item(item a,item b ){
a.total=a.item_1+a.item_2+a.item_3+a.item_4+a.item_5;
cout<<"THE TOTAL PRICE : RM "<<a.total<<endl;

if (a.total>150)

{
b.total=a.total*20/100;
cout<<"20% DISCOUNT : RM "<<b.total<<endl;
b.total=a.total - b.total;
cout<<"THE TOTAL PRICE AFTER DISCOUNT : RM "<<b.total<<endl;
}

else

cout<<"NO DISCOUNT!"<<endl;
cout<<"THE TOTAL PRICE THAT HAVE TO PAY : RM "<<a.total<<endl;
}

void main(){
int it_1,it_2,it_3,it_4,it_5;
cout<<"PRICE FOR ITEM 1 : RM ";
cin>>it_1;
cout<<"PRICE FOR ITEM 2 : RM ";
cin>>it_2;
cout<<"PRICE FOR ITEM 3 : RM ";
cin>>it_3;
cout<<"PRICE FOR ITEM 4 : RM ";
cin>>it_4;
cout<<"PRICE FOR ITEM 5 : RM ";
cin>>it_5;

item item1(it_1,it_2,it_3,it_4,it_5);
calculate_item(item1,item1);
}

*****************************************************
MASALAH YANG AKU DAPATI OUTPUT DISKAUN DAN TIDAK DISKAUN KELUAR SEKALI
HARAP SANGAT BANTUAN OTAI2 UNTUK PERIKSA APA SALAH YANG AKU DAPAT... TERIMA KASIH :) Edited by mamakima

Share this post


Link to post
Share on other sites
[quote]else
[color="#FF0000"]{[/color]
cout<<"NO DISCOUNT!"<<endl;
cout<<"THE TOTAL PRICE THAT HAVE TO PAY : RM "<<a.total<<endl;
[color="#FF0000"]}[/color]
}
[/quote]

ko lupa letak pembuka dan penutup " { " dalam else tu, pembetulan dalam kaler merah. Dah lama tak buat c++ nih. Kalau masih tak betul kena tunggu orang lain tolong lak .. huhu

Share this post


Link to post
Share on other sites
rs ada logical error sbb max item coding ni leh support 5 item jerk.kalo ada 10 item mcm mn?

Share this post


Link to post
Share on other sites
[quote name='otai_g' timestamp='1314535682' post='1071607']
rs ada logical error sbb max item coding ni leh support 5 item jerk.kalo ada 10 item mcm mn?
[/quote]
blh gune statement "for" untuk masukkan input

Share this post


Link to post
Share on other sites
coding ko awal2 dah salah sbb dah setkan item kpd 5.kalo item ada ada lebih atau kurang dr 10 program akan error.kalo nak guna for bnyk kena renovate blk coding tu.alang2 aku wat baru jerk la.yg ni coding bantai semua guna global variable sbb mls nak pk pnjng ;)

[CODE]#include <iostream>
using namespace std;

int index;
double tot=0;
double* t;

void inputPrice() {

cout<<"\nENTER THE NUMBER OF ITEM : ";
cin>>index;

t = new double[index];

for (int a=0; a<index; a++) {
cout<<"\nPRICE FOR ITEM "<<a+1<<" : RM ";
cin>>t[a];
}
}

void countPrice() {

for (int b=0; b<index; b++) { tot = tot + t[b]; }
cout<<"\nTHE TOTAL PRICE : RM "<<tot<<endl;
}

void countDiscount() {

cout<<"\n20% DISCOUNT : RM "<<(0.2*tot)<<endl;
cout<<"\nTHE TOTAL PRICE AFTER DISCOUNT : RM "<<tot - (0.2*tot)<<endl;
}

void main () {

inputPrice();
countPrice();

if (tot>150) { countDiscount(); }
else {
cout<<"\nNO DISCOUNT!"<<endl;
cout<<"\nTHE TOTAL PRICE THAT HAVE TO PAY : RM "<<tot<<endl;
}
}[/CODE]

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...