Rick Robles
Rick Robles

Reputation: 21

Calculation not returning correct value

I was asked to write this program: "A software company sells a package that retails for $99. Quantity discounts are given according to the following table:

QUANTITY    DISCOUNT  
10-19       20%  
20-49       30%  
50-99       40%  
100 or more 50%

Write a program that asks for the number of units sold and computes the total cost of the purchase. Input Validation: Make sure the number of units is greater than 0"

This is what I have so far:

#include <iostream>
#include <string>           //String class- a string of text
#include <iomanip>          //Required for setw= the field width of the value after it
using namespace std;

int main()
{
    double sales, charges, numOfUnits = 0,
           rateA = .20, rateB = .30, rateC = .40, rateD = .50;

    //Set the numeric output formatting:
    cout << fixed << showpoint << setprecision(2);
    cout << "Enter the quantity for your order: ";
    cin >> sales;
            
    // Determine the discount:
    double PRICE=99.0;
    if (sales >= numOfUnits)
    if (sales >= 10 && sales <= 19 )
    rateA;
    charges = PRICE - rateA *sales;
    if (sales >= 20 && sales <= 49)
    rateB;
    charges = PRICE - rateB *sales;
    if (sales >= 50 && sales <= 99)
    rateC;
    charges = PRICE - rateC *sales;
    if (sales > 100 )
    rateD;
    charges = PRICE - rateD *sales;

    cout << "Your total price for this quantity is: $" <<charges 
         << " per unit."<< endl;
    cout << "That is an invalid number. Run the program again\n "
         << "and enter a number greater than\n" 
         << numOfUnits << ".\n";
} 

After compiling, the output does not give me the right answers. Maybe my math is wrong, or my flow is off? any suggestions?

I do not want anyone to write this for me, but maybe give me some pointers

Upvotes: 2

Views: 2833

Answers (3)

nullPointer
nullPointer

Reputation: 21

On a quick glance, I think the math is off a bit.

It should be :

double originalPrice = PRICE * sales;
if (sales >= 10 && sales <= 19 )
  charges = originalPrice - (rateA * originalPrice);
else if (sales >= 20 && sales <= 49)

and so on..

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129454

This sort of construct:

if (sales >= 50 && sales <= 99)
rateC;
charges = PRICE - rateC *sales;

does:

if (sales >= 50 && sales <= 99)
    rateC;

charges = PRICE - rateC *sales;

In other words, charges is always calculated with rateC, rather than only being so when the sales is in the relevant range. rateC inside the if-statement is also completely useless - it doesn't "do" anything with rateC, the it's just telling the compiler to "go look at this value, then throw it away" [which the compiler probably translates to "do nothing at all", because "looking" at rateC doesn't actually have any visible effect outside of that statement, so it can be removed].

Upvotes: 0

simonc
simonc

Reputation: 42185

You need to use braces {} around multi-line conditions

if (sales >= 10 && sales <= 19 )
    rateA;
    charges = PRICE - rateA *sales;

is actually

if (sales >= 10 && sales <= 19 )
    rateA;
charges = PRICE - rateA *sales;

i.e. rateA is executed conditionally and the update to charges is always executed.

Also, statements like rateA; have no effect so should either be updated or removed.

Upvotes: 4

Related Questions