Thread: If, else in a function?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    If, else in a function?

    So I am trying to do a simple subtraction problem where the larger number would go first so the difference can be a positive or zero, but I am stump. If I just remove the if and else with either x - y or y - x it would work. It is something in my function from what it seems.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int sub ( int x, int y );
    
    int main()
    {
      int x;
      int y;
    
    	cout<<"We will subtract two numbers and it will always be positive or zero.";
    	cin.ignore();
    	cout<<"Please input two numbers: ";
    	cin>> x >> y;
    	cin.ignore();
    	cout<<"The difference is "<< sub ( x, y ) <<"\n";
    	cin.get();
    }
    
    int sub ( int x, int y )
    {
    	if ( x < y ) {
    		return y - x;
    	}
    	else ( x > y ) {
    		return x - y;
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Else should not have a condition. Just write else, followed by the curly brace.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's either
    else if (condition)
    Or
    else
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    int sub ( int x, int y )
    {
    	if ( x < y ) {
    		return y - x;
    	}
    	else ( x > y ) {
    		return x - y;
    	}
    }
    Or just replace "else" with "if".
    Just GET it OFF out my mind!!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, that's not good, because this is a if or else if type of logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Thanks guys, else if works, but I did it before and it didn't work. I must have missed a semi colon before.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What about the case where neither if condition is met (for example, if I input 5 and 5)?

    Don't forget to come up with a solution for that (hopefully you can figure it out before somebody gives it to you).


    >> No, that's not good, because this is a if or else if type of logic.
    Since the blocks contain a return statement, audinue's suggestion is logically equivalent to an if/else if. Whether it's better or worse for readability is probably a debate for another thread.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    >> What about the case where neither if condition is met (for example, if I input 5 and 5)?

    I did this, seems to work, I don't know if there is any way around it.

    Code:
    int sub ( int x, int y )
    {
    	if ( x < y ) {
    		return y - x;
    	}
    	else if ( x > y ) {
    		return x - y;
    	}
    
    	else  {
    		return 0;
    	}
    
    }
    http://www.mediafire.com/?myynmdyqmmz
    Last edited by ShinobiRAGE; 08-29-2009 at 04:50 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I don't know if there is any way around it.
    There are other ways, but that's just fine.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    >> No, that's not good, because this is a if or else if type of logic.
    Since the blocks contain a return statement, audinue's suggestion is logically equivalent to an if/else if. Whether it's better or worse for readability is probably a debate for another thread.
    I know it is and in this case it might be fine, but in others it might not, since in if, all ifs are executed and checked, but in else/if, only the if/else ifs down to the point where the condition is true is evaluated.
    Therefore, I suggest separating if and else if logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What about the case where neither if condition is met (for example, if I input 5 and 5)?
    Wouldn't this be handled automatically if you didn't check for the second condition (if x is not smaller than y, then it is greater or equal)?

    Code:
    if (x < y) {
        return y - x;
    }
    else {
        return x - y;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Wouldn't this be handled automatically if you didn't check for the second condition (if x is not smaller than y, then it is greater or equal)?
    No actually, it comes out 5 instead of 0.

  13. #13
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by ShinobiRAGE View Post
    No actually, it comes out 5 instead of 0.
    There must be some error in your code then, because it should definitely return 0;

    For example -

    Code:
    if (x < y) {
        return y - x;
    }
    else {
        return x - y;
    }
    Lets plug in 5 and see,

    Code:
    if (5 < 5) {//False so skip
        return y - x;
    }
    else {//True
        return 5 - 5;//should be 0
    }
    Spidey out!

  14. #14
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    also
    Code:
    int sub ( int x, int y )
    {
    	if ( x < y ) {
    		return y - x;
    	}
    	return x - y;
    }
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Wouldn't this be handled automatically if you didn't check for the second condition?

    Yes, and that's how I would do it (or even something closer to ಠ_ಠ's solution). But I think the solution ShinobiRAGE came up with is fine and it is at least original.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM