Using Constant Variables in C++ for Shipping Cost Calculation

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Constant Variable
Click For Summary
SUMMARY

This discussion focuses on using constant variables in C++ for calculating shipping costs. The shipping cost formula includes a flat fee of 75 cents and a variable cost of 25 cents per pound, represented by the constant variable COST_PER_POUND. The correct implementation requires multiplying COST_PER_POUND by shipWeight to compute the total shipCost accurately. The use of constant variables enhances code readability and prevents unintended modifications.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with constant variables and their declaration
  • Basic knowledge of arithmetic operations in programming
  • Experience with compiling and running C++ programs
NEXT STEPS
  • Study the principles of constant variables in C++
  • Learn about best practices for code readability in programming
  • Explore more complex shipping cost calculations using C++
  • Investigate error handling in C++ for variable assignments
USEFUL FOR

C++ developers, programming students, and anyone interested in improving their coding practices related to constant variables and arithmetic operations.

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);
 

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 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K