mardi 31 mai 2016

program only executing first IF statement

I'm currently having an issue where my program will only execute the first if statement, where it multiplies 'w' by 3.5, even if 'w' does not meet the requirements. If I put an input of 4, the output turns into 14 (4*3.5) where it should really be 34 (4*8.5) since the weight is between 3 and 10 lbs. I'm sure the fix is simple, but I can't seem to find it!

#include <iostream>
using namespace std;

int main()
{
float w, price;
cout << "Enter weight of package : ";
cin >> w;

if (0 < w <= 1)
{
    price = 3.5 * w;
}
else if (1 < w <= 3)
{
    price = 5.5 * w;
}
else if (3 < w <= 10)
{
    price = 8.5 * w;
}
else if (10 < w <= 20)
{
    price = 10.5 * w;
}
else if (20 < w <= 30)
{
    price = 12.5 * w;
}
else if (30 < w)
{
    cout << "The package cannot be shipped" << endl;
}
else
    cout << "Invalid input" << endl;
cout << "Weight : " << w << " lbs" << endl;
cout << "Shipping cost : $" << price << endl;
}

Aucun commentaire:

Enregistrer un commentaire