# Changing from positive to negative numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-07-2008
vopo
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; }```
• 08-07-2008
matsp
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
• 08-07-2008
vopo
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
• 08-07-2008
QuantumPete
Quote:

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
• 08-07-2008
matsp
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
• 08-07-2008
matsp
[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
• 08-07-2008
Elysia
Quote:

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

--
Mats

I never posted anything ;)
• 08-08-2008
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;       }```
• 08-08-2008
Elysia
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.
• 08-08-2008
manzoor
In Simple algebra

x * -1 = -x
-x * -1 = x
• 08-08-2008
matsp
Quote:

Originally Posted by manzoor
In Simple algebra

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

There is a simpler form tho'.

--
Mats
• 08-08-2008
QuantumPete
Quote:

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

--
Mats

Ooh, bit-wise negation ;)

QuantumPete
• 08-08-2008
matsp
Quote:

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
• 08-08-2008
m37h0d
x*=-1;
• 08-08-2008
matsp
Quote:

Originally Posted by m37h0d
x*=-1;

There's a simpler answer. Just a basic assignment.

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last