Thursday, July 9, 2009

How do i write a c++ program using && operator along with if/else to deternime the shipping rate?

ive been given from 1lb up to 3lb cost $5.45 per 260 miles;


4 lb up to 7 lb cost $8.45 per 260 miles; 8 lb up to 15 lb cost $11.45 per 260 miles; and 16 lb uup to 23 lb cost $ 14.45 per 260 miles

How do i write a c++ program using %26amp;%26amp; operator along with if/else to deternime the shipping rate?
if(myWeight %26lt;= 3) {


value = myWeight * 5.45;


} else if (myWeight %26lt;= 7) {


value = myWeight * 8.45;


} else if (myWeight %26lt;= 15) {


value = myWeight * 11.45;


} else {


value = myWeight * 14.45;


}
Reply:Actually this is C, which is a subset of C++. There is nothing particular to C++ (v.s. "straight" C) required to solve this problem.





Please note that I have NOT tried to compile this code, so I can't guarantee that it will compile without errors. Also, error checking is very minimal - this isn't a particularly user-friendly program, but it does what you need.





That having been said:





/* Note that the weight of the package is passed in on the command line and retrieved via argv. argv[0] contains the program name itself, argv[1] contains the weight in pounds of the package, and argv[2] contains the miles to ship. */





#include "stdio.h"





#define RATE_FROM_1_TO_3LBS 5.45


#define RATE_FROM_4_TO_7LBS 8.45


#define RATE_FROM_8_TO_15LBS 11.45


#define RATE_FROM_16_TO_23LBS 14.45





#define MILEAGE_UNIT 260








void


main(int argc, char *argv[])


{


int Weight;


int Miles;


float ShippingCost = 0.0;





if(argc %26gt;= 3)


{


sscanf(argv[1],"%d",%26amp;Weight);


sscanf(argv[2],"%d",%26amp;Miles);


}


else


{


printf("\nNot enough arguments entered, try again");


exit();


}





if((Weight %26lt; 1) || (Weight %26gt; 23))


{


printf("\nPackage must be between 1 and 23 lbs weight");


exit();


}





if((Weight %26gt;= 1) %26amp;%26amp; (Weight %26lt;= 3))


{


ShippingCost = RATE_FROM_1_TO_3LBS * (Miles / MILEAGE_UNIT);


}





if((Weight %26gt;= 4) %26amp;%26amp; (Weight %26lt;= 7))


{


ShippingCost = RATE_FROM_4_TO_7LBS * (Miles / MILEAGE_UNIT);


}





if((Weight %26gt;= 8) %26amp;%26amp; (Weight %26lt;= 15))


{


ShippingCost = RATE_FROM_8_TO_15LBS * (Miles / MILEAGE_UNIT);


}





if((Weight %26gt;= 16) %26amp;%26amp; (Weight %26lt;= 23))


{


ShippingCost = RATE_FROM_16_TO_23LBS * (Miles / MILEAGE_UNIT);


}





printf("\nPackage Weight = %d lbs",Weight);


printf("\nMiles to ship = %d",Miles);


printf("\nShipping Cost = $%f",ShippingCost);


}

statice

No comments:

Post a Comment