Thread: help with if and else statements

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    Post help with if and else statements

    I am trying to write this program to show that all customers get a 30% discount and if you are a student and pay in cash then if answer is yes, you will get an additional 20%. This is what I have and am really stuck. Have went over my chapters as well as online and seem to be in a rut. Can any help?

    This is what I have:

    #include <stdio.h>

    #define SALERATE 0.3
    #define STURATE 0.2

    int main (void)
    {
    float PUR, /* Total price of purchases */
    SALEDIS, /* Primary sale discount */
    STUDIS, /* Student/Cash discount */
    NET; /* Net amount to pay after discounts */
    char STU, /* Student status indicator (Y/N)? */
    CASH; /* Case sale indicator (Y/N)? *)

    printf ("Ace Sports Bill\n\n");

    printf ("Total price of purchases? "); scanf (%lf, &PUR);
    printf ("Is this customer a student (Y/N)? "); fflush (stdin); scanf ("%c", &STU);

    if (STU = = 'Y' || STU = = 'y') STUDIS = PUR * SALERATE; PUR - SALERATE * STURATE;

    (This is where I am stuck, I do not know if the STU is correct and where to go to from that point on)

    Thanks
    Kathryn

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    scanf (%lf, &PUR);
    You are missing quotes around the format string.

    Code:
    fflush (stdin);
    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    Code:
    if (STU = = 'Y' || STU = = 'y') STUDIS = PUR * SALERATE; PUR - SALERATE * STURATE;
    Only the first statement after that if will be run if the conditional is true. The second statement will always be run. Did you mean to do this instead?
    Code:
    if (STU = = 'Y' || STU = = 'y') {
        STUDIS = PUR * SALERATE; 
        PUR - SALERATE * STURATE;
    }
    To avoid problems like this, it is a good idea to never put more than one statement on a line.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    First, this is C code, not C# code, so you're in the wrong forum.

    Second, you should always use code tags when posting code.

    Third, read this: Why fflush(stdin) is wrong.

    Fourth, what do you mean by "I don't know if the STU is correct?"

    You probably need/want to create a block after your if statement.

    Code:
    if (whatever)
    {
        /* Block of code to handle when whatever is true */
    }
    else if (whosiewhatsie)
    {
        /* Block of code to handle when whosiewhatsie is true */
    }
    else
    {
        /* Block of code to handle when neither whatever, nor
            whosiewhatsie are true */
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Hello,

    Do not forget to use code tags while posting code also as u r using C language use the C furum.

    anyway

    you can write the main part of ur code as follow:

    1- total = cost while u have calculated the 30% disc
    2-
    if ((student) AND (pay in cash))
    total = cost while u calculate additional disc


    Good luch
    Arian

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    Thanks Bithub

    Thank you for helping me with this. Our assignment was to draw a flowchart. Examples that were given did not include the Symb constant, so therefore I was even more confused as to what to do with it.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    Thank you for helping me. We are suppose to do a flowchart and the example nor my book showed how to do this with the symb constants. I got a bit confused. So each time I ask the question of whether a student and if pays in cash, I need to enclose this in braces?

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Quote Originally Posted by kburyanek View Post
    I need to enclose this in braces?

    There are several ways to write the conditional statement that u need. like:

    Code:
    if((student) AND (payInCash))
    {
        your code
    }
    Code:
    if(student)
        if(payInCash)
       {
            your code
        }

Popular pages Recent additions subscribe to a feed