Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
10/14/2009 3:12:54 PM EDT
Everything works now, I just have problems with the two outputs at the end of tax amount and total bill not being set to 2 decimal places
I thought I had rounding covered, but it doesnt work, any ideas?

i know generically speaking its:

double RoundToHundredths(double n)
{
   double d;
   int    i;

   /* rescale 123.45678 to 12345.678 */
   d = n * 100.0;
   /* round off: 12345.678 + 0.5 = 12346.178 -> 12346 */
   i = d + 0.5;
   /* restore to its original scale: 12346 -> 123.46 */
   d = (float)i / 100.0;

   return d;
}



#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

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

void showMenu();
int getData(int number);
double Round(double &totalTax);
void printCheck(double &tax, double &totalBill);
int choice=0; //    The number the user chooses for the item they want    
int number=0; // Used in the getData function
int x=0; // used in "for" loop to initialize numOrdered to 0.
double bill = 0.0;
double totalBill = 0.0; //  w/ tax
double tax = 0.0;  
double taxRate = .05; // tax%
double totalTax =0.0;
char shouldContinue;
   


int main()
{    
   

   theMenu[0].menuItem  = "#1  Plain Egg...........";    
   theMenu[1].menuItem  = "#2  Bacon and Egg.......";
   theMenu[2].menuItem  = "#3  Muffin..............";
   theMenu[3].menuItem  = "#4  French Toast........";
   theMenu[4].menuItem  = "#5  Fruit Basket........";
   theMenu[5].menuItem  = "#6  Cereal..............";
   theMenu[6].menuItem  = "#7  Coffee..............";
   theMenu[7].menuItem  = "#8  Tea.................";

   theMenu[0].menuPrice  = 1.45;    
   theMenu[1].menuPrice  = 2.45;
   theMenu[2].menuPrice  = 0.99;
   theMenu[3].menuPrice  = 1.99;
   theMenu[4].menuPrice  = 2.49;
   theMenu[5].menuPrice  = 0.69;
   theMenu[6].menuPrice  = 0.50;
   theMenu[7].menuPrice  = 0.75;

   for (x = 0; x < 8; x++) // Initialize theMenu.numOrdered to all 0's.  
   {
       theMenu.numOrdered = 0;
   }
   
   do
   {
       showMenu();  // run the void showMenu() function
       cout << "\n" << endl;

       choice = getData(number); // Returns the number the person chose the the array number

       bill = theMenu[choice].menuPrice + bill; // Keeps a running total of the bill
   
       theMenu[choice].numOrdered++; //Add 1 to the amount of the item ordered.  

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

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

cout << endl;

totalTax = bill * taxRate;

tax = Round(totalTax);
   
totalBill = bill + tax;

printCheck(tax, totalBill);
 
}


void showMenu()
{
   int x;
   for (x = 0; x <=8; x++)
   cout << theMenu.menuItem << "\t" << theMenu.menuPrice << endl;
}


int getData(int number)
{
int z;
z = 0;

cout << "Please choose a food item by the corresponding number." << endl;
cin >> z;
number = z - 1;

return number;
}
double Round(double &totalTax) //Round to the nearest cent.  
  {
   double tax;
 
   tax = totalTax * 100.0 + 0.5;
   return tax / 100.0;

}

void printCheck(double &tax, double &totalBill)
{
   int x;
   x = 0;

   cout << "Welcome Johnny's Restaurant\n" << endl;
   for (x = 0; x < 8; x++)
   {
       if (theMenu.numOrdered > 0)
       {
                 cout << theMenu.numOrdered << "\t" << theMenu.menuItem << "\t" << right << theMenu.menuPrice << right << endl;
       }
else
{
cout << "";
}
}

   cout << "Tax" << setw (27) << "$" << tax <<  endl;
   cout << "Amount Due" << setw (20) << "$" << totalBill  << endl;
cin.ignore(2);

}
10/14/2009 3:15:31 PM EDT
[#1]
Not sure how to do it in C++ syntax, but in C syntax:



printf("%.2lf", tax);




tax being the double variable you want to only print 2 decimals.
10/14/2009 3:19:32 PM EDT
[#2]
Look into setprecision in std::cout.
10/14/2009 3:20:36 PM EDT
[#3]
Quoted:
Not sure how to do it in C++ syntax, but in C syntax:

printf("%.2lf", tax);

tax being the double variable you want to only print 2 decimals.



i tried that, but it sets 2 siginificant digits
eg would be

$0.034
or $5.5
10/14/2009 3:23:12 PM EDT
[#4]
Make sure you have the '.' in front of '2lf'.



It may not put a trailing zero on 5.5 though, can't remember, but you could code that in by checking the number of digits after the . point if need be.
10/14/2009 3:29:52 PM EDT
[#5]
Quoted:
Make sure you have the '.' in front of '2lf'.

It may not put a trailing zero on 5.5 though, can't remember, but you could code that in by checking the number of digits after the . point if need be.


original output:

Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
Tax                          $0.0545
Amount Due                   $1.0445


here the 1 in front is how many i ordered






cout << "Tax" << setw (27) << "$" << printf("%.2lf", tax) <<  endl;
cout << "Amount Due" << setw (20) << "$" << printf("%.2lf", totalBill) << endl;

screwed up my output:


Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
0.05Tax                          $4
1.04Amount Due                   $4

which are the right values on the left side for some reason?
and lost the 1


ETA its tabbed correctly in the console, just lost it in copy and past

10/14/2009 3:34:26 PM EDT
[#6]



Quoted:



Quoted:

Make sure you have the '.' in front of '2lf'.



It may not put a trailing zero on 5.5 though, can't remember, but you could code that in by checking the number of digits after the . point if need be.




original output:



Welcome Johnny's Restaurant



1       #3  Muffin..............        0.99

Tax                          $0.0545

Amount Due                   $1.0445
cout << "Tax" << setw (27) << "$" << printf("%.2lf", tax) <<  endl;

cout << "Amount Due" << setw (20) << "$" << printf("%.2lf", totalBill) << endl;



screwed up my output:





Welcome Johnny's Restaurant



1       #3  Muffin..............        0.99

0.05Tax                          $4

1.04Amount Due                   $4



which are the right values on the left side for some reason?





ETA its tabbed correctly in the console, just lost it in copy and past





looks like printf() is firing off first.

 



There is a way to do it with cout and C++ syntax, but I am not familiar with C++.




using printf() only it would be:




printf("Tax $%.2lf\n", tax);

printf("Amount Due $%.2lf\n", totalBill);
10/14/2009 3:41:17 PM EDT
[#7]
Quoted:

Quoted:
Quoted:
Make sure you have the '.' in front of '2lf'.

It may not put a trailing zero on 5.5 though, can't remember, but you could code that in by checking the number of digits after the . point if need be.


original output:

Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
Tax                          $0.0545
Amount Due                   $1.0445







cout << "Tax" << setw (27) << "$" << printf("%.2lf", tax) <<  endl;
cout << "Amount Due" << setw (20) << "$" << printf("%.2lf", totalBill) << endl;

screwed up my output:


Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
0.05Tax                          $4
1.04Amount Due                   $4

which are the right values on the left side for some reason?


ETA its tabbed correctly in the console, just lost it in copy and past


looks like printf() is firing off first.  

There is a way to do it with cout and C++ syntax, but I am not familiar with C++.

using printf() only it would be:

printf("Tax $%.2lf\n", tax);
printf("Amount Due $%.2lf\n", totalBill);


cout <<  printf("Tax $%.2lf\n", tax) <<  endl;
   cout <<  printf("Amount Due $%.2lf\n", totalBill) << endl;

Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
Tax $0.05
10
Amount Due $1.04
17


im just plugging and chugging right now, its close.  Im trying to look up actual syntax
10/14/2009 3:52:00 PM EDT
[#8]

tax and totalBill are of type double, printf("%.2f", tax)
calls for a floating type
10/14/2009 3:53:07 PM EDT
[#9]
Get rid of the endl after each printf(), the \n inside the printf() does that.
10/14/2009 3:55:43 PM EDT
[#10]


Welcome Johnny's Restaurant

1       #3  Muffin..............        0.99
Tax $0.05
10Amount Due $1.04
17

pretty good, where do the 10 and 17 come from?
10/14/2009 3:58:02 PM EDT
[#11]
Calling cout << printf() is unnecessary and may be the cause of your weird output.

printf() itself will print to stdout.
10/14/2009 3:58:21 PM EDT
[#12]



Quoted:






Welcome Johnny's Restaurant



1       #3  Muffin..............        0.99

Tax $0.05

10Amount Due $1.04

17



pretty good, where do the 10 and 17 come from?


No idea, but to keep it C++ like, get rid of the \n in the printf()s and put the endl back in.

 


10/14/2009 4:03:05 PM EDT
[#13]
that did it, super
glad to be done with this program
thanks all
10/14/2009 4:06:15 PM EDT
[#14]



Quoted:


that did it, super

glad to be done with this program

thanks all


You may get dinged for using printf() though.