#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Writing a program that increments by a certain amount.

Member Posts: 10
I try to write a program that will get the weight and then calculate the cost. If <=16 ounces, cost is \$3.00 (in this format). If >16 the cost increases for each increment of 4 ounces by .50. Can someone tell me what is wrong with my coding. The program give me a cost of 3 when I input a weight <16 but it gives me a negative number for weights >16.

#include
#include
#include

int main()
{
int wgt;
int x=0;
double cost;
int n = 4;
double i = 0.50;
double y;

cout <<"Enter the weight" <<endl;
cin >> wgt;

if (wgt <16)
cost = 3.00;

cout <<"The cost is" <<cost <<endl;
{
if
(wgt >16)
wgt = wgt +x;
x= n++;
y= i++;
cost = cost +y;

cout <<"Do you want to calculate the delivery";
cout <<" cost for another package?" <<endl;
}
return 0;

}

• Member Posts: 63
: I try to write a program that will get the weight and then calculate the cost. If <=16 ounces, cost is \$3.00 (in this format). If >16 the cost increases for each increment of 4 ounces by .50. Can someone tell me what is wrong with my coding. The program give me a cost of 3 when I input a weight <16 but it gives me a negative number for weights >16.
:
:
: #include
: #include
: #include
:
: int main()
: {
: int wgt;
: int x=0;
: double cost;
: int n = 4;
: double i = 0.50;
: double y;
:
: cout <<"Enter the weight" <<endl;
: cin >> wgt;
:
: if (wgt <16)
: cost = 3.00;
:
: cout <<"The cost is" <<cost <<endl;
: {
: if
: (wgt >16)
: wgt = wgt +x;
: x= n++;
: y= i++;
: cost = cost +y;
:
:
: cout <<"Do you want to calculate the delivery";
: cout <<" cost for another package?" <<endl;
: }
: return 0;
:
: }
:
:
Your positioning of {} make no sense at all. The only pair you have at the moment are redundant, since they do not belong to either if statement.

Somewhere, you need to have
[code]
if ( wgt < 16 ) {
// do something
} else {
// do something else
}
cout <<"The cost is" <<cost <<endl;
[/code]

• USAMember Posts: 4,624 ✭✭

_ { http://forcoder.org } free ebooks and video tutorials about // Delphi, Assembly, Objective-C, Swift, C#, PHP, C, Go, Java, Visual Basic .NET, Visual Basic, Ruby, Python, Perl, C++, Scratch, JavaScript, R, MATLAB, PL/SQL Scheme, SAS, VBScript, Fortran, ABAP, COBOL, FoxPro, LabVIEW, Alice, Ada, Scala, Awk, Julia, Logo, Hack, Clojure, F#, Prolog, ML, Rust, Apex, Erlang, Dart, D, Lisp, Bash, Lua, Kotlin, Crystal, Transact-SQL // ____________