C++/c++

Advertisement


Question
i do nt knw which part of the code is wrong.wen i print the bill,it did nt calculate the quantity of the item,tax and amount due.example wen i order 2 bacon it should give mi 3.00 instead of 1.50.for the new order function,it should discard any item that is already ordered and prepare for a new order.do nt print out the bill for the discarded items.i am so stuck at it.i do nt knw hw to start with it.could you pls help mi!!i would be appreciated for all the help frm you!!!!

output:
Enter your option?
(a}Purchase item
(b)Check out and print bill
(c)New order

Your selection is: a

       Cafe Menu
       *********

#1  Bacon       1.50
#2  Eggs        1.00
#3  Sausage     1.50
#4  Coffee      1.80
#5  Tea         1.80

Please select an item you would like to purchase: 1

1          #1  Bacon

Would you like to place another order?  (Y/N)  y

Please select an item you would like to purchase: 1

2          #1  Bacon

Would you like to place another order?  (Y/N)  n

Your ordered are:

2     #1  Bacon
0     #2  Eggs
0     #3  Sausage
0     #4  Coffee
0     #5  Tea

Enter your option?

(a)Purchase Items
(b)Check out and print bill
(c)New order
(q)Quit

Your selection is: b

2          #1  Bacon          1.50
Tax          $0.00
Amount Due          $0.00

         
code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;


struct menuItemType
{
  string menuItem;
  double menuPrice;   
  int numOrdered;
};
  menuItemType theMenu[5];

void purchaseItem();
int getData(int choice);
double Round(double &totalTax);
void printBill(double &tax, double &totalBill);
void newOrder();
void quit();

int main()
{   
  int choice;    
  int number;
  int x;
  double bill;
  double totalBill;
  double tax;
  double taxRate;  
  double totalTax;
  char shouldContinue;
  
  fstream inData;
  
   cout.setf(ios::fixed, ios::floatfield);
   cout.setf(ios::showpoint);
   cout<<setprecision(2);
  
   inData.open("items.dat");

   if(!inData)//error checking...if can't find file program terminates
   {
       cout<<"Can't find items.dat...program will close."<<endl;
       system("pause");
       return 0;
   }

  totalBill = 0;
  taxRate = .05;
  tax = 0;
  totalTax = 0;
  bill = 0;
  number = 0;
  choice = 0;
  x = 0;

  
   theMenu[0].menuItem  = "#1  Bacon";   
  theMenu[1].menuItem  = "#2  Eggs";
  theMenu[2].menuItem  = "#3  Sausage";
  theMenu[3].menuItem  = "#4  Coffee";
  theMenu[4].menuItem  = "#5  Tea   ";
  

  theMenu[0].menuPrice  = 1.50;   
  theMenu[1].menuPrice  = 1.00;
  theMenu[2].menuPrice  = 1.50;
  theMenu[3].menuPrice  = 1.80;
  theMenu[4].menuPrice  = 1.80;

  for (x = 0; x < 5; x++)
  {
     theMenu[x].numOrdered = 0;
  }
  
  char option;
      
   while (option != 'q')

  {
    
         cout << "          WELCOME TO TERRY'S CAFE\n";
       cout << "          -----------------------\n\n";
       cout << "Enter your option?\n\n";
         cout << "(a)Purchase Items\n";
         cout << "(b)Check out and print bill\n";
       cout << "(c)New order\n";
       cout << "(q)Quit\n\n";
       cout << "Your selection is: ";
       cin  >> option;
         cout << endl;
       cin.clear();
         cin.ignore(80,'\n');
         
         
      switch(option)
    {
         case 'a':  
         purchaseItem();
         break;
       case 'b':
         printBill(tax,totalBill);
         break;
       case 'c':
         newOrder();
         break;
        case 'q':
         quit();
         break;
         default:
         cout <<endl;
         break;
    }
  }
  
  
  return 0;
   
 
}

void purchaseItem()
{
  int choice;
  int number;
  int x;
  double bill;  
  double totalBill;
  double tax;  
  double taxRate;
  double totalTax;
  char shouldContinue;
  char c;
  
  cout <<"\tCafe Menu"<< endl;
  cout <<"\t*********"<< endl;
  cout << endl;
  for (x = 0; x <=4; x++)
  cout << theMenu[x].menuItem << "\t" << theMenu[x].menuPrice << endl;
  
  do
  {
  choice = getData(number);

  bill = theMenu[choice].menuPrice + bill;
  
  theMenu[choice].numOrdered++;   

  cout << "\n" << theMenu[choice].numOrdered << setw (27) << theMenu[choice].menuItem << " \n" << endl;

  cout << "Would you like to place another order?  (Y/N)  ";
  cin >> shouldContinue;
   }
  while (shouldContinue == 'Y' || shouldContinue == 'y');
  while (shouldContinue == 'N' && shouldContinue == 'n');
  cout << endl;
      
}

int getData(int number)
{
  int z;
  z = 0;
  cout << endl;
  cout << "Please select an item you would like to purchase: ";
  cin >> z;
       z = z - 1;       

  return z;   
}

double Round(double &totalTax)  
{
  int tax;
  
  tax = totalTax * 100.0 + 0.5;
  return tax / 100.0;
}

void printBill(double &tax, double &totalBill)
{
  int x;
  x = 0;
  double bill; //   This is the total for items purchased, without tax.
  double taxRate; // Percentage of tax to be charged.

  cout << "Welcome to Terry's Cafe \n" << endl;
  for (x = 0; x < 5; x++)
  {
  if (theMenu[x].numOrdered > 0)
  {
  cout << theMenu[x].numOrdered << "\t\t" << theMenu[x].menuItem << "\t\t" << right
  << theMenu[x].menuPrice << right << endl;
  }
  else
  {
     cout << "";
  }
  }
   
  cout << "Tax" << setw (27) <<"\t\t" <<"$" << tax << endl;
  cout << "Amount Due" << setw (20) <<"\t\t" <<"$" << totalBill << endl;
  cout << endl;
}  

Answer
Dear Grace:

You add up the bill but you do not use the result.  You should pass totalbill into purchaseItem ( double & totalbill ) and calculate it (add it up) so that when you print the bill totalbill will not be zero.

You do not calculate tax at all.  Once you do you must add it to the totalbill.

You do not multiply the item price by the number ordered.  You can do that as you print the bill if you like.  Or you can do it ahead of time say in purchaseItem.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.