c_programming

This is a discussion on c_programming within the C Programming forums, part of the General Programming Boards category; Hi I have a problem whit this code: Code: double charge =2; //2$ double houre; if(houre<=3) return charge; if(houre>3 && ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    c_programming

    Hi
    I have a problem whit this code:
    Code:
    double charge =2; //2$
    double houre;
    if(houre<=3)
    return charge;
    	 
     if(houre>3 && houre <=24)
       /* charge must  increase whit 0.5$
         every houre.???
       */

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    Please be more specific. What do you want this code to do? What exactly is your problem?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    A parking charge is 2 dollor for up to three houres. After 3 houres the charge increase
    0.5$ every houre. and we can not park more than 24 houres.
    My problem: if a person parks his car 5 houres.
    The charge=(fisrt 3 houre = 2$ +every houre 0.5$)=4$

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    Code:
    charge=2+(0.5*(houre-3))
    return charge
    That code would return the fee for anything more than 24 hours. You'll probably want to include some sort of error message if hours is more than 24.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    SlayerBlade
    Thank you very much for your help.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    but if I park my car 5.25 hourse.
    first: 5.25-3=2.25
    second: vi pay 0.5$ every houre after 3 hourse. For 2.25 hourse we must pay 3$
    because for 2 hourse we pay 2$ and 0,25 houre we should pay one dollor more.
    How do I complete this code?
    Code:
    charge=2+(0.5*(houre-3))
    return charge

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you think you'd do it? There are many ways. You do know that you can do things across multiple lines. You don't have to try and figure out one single line of code to do what you want.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I tried to work on it, but I think I have a problem with 'return'. If I have a 'printf' that states the value of X, the 'return' command for evaluating X should be written before or after the printf?
    Something else, how do you call upon a double? ("%.4f") ?
    Thanks

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When code reaches a 'return' statement, it immediately returns, or exits, the function. Thus:
    Code:
    int foo( void )
    {
        printf("This line happens.\n");
        return 0; /* This line happens, and the function immediately ends. */
        printf("This line will never happen.\n");
    }
    Here is a FAQ on using printf to format output.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21