# Thread: Changing from positive to negative numbers

1. ## 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. 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

3. 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. Originally Posted by vopo
-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

5. 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

6. [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

7. Originally Posted by matsp
You spoilsport. I was trying to get the original poster to get there him/herself.

--
Mats
I never posted anything

8. 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. 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. In Simple algebra

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

11. Originally Posted by manzoor
In Simple algebra

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

--
Mats

12. Originally Posted by matsp
There is a simpler form tho'.

--
Mats
Ooh, bit-wise negation

QuantumPete

13. Originally Posted by QuantumPete
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

14. x*=-1;

15. Originally Posted by m37h0d
x*=-1;
There's a simpler answer. Just a basic assignment.

--
Mats