need homework help asap with if statement

This is a discussion on need homework help asap with if statement within the C Programming forums, part of the General Programming Boards category; So i have a homework due in a few hours and i think im screwed Here is what i want ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    need homework help asap with if statement

    So i have a homework due in a few hours and i think im screwed

    Here is what i want to do i need some help with the syntax

    comparing to values
    If they are the same then we go to end of program
    if they are not the same we continue on

    so i wrote

    Code:
    if (x==y) {return(0)};


    Also i tried the jump which i dont think works in C

    e.g

    Code:
    if (x==y){jump end};
    
    end
    return(0)



    Can someone please give me a hand its my first c aisgnment and our teacher is a loser and gave us way more then we can handle

    she has not even taught us how to use if and she expects us to be able to use it

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    exit(0) will exit the program and return 0 to the OS.
    exit - C++ Reference
    Code:
    if (x==y) {exit(0)};
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    ok what if i want it to print something right before it exits

    So instead of exit i want it to jump to a location

    Because i need it to print the answer to something right before it exits

    Basically if two things are equal i want it to display the result of the 2 things and then exit

    Is there a jump command of sorts for c?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    You could just put the print code inside the curly braces, but before the call to exit. Yes, there is a "jump" command, but it's "goto", not "jump". Using it is generally considered very poor practice, so I would avoid it whenever possible (you don't need it here). Why can't you use a loop that stop when x == y:
    Code:
    while x is not equal to y
        do whatever
    
    print result
    EDIT: Never mind my loop comment, I misread and thought you wanted to keep doing the same thing until x is equal to y.
    Last edited by anduril462; 01-27-2012 at 12:24 PM.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by stahta01 View Post
    exit(0) will exit the program and return 0 to the OS.
    exit - C++ Reference
    Code:
    if (x==y) {exit(0)};

    Also this did not work
    is exit propritary for the IDE you are using because it wont work for me

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's goto, but it's use often means your program is badly designed.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by automagp68 View Post
    Basically if two things are equal i want it to display the result of the 2 things and then exit
    Code:
    if( x == y ) {
        // display x and y
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Quote Originally Posted by automagp68 View Post
    Also this did not work
    is exit propritary for the IDE you are using because it wont work for me
    Nope, not proprietary. It's part of the C standard. Perhaps you aren't including the correct header for it to work.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to #include <stdlib.h> for exit.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I suspect "return 0" will work equally well in this case.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by anduril462 View Post
    Nope, not proprietary. It's part of the C standard. Perhaps you aren't including the correct header for it to work.
    hi yes is there a lib i need for it to work?

    i am only including stdio.h at the moment

    just so you guys know what im trying to do here is the asignment

    Im sure you will think this is super lame but this is the first thing ive ever tried in c and im completly stuck


    Write a program that asks the user to enter a U.S. dollar amount ant then show how to pay that amount using the smallest number of $20, $10, $5, and $1 bills:
    Enter a dollar amount:
    93
    $20 bills: 4 $10 bills: 1 $ 5bills: 0 $ 1bills: 3
    Hint: Divide the amount by 20 to determine the number of $20 bills needed and then reduce the amount by the total value of the $20 bills. Repeat for other bill sizes. Be sure to use integer values throughout, not floating-point numbers.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by oogabooga View Post
    You need to #include <stdlib.h> for exit.
    dunno why because i do have that included now and compiler still is throwing a fit

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by automagp68 View Post
    Write a program that asks the user to enter a U.S. dollar amount ant then show how to pay that amount using the smallest number of $20, $10, $5, and $1 bills:
    Enter a dollar amount:
    93
    $20 bills: 4 $10 bills: 1 $ 5bills: 0 $ 1bills: 3
    Hint: Divide the amount by 20 to determine the number of $20 bills needed and then reduce the amount by the total value of the $20 bills. Repeat for other bill sizes. Be sure to use integer values throughout, not floating-point numbers.
    You don't need any if statements, goto or exit for that, just divide by 20, 10, 5 and 1. And present the result of each step.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by oogabooga View Post
    There's goto, but it's use often means your program is badly designed.
    Here is a very nice candy. But eating it often may make one sick !

    (Seriously, I don't one another beginner to suffer like me...when I learnt about goto and held on to it until I joined this board and got educated. (AFAI remember, I once argued that if-goto was easier for me than 'for' loops !) )
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    yea but how does it know when we have reached the dollar amount of so it stops?

    like if the number is not even and we divide by 20

    and we got 4.75

    we display the 4 but when we go to divide the new result by 10 for the next dollar amount it is only going to divide the total by 10 not the total less what we used for 20

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help ASAP Please
    By asanchez1171 in forum C Programming
    Replies: 4
    Last Post: 10-14-2011, 01:10 PM
  2. Help asap!
    By tayexdrums in forum C Programming
    Replies: 17
    Last Post: 09-22-2011, 11:26 PM
  3. Please help ASAP
    By Korn1699 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-16-2001, 10:27 AM

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