-
Negative Numbers
i have a as an integer but the output would be -a. does anyone not understand? here..if a user enters 5 the output would be -5. if a user enters -5 the output would be 5. How do i do this using this code:
[code]
#include <iostream.h>
void main()
{
cout << "Enter a: ";
int a;
cin >> a;
if(a == //i have no idea what to put here)
{
//something
}
}
-
Code:
#include <iostream.h>
void main()
{
int a;
cout << "Enter a: ";
a=0;
cin >> a;
if(a) {
a = a* (-1);
cout << a;
}
return;
}
-
Code:
#include <iostream.h>
void main()
{
int a;
cout << "Enter a: ";
a=0;
cin >> a;
if(a) {
a = a* (-1);
cout << a;
}
return;
}
-
hmm why are you multiplying it with -1, is there a special reason for it?
because this also seems to work, or am I overlooking something?
PHP Code:
#include <iostream>
using namespace std;
int main(){
int i;
cout << "Enter i" << endl;
cin >> i;
cout << -i << endl;
}
-
Yes. if(a) {} should really puke when value is zero.
The 'specs' are a bit scanty to say the least :D
-
unary - operator does exactly that. And for the if(a):
if(cin.good()) {}
checks if cin was able to read a number.