1

I was trying to use if statement to check the number entered fits in the range I am allowing. But apparently it is not working. Here's my if statement

The user inputs the weight and I check if it's between the small/top weight

  const float SMALL_WEIGHT        = 2.3f;      // low end 1st range weight
  const float TOP_SMALL_WEIGHT    = 4.3f;      // top end 1st range weight

  cout << "Enter the weight of the laptop: ";
  cin >> weight;

            if ( weight < SMALL_WEIGHT && weight > TOP_SMALL_WEIGHT )
           {
              cout << "The weight " << weight << " is not between "
                   << SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
              return 1;
           }

I tried using logical or || but it's still not working.

edit: this is the whole code

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

const float SALE_START_PRICE    = 500.0f;    // discount if more than 500
const float DISCOUNT_PCT        = .15f;      // 15% discount
const float TAX_PCT             = .075f;     // tax is 7.5%

const float SMALL_WEIGHT        = 2.3f;      // low end 1st range weight
const float TOP_SMALL_WEIGHT    = 4.3f;      // top end 1st range weight
const float TOP_MID_WEIGHT      = 5.8f;      // top 2nd range weight
const float TOP_WEIGHT          = 7.9f;      // top 3rd range weight

const float SMALL_SHIP          = 5.4f;      // 1st range basic ship charge
const float MID_SHIP            = 5.8f;      // 2nd range basic ship charge
const float TOP_SHIP            = 6.2f;      // 3rd range basic ship charge

const float SMALL_SHIP_PCT      = .025f;
const float MID_SHIP_PCT        = 2.75f;
const float TOP_SHIP_PCT        = 3.5;

int main()
{
   float    price, weight;
   float    discountAmount, salePrice;                              
   float    taxAmount, shippingCharges, totalSaleAmount;

   cout << "Enter the original price of the laptop: ";             
   cin  >> price;

   cout << "Enter the weight of the laptop: ";
   cin >> weight;


   if ( weight < SMALL_WEIGHT && weight > TOP_SMALL_WEIGHT )
   {
      cout << "The weight " << weight << " is not between "
           << SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
      return 1;
   }

   if ( price > SALE_START_PRICE )
   {
      discountAmount =  price * DISCOUNT_PCT;
   }
   else
       discountAmount = 0.00;

   taxAmount = price * TAX_PCT;
   salePrice = price - discountAmount;


   if (weight < TOP_SMALL_WEIGHT)
      shippingCharges = SMALL_SHIP + ( salePrice * SMALL_SHIP_PCT );
   else if ( weight >= TOP_SMALL_WEIGHT && weight < MID_SHIP )
      shippingCharges = MID_SHIP + ( salePrice * MID_SHIP_PCT);
   else
      shippingCharges = TOP_SHIP + ( salePrice * TOP_SHIP_PCT );

   totalSaleAmount = salePrice + taxAmount + shippingCharges;

   cout <<  fixed << showpoint << setprecision(2);
   cout << "The original price : $" << price << endl;
   cout << "The discount       : $" << discountAmount << endl;
   cout << "The sale price     : $" << salePrice << endl;
   cout << "The sales tax      : $" << taxAmount << endl;
   cout << "The shipping       : $" << shippingCharges << endl << endl;
   cout << "The total sale     : $" << totalSaleAmount << endl;

   return 0;                                      
}                             
5
  • 3
    || should work. Maybe you should post the full code. Commented Feb 18, 2014 at 17:11
  • What type is weight declared as? Commented Feb 18, 2014 at 17:13
  • Float, I posted the whole code Commented Feb 18, 2014 at 17:15
  • Or better, define not working. Is return 1; is what not working means? Commented Feb 18, 2014 at 17:17
  • you can see this : stackoverflow.com/questions/10334688/… stackoverflow.com/questions/17333/… Commented Feb 18, 2014 at 17:26

1 Answer 1

3

weight cannot be both smaller than SMALL_WEIGHT and bigger than TOP_SMALL_WEIGHT.

I think you want to use a logical or || So it becomes:

if (weight < SMALL_WEIGHT || weight > TOP_SMALL_WEIGHT)
{
    cout << "The weight " << weight << " is not between "
         << SMALL_WEIGHT << " and " << TOP_WEIGHT << endl;
    return 1;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I was just about to press post of the exact same answer, :P.
Thank you all, when I changed it to or. I try and put the weight to 5.5 (which is between the range) it gives the error message as if I entered something outside of it.
@na9er 5.5 is outside the range according to the code you posted.
@k4rlsson excuse my ignorance. I see my mistake now
For range I would use && as it will also work for comparing multiple ranges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.