Using Constant Variables in C++ for Shipping Cost Calculation

In summary, the conversation discussed the use of constant variables and their benefits in improving code readability and preventing changes to important values. The specific example given was to compute the cost of shipping a package based on its weight, with a flat fee of 75 cents and 25 cents per pound. The code provided to calculate the shipping cost correctly used the constant variable COST_PER_POUND and multiplied it by the package weight.
  • #1
ineedhelpnow
651
0
i don't even understand this...
Given shipWeight in pounds, compute the cost to ship a package and assign to shipCost. Shipping involves a flat fee of 75 cents, plus 25 cents per pound. Declare and use a const named COST_PER_POUND. Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int shipWeight = 10;
   int shipCost = 0;
   const int FLAT_FEE = 75;

   <STUDENT CODE>

   cout << "Shipping cost: " << shipCost << endl;
  
   return 0;
}

here's a little "review" about constant variables:

A good practice is to minimize the use of literal numbers in code. One reason is to improve code readability. newPrice = origPrice - 5 is less clear than newPrice = origPrice - priceDiscount. When a variable represents a literal, the variable's value should not be changed in the code. If the programmer precedes the variable definition with the keyword const, then the compiler will report an error if a later statement tries to change that variable's value. An initialized variable whose value cannot change is called a constant variable. A common convention, or good practice, is to name constant variables using upper case letters with words separated by underscores, to make constant variables clearly visible in code.

thanks EM for telling me bout the
Code:
 function. looks way better.[/SPOILER]

i tried this
[CODE]const int COST_PER_POUND = 25;
shipCost = FLAT_FEE+COST_PER_POUND;

one test passed but the other two didnt
 
Last edited:
Technology news on Phys.org
  • #2
oh ok i see. its supposed to be
Code:
const int COST_PER_POUND = 25;
shipCost = FLAT_FEE+(COST_PER_POUND * shipWeight);
 

FAQ: Using Constant Variables in C++ for Shipping Cost Calculation

What are constant variables in C++?

Constant variables in C++ are variables whose values cannot be changed once they are initialized. They are declared using the const keyword and can only be assigned a value once.

Why would I use constant variables for shipping cost calculation?

Using constant variables for shipping cost calculation ensures that the values used in the calculation remain consistent throughout the program. This helps to avoid any accidental changes to the values and ensures accurate results.

How do I declare and initialize a constant variable in C++?

To declare and initialize a constant variable in C++, you can use the const keyword followed by the variable's data type and name, and then assign a value to it using the equal sign. For example, const float SHIPPING_RATE = 2.50;

Can I change the value of a constant variable?

No, the value of a constant variable cannot be changed once it is initialized. Attempting to change the value of a constant variable will result in a compilation error.

Is it necessary to use constant variables for shipping cost calculation?

While it is not necessary to use constant variables for shipping cost calculation, it is considered good practice to do so. This helps to ensure the accuracy and consistency of the calculation and makes the code more readable and maintainable.

Similar threads

Replies
23
Views
2K
Replies
31
Views
2K
Replies
1
Views
1K
Replies
11
Views
4K
Replies
23
Views
2K
Replies
8
Views
2K
Replies
1
Views
3K
Back
Top