Passing Sruct Into Function Using C++


Assalamu'alaikum. Hari ini saya cuba kongsikan coding program yang saya buat. Ianya berkaitan dengan bahasa pengaturcraan C++. Sebagaimana tajuk di atas, program ini ingin menunjukkan bagaimana kita ingin menghantar data dari function main ke function yang lain menggunakan struct. Saya menggunakan TURBO C++ compiler.

Ini adalah soalan dari pensyarah saya. Tetapi telah diubah sedikit. Jawapannya officially dari saya. :)

Write a C++ program to help a local restaurant owner to automate his/her breakfast billing system. The program should do the following tasks:


  • Show the customer the different breakfast items offered by the restaurant.
  • Allow the customer to select one item and the amount of item purchased from the menu.
  • Calculate and print the bill.
Assume that the restaurant offers the following breakfast items:

Items
Prices/Unit
Nasi Lemak
RM1.50
Roti Canai Telur
RM2.00
Nasi Lemak Ayam
RM2.50
Roti Sardin + Milo Ais
RM3.00

Your program must contain at least the following functions:
·         Function showMenu: this function shows the different items offered by the restaurant and tells the user how to select the items.
·         Function CalculateTotal: this function calculates the total price to be paid by the customer. The total price should include a 5% tax.
·         Function printCheck: this function prints the check.

Your program should use a structure to store information about the customer’s purchase: menu item, item price, the amount of item, tax value and the total amount due.
A sample output is as follows:

Thanks for dining at  Kedai PakMat
Nasi Lemak                                  2          RM3.00
Tax                                                          RM0.15

Amount Due                                              RM3.15






#include<iostream>
#include<string.h>
#include <iomanip>
struct customer
{
 char menu_item[20];
 float price_item,tax_value,total_amount;       //struct customer
 int amount_item;

}customer;

void showMenu();
void CalculateTotal(struct customer);                                                                                     //function prototype
void printCheck(struct customer);

using namespace std;
int main()                                                                                                                            //main function
{
  showMenu();
  cin.getline (customer.menu_item,40);
  cout << "Please enter the amount of item :";
  cin >> customer.amount_item;

  if(strcmp(customer.menu_item,"NASI LEMAK")==0)
  {
                customer.price_item = 1.50;
                CalculateTotal(customer);
  }
  else if(strcmp(customer.menu_item,"ROTI CANAI TELUR")==0)
  {
                customer.price_item = 2.00;
                CalculateTotal(customer);
  }
  else if(strcmp(customer.menu_item,"NASI LEMAK AYAM")==0)
  {
                customer.price_item = 2.50;
                CalculateTotal(customer);
  }
  else if(strcmp(customer.menu_item,"ROTI SARDIN")==0)
  {
                customer.price_item = 3.50;
                customer.tax_value = 0;
                customer.total_amount =0;
                CalculateTotal(customer);
  }

  printCheck(customer);
  return 0;
 
}

void showMenu()
{

 cout << "-----------------------------------------------------"<<endl;
 cout << "|          ITEMS           |       PRICES/UNIT       |"<<endl;
 cout << "-----------------------------------------------------"<<endl;
 cout << "| 1)NASI LEMAK             |        RM 1.50          |"<<endl;
 cout << "-----------------------------------------------------"<<endl;
 cout << "| 2)ROTI CANAI TELUR       |        RM 2.00          |"<<endl;
 cout << "-----------------------------------------------------"<<endl;
 cout << "| 3)NASI LEMAK AYAM        |        RM 2.50          |"<<endl;
 cout << "-----------------------------------------------------"<<endl;
 cout << "| 4)ROTI SARDIN + MILO AIS |        RM 3.00          |"<<endl;
 cout << "-----------------------------------------------------"<<endl;
 cout <<endl<<endl;
 cout <<"Please enter the item : ";

}

void CalculateTotal(struct customer)
{

  customer.price_item = customer.price_item * customer.amount_item;
  customer.tax_value = customer.price_item * 0.05;
  customer.total_amount = customer.price_item + customer.tax_value;
}

void printCheck(struct customer)
{
                cout.setf(ios::showpoint);
                cout.setf(ios::fixed);
                cout <<"Thanks for dining at Kedai PakMat"<<endl;
                cout <<customer.menu_item <<"\t" <<customer.amount_item <<" \t RM"<<setprecision(2)<<customer.price_item<<endl;
                cout <<"Tax \t\t\tRM"<<setprecision(2)<<customer.tax_value<<endl<<endl;
                cout <<"Amount Due \t\tRM"<<setprecision(2)<<customer.total_amount;
}

Share this

Related Posts

Previous
Next Post »