Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

if statements in C programming

Name: new to c 2011-02-09 15:23

#include <stdio.h>
#define TAX_RATE 0.065

int main () {
   
    double cost;
    int quantity, taxed, weight, cost_with_shipping;
   
    printf("What is the cost of the item to be purchased (in dollars)?\n");
    scanf("%lf", &cost);
    printf("What is te weight of the item (in pounds)?\n");
    scanf("%d", & weight);
    printf("How many of the item are you purchasing?\n");
    scanf("%d", &quantity);
    printf("Is the item a taxed item (1 = yes, 0 = no)?\n");
    scanf("%d", & taxed);
       
    if (weight > 0 && weight <= 10);
    cost_with_shipping = (weight * 4) + cost;
    double total_cost = cost_with_shipping*quantity*(1+ taxed*TAX_RATE);   
    printf("Your total purchase, with shipping, will cost $%.21f.\n", total_cost);
   
    else (weight > 10 && weight <= 30);
    cost_with_shipping = (weight * 2.5) + cost;
    double total_cost = cost_with_shipping*quantity*(1+ taxed*TAX_RATE);
    printf("Your total purchase, with shipping, will cost $%.21f.\n", total_cost);
   
    else (weight > 30 && weight <= 50);
    cost_with_shipping = (weight * 1.5) + cost;
    double total_cost = cost_with_shipping*quantity*(1+ taxed*TAX_RATE);
    printf("Your total purchase, with shipping, will cost $%.21f.\n", total_cost);
   
    else (weight > 50);
    cost_with_shipping = (weight * .99) + cost;
    double total_cost = cost_with_shipping*quantity*(1+ taxed*TAX_RATE);
    printf("Your total purchase, with shipping, will cost $%.21f.\n", total_cost);
   
    system("PAUSE");
    return 0;
}

Name: OP 2011-02-09 15:25

I forgot to state the problem, I'm still new to C, and I can't seem to get a handle on the IF statements, can anyone offer a suggestion to a fix, or see if they can at least explain it to me simply.

Name: Anonymous 2011-02-09 15:37

>>2
``sperg''

Name: OP 2011-02-09 15:40

>>2
I've only been taught system("PAUSE"), I don't know any alternatives.

Name: OP 2011-02-09 16:04

>>9
I see what I was doing wrong (calculating the total cost every time), but what you've posted doesn't work for a weight that isn't a full number.  How could I set this up so it will accept all weight values?

Name: OP 2011-02-09 16:30

>>11
Basically anything with a decimal. A number such as 32.5 or 54.7

Name: VIPPER 2011-02-09 16:38

>>12
Use GMP.
C can natively only handle ints and floats.
Or implement your own.

Name: FLW_FTW 2011-02-09 19:01

Two things: 1, take out the semi-colon after the if statement.
2. You need to enclose the code under the if statements in brackets to that code is assigned to a particular if-statement.

You have:

if( x > y);
    code
    code
    code
    code


You should have:

if( x > y )
{
    code
    code
    code
    code
}


The semi-colon after the if statement is treated as it's own line of code. In if statements without brackets, only the first line of code beneath the if-statement is executed:

--No bracket if-statement--

if( x == y )
    do_something(); /*this line is executed if x == y*/
    do_something_else() /*this line is not*/
[code]

--bracket if-statement--
[code]
if( x == y )
{
    do_something(); /*this line is executed if x == y*/
    do_something_else(); /*so is this line*/
    do_a_third_thing(); /*so is this line*/
}


Make sense?

Name: Anonymous 2011-02-09 23:08

>>17
stmtmtmtmtmt

Name: Anonymous 2011-02-10 0:38

Let us not forget the ternary conditional

condition ? true_statement : false_statement

Also, OP, if you don't get if/else statements down fast, you're doomed as a programmer.
The idea should be clear to you right?
you're trying to find out

if(this_value_is_true) {
    then_we_do(this);
    and_we_do(this);
} else we_do(this);


Curly braces are like extended versions of mathematical parenthesis
Not always necessary, but generally needed to make your point clear

Name: Anonymous 2011-02-10 2:55

>>17
stmt = code ';' | block ';'* | if ';'*
FTFY

Name: Anonymous 2011-02-10 4:57

>>17,20
(6.8.4) selection-statement:
        if (expression )statement
        if (expression )statement elsestatement
        switch (expression )statement

Name: Anonymous 2011-02-10 4:59

>>21
(6.8.4) selection-statement:
        if ( expression ) statement
        if ( expression ) statement else statement
        switch ( expression ) statement

Name: Anonymous 2011-02-10 5:10

>>21-22
(1.3.3.7) expression:
  statement

Don't change these.
Name: Email:
Entire Thread Thread List