C++ program doesn't execute correctly!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1610 Views - Last Post: 23 October 2012 - 10:13 PM Rate Topic: -----

#16 tedwards91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-October 12

Re: C++ program doesn't execute correctly!

Posted 22 October 2012 - 07:23 PM

No it doesn't, I changed it so it made logical sense and now it reads the correct floor types but now its not calculating everything correctly.

Here's the current code

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

   bool isValid (char floortype);
   int calculateNumBoxes (int squareFeet);
   void calculateReceipt (char floorType, int numBoxes, double& subTotal, double& tax , double& total, double& downPayment);
   void printReceipt (int numBoxes, double subTotal, double tax, double total, double downPayment);
   const double laminateRate = 2.59;
   const double engineeredRate = 3.19;
   const double woodRate = 3.99;
   const double maxDownPayment = 500;
   const double taxRate = 0.0925;
   const int squareFeetPerBox = 30;

int main()
{
   char floorType; // type of flooring
   int squareFeet;
   int numBoxes; // number of boxes to purchase
   double floorRate;
   double subTotal;
   double tax;
   double total;
   double downPayment;

   cout.setf(std::ios::fixed);
   cout.setf(std::ios::showpoint);
   cout.precision(2);

      cout <<"Welcome to Wood Floors R Us!\n";
      cout <<"To order your flooring, type\n";
      cout <<"\tL: Laminate ($2.59/sq ft)\n";
      cout <<"\tE: Engineered Hardwood ($3.19/sq ft)\n";
      cout <<"\tW: Solid Wood ($3.99/ sq ft)\n";
      cout <<"\tQ: to quit\n";
      cout <<"Floor type: ";
      cin >> floorType;
     
      while (floorType !='Q' && floorType !='q')
      {
          if (isValid (floorType))
          {
            do
            {
               cout <<"Square feet required: ";
               cin >> squareFeet;

               if (squareFeet <= 0)
               {  
                  cout <<"Square feet must be positive!\n";
               }
            } while (squareFeet <= 0);

         // Calculate number of boxes

            calculateNumBoxes (squareFeet); 

         // Calculate receipt values

            calculateReceipt (floorType, numBoxes, subTotal, tax, total, downPayment);   

         // Print receipt

            printReceipt ( numBoxes, subTotal, tax, total, downPayment);

          }
          else
          { 
             cout <<"Invalid floor type!\n";
             cout <<"Floor type: ";
             cin >> floorType;
          }
      }
return( 0 );
}

bool isValid (char floorType)
{
   if (floorType =='L' || floorType =='l' || floorType =='E' || floorType =='e' || floorType =='W' || floorType =='w') 
   {   
      return true;
   }
   else
   {
      return false;
   }
}

/* Preconditions: squarefeet is valid
   Postconditions: numBoxes is calculated corrrectly 
*/

int calculateNumBoxes (int squareFeet) 
{
   int numBoxes;

   numBoxes = squareFeet / 30;
   if (numBoxes % 30 !=0)
   {
      numBoxes++;
   }
   return numBoxes;
}   

/*  Preconditions: floorRate and numBoxes are  valid 
    Postconditions: subTotal, tax, total, downPaymant have been calculated correctly
*/
 
void calculateReceipt (char floorType, int numBoxes, double& subTotal, double& tax, double& total, double& downPayment)
{
   double floorRate;

   if (floorType == 'L' || floorType == 'l')
   {
      floorRate = laminateRate;
   }
   else if (floorRate == 'E' || floorRate == 'e')
   {
      floorRate = engineeredRate;
   }
   else
   {
      floorRate = woodRate; 
   }

   subTotal = numBoxes * squareFeetPerBox * floorRate;
   tax = subTotal * 0.0925;
   total = subTotal + tax;

   if ( total <= maxDownPayment)
   {
      downPayment = total;
   }
   else  
   {
      downPayment = maxDownPayment;
   }
}

/* Preconditions: numBoxes. subTotal, tax, total, downPayment are calculated correctly
   Postconditions: numBoxes, subTotal, tax, total, downPayment are printed correctly
*/
 
void printReceipt (int numBoxes, double subTotal, double tax, double total, double downPayment)
{
   cout <<"Number of boxes: " << numBoxes << endl;
   cout <<"Subtotal: $" << subTotal << endl;
   cout <<"Tax (9.25%): $" << tax << endl;
   cout <<"Total: $" << total << endl;
   cout <<"Required downpayment: $" << downPayment << endl;
   cout <<"Thank you for your patronage!" << endl;
}


Was This Post Helpful? 0
  • +
  • -

#17 ZenithMkII  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-September 12

Re: C++ program doesn't execute correctly!

Posted 23 October 2012 - 10:13 PM

Instead of testing for both the uppercase and lowercase letters you could use toupper() to make everything uppercase. Then all you would need to do is check for the uppercase stuff. Also a switch statement would help a lot. Then anything that wasn't on your list you can handle with the switch's default part. Just some suggestions.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2