Changing from positive to negative numbers

This is a discussion on Changing from positive to negative numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having some trouble trying to convert integer values from positive to negative and vice versa. Here's an example ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Changing from positive to negative numbers

    Hi,

    I'm having some trouble trying to convert integer values from positive to negative and vice versa.

    Here's an example of what I mean:
    = 10 convert to -10
    = -10 conver to 10


    Here's my code:

    Code:
    #include "Number.h"
    
    #include <iostream>
    using std::cout;
    
    int main()
    {
    	Number num;
    
    	num.setNum(7);
    
    	cout << "Number: is " << num.getNum() << "\n";
    
    	return 0;
    }
    Code:
    #ifndef NUMBER_H
    #define NUMBER_H
    
    class Number
    {
    public:
    	Number();
    	void setNum(const int &n);
    	int getNum();
    	int negation(int value);
    private:
    	int num;
    };
    
    #endif
    Code:
    #include "Number.h"
    
    
    Number::Number()
    {
    	this->num = num;
    }
    
    void Number::setNum(const int &n)
    {
    	this->num = negation(n);
    }
    
    int Number::getNum()
    {
    	return num;
    }
    
    int Number::negation(int value)
    {
    	if(value < 0)
    	{
    		value  > value;
    	}
    
    	if(value > 0)
    	{
    		value < value;
    	}
    
    	return value;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int Number::negation(int value)
    {
    	if(value < 0)
    	{
    		value  > value;
    	}
    
    	if(value > 0)
    	{
    		value < value;
    	}
    
    	return value;
    }
    The red pieces above do not do anything useful [if the compiler doesn't completely optimize it away, it will generate a value of 0 since value < value and value > value are always false]. You should get some sort of warning from the compiler about "statement with no effect" if you enable warnings.

    In math, how you do convert a value from positive to negative?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    O ya, the compiler does give a warning.

    I get it now how to convert from positive to negative:

    -to get -10 from 10, I would need to do this:
    0 - 10 = 10


    Thanks for the help

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vopo View Post
    -to get -10 from 10, I would need to do this:
    0 - 10 = 10
    Well, no, 0 - 10 = -10
    Though, if you subtracted the number from 0, you would indeed get the right result. Normally though, you would multiply by -1 to switch between +ve and -ve.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you have 10, and you want to make it negative [using a pen and paper], what do you do [and don't say "write 0-10=-10", but think of another, simpler solution].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=Elysia;778325]You can also use the negation operator:
    [deleted]/QUOTE]

    You spoilsport. I was trying to get the original poster to get there him/herself.
    Edit: Now corrected.

    --
    Mats
    Last edited by matsp; 08-07-2008 at 05:00 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Quote Originally Posted by matsp View Post
    You spoilsport. I was trying to get the original poster to get there him/herself.

    --
    Mats
    I never posted anything
    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.

  8. #8
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    try this simple and short code instead

    Code:
    #include<iostream.h>
    #include<conio.h>
    main()
    {
          system("cls");
          int a,b;
          cout<<"Enter a number "<<endl;
          cin>>a;
          b=a-(2*a);
          cout<<"The interchanged value is "<<b;
          getch();
          return 0;
          }

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Implicit main is not allowed in C++.
    Iostream.h is not a standard header.
    cout, cin, endl are located in the std namespace.
    conio.h is a non-standard header and getch() is a non-standard function. Better use cin.get() instead, and get rid of both those two.

    Are by chance using Turbo C++ or some other old compiler? This won't compile.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    In Simple algebra

    x * -1 = -x
    -x * -1 = x

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manzoor View Post
    In Simple algebra

    x * -1 = -x
    -x * -1 = x
    There is a simpler form tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    There is a simpler form tho'.

    --
    Mats
    Ooh, bit-wise negation

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    Ooh, bit-wise negation

    QuantumPete
    No, simpler than that [and in two's complement, it's bitwise negation + 1 - I don't know of ANY current machine using ones complement]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    x*=-1;

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    x*=-1;
    There's a simpler answer. Just a basic assignment.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

Similar Threads

  1. Ignoring negative numbers and moving on
    By quiet_forever in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 11:38 PM
  2. Question
    By Detrigan in forum C Programming
    Replies: 2
    Last Post: 10-19-2003, 10:13 PM
  3. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  4. Replies: 1
    Last Post: 10-01-2001, 10:39 AM

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