C/C++ Using Constant Variables in C++ for Shipping Cost Calculation

Click For Summary
The discussion focuses on calculating shipping costs based on weight, emphasizing the importance of using constant variables for clarity and maintainability in code. The shipping cost formula includes a flat fee of 75 cents plus 25 cents per pound, which is represented by a constant named COST_PER_POUND. A sample program is provided, illustrating the use of constants to enhance code readability. The initial attempt at calculating the shipping cost was incorrect, as it failed to account for the weight of the package. The correct formula is highlighted: shipCost should be calculated as the flat fee plus the product of COST_PER_POUND and shipWeight. This reinforces the best practice of avoiding literal numbers in code to improve clarity and prevent errors.
ineedhelpnow
Messages
649
Reaction score
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
oh ok i see. its supposed to be
Code:
const int COST_PER_POUND = 25;
shipCost = FLAT_FEE+(COST_PER_POUND * shipWeight);
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K