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);
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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