document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*input text file named guest.dat contains
  2. a list of guests\' name, payment type(Master Card, or Visa or Cash);
  3. room charge per day and number of days stayed in a hotel. The data is for a specific month
  4. of the year as shown if Figure 1:
  5.        Aiman Hakeem
  6.        Master Card
  7.        250.0 7
  8.        
  9.        Tasmin Munirah
  10.        Visa
  11.        210.0 10
  12.        
  13.        Ali Abdul
  14.        Cash
  15.        150.0 3
  16.        
  17.        Anis Syifa
  18.        Master Card
  19.        210.0 7
  20.     Figure 1: The data written in guest.dat file
  21.        
  22. The program will read data from the input text file and produce the report as shown
  23. in figure 2 on the output text file named report.dat.
  24.    GUEST NAME     PAYMENT TYPE    TOTAL PRICE
  25.    Aisman Hakeem  Master Card     RM 1575.0
  26.    Tasnim Munirah Visa            RM 1785.0
  27.    Ali Abdul      Cash            RM  450.0
  28.    Anis Syifa     Master Card     RM 1323.0
  29.    
  30.    The total monthly collection is : ---> RM 5133.0
  31.  
  32.    The number of hotel guests is: 4
  33.    The number of Master Card users is: 2
  34.    The number of Visa users is : 1
  35.  
  36.  Figure 2: The report produced on report.dat file
  37.  
  38.  The information printed includes guest\'s name, payment type, and total charge. The formulas are as follows:    
  39.      Total charge = room charge * number of days stated
  40.      the discount of the total charge entitled for each guest will be given based on the following table:
  41.      Number of days stayed     Discount(%)
  42.        1 to 4                       0
  43.        5 to 9                      10
  44.        more than 9                 15
  45.        
  46.  Write a complete C++ program to calculate and produce the report as shown in Figure 2 above.
  47. */
  48. #include <iostream>
  49. #include <fstream>
  50. #include <iomanip>
  51. using namespace std;
  52.  
  53. int main()
  54. {
  55.     char name[20];
  56.     char paymentType[20];
  57.     float charge;
  58.     int days;
  59.     int count=0,masterCard=0,visa=0;
  60.     float total=0.0,discount,payment;
  61.    
  62.     ifstream inf("guest.dat");
  63.     ofstream outf("report.dat");
  64.     if (!inf)
  65.     {
  66.        cout << "Fail to open input file--> guest.dat" << endl;
  67.        exit(1);
  68.     }
  69.     if (!outf)
  70.     {
  71.        cout << "Fail to open output file--> report.dat" << endl;
  72.        exit(1);
  73.     }
  74.     outf << setiosflags(ios::left) << setw(20) << "GUEST NAME"
  75.          << setw(20) << "PAYMENT TYPE"
  76.          << "TOTAL PRICE" << endl;
  77.     while (!inf.eof())
  78.     {
  79.           inf.getline(name,20);
  80.           inf.getline(paymentType,20);
  81.           inf >> charge;
  82.           inf >> days;
  83.           outf << setiosflags(ios::left) << setw(20) << name
  84.                << setw(20) << paymentType;
  85.           if (strcmp(paymentType,"Master Card")==0)
  86.              masterCard+=1;
  87.           else if (strcmp(paymentType,"Visa")==0)
  88.                visa+=1;
  89.          
  90.           if (days>9)    
  91.              discount = 0.15;
  92.           else if (days>=5&&days<=9)
  93.                discount = 0.1;
  94.           else
  95.               discount =0.0;
  96.           payment = (1-discount) * charge * days;
  97.           total+=payment;
  98.           outf << "RM "<< setiosflags(ios::fixed) << setprecision(1) << payment;
  99.           outf << endl;
  100.           count++;
  101.           inf>>ws;
  102.     }
  103.     outf << endl;
  104.     outf << "The number of hotel guests is: " << count << endl;
  105.     outf << "The number of Master Card users is: "<< masterCard << endl;
  106.     outf << "The number of Visa users is : " << visa << endl;
  107.     inf.close();
  108.     outf.close();
  109.     cin.get();
  110.     return 0;
  111. }
');