Thread: -3 to 3 need code..

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    -3 to 3 need code..

    hello there what is the code used when a user input one number and it will displays its absolute value for example if the user input -3 the output is 3 and if the user input a positive value or 0 then it will concide with the input???

    Code:
    <iostream>
    
    using namespace std;
    
    int main()
    {
    
    int nm;
    
    cout << "please input any number\n";
    cin>> nm;
    
    //what code is needed here?
    
    cout << "number is " << nm << "\n";
    
    }
    thanks....

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Isn't there a function in the standard library to do this? abs() or something, I think.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    I like pink bunnies. I won't give you the code but I'll tell you you need the abs() function. Asking us to feed you with code is very coarse.

    /* mod edit for offensive language ~kenf; based on an original script by nv*/

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well, in all fairness he did provide most of the necessary code, no need to be a jerk about it just because of one simple function.

    http://www.cppreference.com/stdmath/abs.html

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Wow. I suggest you take a cold shower Desolation.

    It's the abs() function, yes. It takes an int as an argument. You need to include the cstdlib header to use it. For historical reasons this function is not declared in the math header.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    okey got it thanks for that...

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    As a newcomer, I'm a little confused.

    I know that I must include <iostream> if I want to use cin or cout in C++. I looked at the reference cited by indigo0086 and it relates to abs() in the Standard C Math Library. Further, Mario F. said that, if abs() is required, the cstdlib must be included. Does this mean that both Math and cstdlib should be included or is it an option that only one is included?

    Going on from this, is there a definitive (and easily understood!) list of which header is required for which function in C++? Logic would have pointed me towards a mathematical header if I'd needed abs() as it is a mathematical function. I wouldn't have had a clue that cstdlib even existed.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Like mario said there are other reasons for why it isn't declared in the math header. How you can find out exactly, maybe by looking through a C library reference guide or something. But trying to find a particular function belonging to a particular library isn't so trivial without a reference guide.

    here's one for example

    http://www.cplusplus.com/ref/

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    abs() is declared in the cstdlib header. Not the math header

    It could probably (it should) have been declared in the math header. For historical reasons it was decided against it. There's only a handful more functions that have this little quirk. However, if you take a look at the long integer and double versions of this function (labs() and fabs(), respectively) you will find that they are declared in the math header.

    So take again a good look again at the link provided by Indigo. You will see abs() requesting the cstdlib.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    also stdlib.h becomes cstdlib.h in c++ I guess.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Going on from this, is there a definitive (and easily understood!) list of which header is required for which function in C++? Logic would have pointed me towards a mathematical header if I'd needed abs() as it is a mathematical function. I wouldn't have had a clue that cstdlib even existed.
    This is a strong and valid question. And I know exactly how you feel. My first experience with C++ went down in my history books as a short period of my life where I trully felt what was to live in the middle of chaos. But, as they say there's order in chaos. And definitely there's order on the standard headers. They are just too many, and for newcommers like you and me, the C++ Standard Library the Standard Template Library and all those names for headers files makes our exposure to C++ anything but a peaceful experience.

    I strongly suggest you buy a c++ reference book. It has been helping so much, it's not fun. And I even went on and bought a lousy one. Imagine what happens if you have a good one. But until then use the c++ reference websites and if something looks fishy to you, take a look in the cstdlib. It may be there.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    also stdlib.h becomes cstdlib.h in c++ I guess.
    Yes. <cstdlib> to be more precise.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I had a hard time with the C++ and C library because coming off of Java where I had easier access to the methods because the entire java library was available for reference on the website. But the java libraries are so damn huge and there isn't much of a standard it's kind of like, if you want to find a function that you want, it's much harder.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Or just put

    If (nm < 0)
    nm = -nm;


  15. #15
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    nm < 0 ? cout << -nm : cout << nm

    I like that little guy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM