Jump to content
Admin2

Masalah Dengan Constant

Recommended Posts

#include<stdio.h>
#define RATE1 1.20 //Alternative lain kalau nak setkan nilai Rate
#define RATE2 1.00 //Alternative lain kalau nak setkan nilai Rate

float get_payment(float);
void display(char[],float,float);


main()

{
    char name[100];

    float weight,payment;

    int i;

    for(i=0; i<3;i++) 
    {
    
        printf("--------------------------------------------\n");
        printf("          ENTER LAUNDRY INFORMATION\n");
        printf("--------------------------------------------\n");

        fflush(stdin);
        printf("Enter name: ");
        gets(name);

        printf("Enter weight of laundry (kg): ");
        scanf("%f", &weight);


        printf("--------------------------------------------\n");
        printf("         LAUNDRY PAYMENT INFORMATION\n");
        printf("--------------------------------------------\n");

        payment = get_payment(weight);
            
        display(name,weight,payment);








} //For loop punya end

}




float get_payment(float weight)


{
    float pay, ratePkg, rate;

    ratePkg = 0;
    if(weight>=0 && weight <=5) 
    
    {

          ratePkg = 1.20; //Masalah sekrang ialah nak tulis code ni kat line ni const ratePkg = 1.20. tapi bila set constant kat sini tak boleh return value.
    }    
    
    else if(weight>5) 
    {
    

        ratePkg = 1.00;   //Masalah sekrang ialah nak tulis code ni kat line ni const ratePkg = 1.00. tapi bila set constant kat sini tak boleh return value.
    }     
         
    

pay = weight * ratePkg;
        
    return pay;



    
}






void display(char name[],float weight,float payment)

{
    printf("Customer Name: %s\n", name);
    printf("Laundry Weight (kg): %.2f\n", weight);
    printf("Payment : RM%.2f\n", payment);

}

kalau kita set kan const ratePkg = 1.20; atau const ratePkg = 1.20; kat bahagian function get_payment camne? sebab soalan dia suruh setkan constant dalam function get_payment cam kat bawah ni:

Question 3

You are required to create a complete program for UNIVERSITY LAUNDRY.

• In main() function :

o Get user input for name and amount of laundry kilogram.

o Call function get_payment(...) and send amount of kilogram to calculate the payment for the service.

o Call function display() to display the customer's name, kilogram and payment.

• In function get_payment(...) :

o Get the amount of kilogram from main() function.

o Calculate the payment by referring to the following table.

o The rate per kilogram must be DEFINE AS CONSTANT <----- NI LA DIA!!

KILOGRAM RATE PER KILOGRAM

0 – 5 RM 1.20

> 5 RM 1.00

• In function display() :

o display the customer's name, kilogram and payment

• This program must be repeated for three customers.

• Use a for loop for this process.

Share this post


Link to post
Share on other sites

bila dia cakap

The rate per kilogram must be DEFINE AS CONSTANT

biasanya dia maksudkan gunakan #define directive. So solution awak biasanya

#define RATE1 1.20
#define RATE2 1.00

Share this post


Link to post
Share on other sites

float get_payment(float weight)
{
    float pay, ratePkg, rate;

    ratePkg = 0;
    if(weight>=0 && weight <=5) 
    
    {

          ratePkg = 1.20; //Masalah sekrang ialah nak tulis code ni kat line ni const ratePkg = 1.20. tapi bila set constant kat sini tak boleh return value.
    }

yup...spt ckp Paralys3r....

satu lg...ratePkg bkn x return value, tp kalu ko wat "const ratePkg = 1.20", sebenarnya, function tu x akan assign ratePkg tu kpd nilai 1.20 sbb ko declarekan dia sbg constant...dia akan tetap spt nilai sebelum ni (i.e: ratePkg = 0 yg ko da assign awal2 tu)...

Share this post


Link to post
Share on other sites

thanx paralys3r dan betik. ye aku dah faham dah. so aku kena guna gak la #define ye. ookk

Share this post


Link to post
Share on other sites

x payah guna define pun blh.ni cth yg aku buat

#include <iostream>

using namespace std;

//#define RATE1 1.20 //Alternative lain kalau nak setkan nilai Rate
//#define RATE2 1.00 //Alternative lain kalau nak setkan nilai Rate

double get_payment(float);
void display(char*,float,float);

void main()
{
    char name[100];
    float weight, payment;
    int i;

    for(i=0; i<3; i++)
    {
    
        cout<<"--------------------------------------------"<<endl;
        cout<<"          ENTER LAUNDRY INFORMATION         "<<endl;
        cout<<"--------------------------------------------"<<endl;

        cout<<"Enter name : ";
        cin>>name;

        cout<<"Enter weight of laundry (kg) : ";
        cin>>weight;

        cout<<"--------------------------------------------"<<endl;
        cout<<"         LAUNDRY PAYMENT INFORMATION        "<<endl;
        cout<<"--------------------------------------------"<<endl;

        get_payment(weight);
         
        payment = get_payment(weight);
        display(name,weight,payment);
    
    } //For loop punya end

} //main punya end


double get_payment(float weight)
{
    float pay = 0;
    const double ratePkg_1 = 1.2;
    const double ratePkg_2 = 1;

    if (weight>=0 && weight <=5)    
           pay = weight * ratePkg_1; 
            
    else 
        pay =  6 + ( (weight-5) * ratePkg_2 );   
    
    return pay;  
   
} // end get_payment function


void display(char name[], float weight, float payment)
{
    cout<<"Customer Name : "<<name<<endl;
    cout<<"Laundry Weight (kg) : "<<weight<<endl;
    cout<<"Payment : RM "<<payment<<endl;
}

Edited by otai_g

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...

×
×
  • Create New...