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

AI Thread 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);
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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