|
-
May 29th, 2002, 08:19 AM
#1
Thread Starter
Frenzied Member
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
}
}
-
May 29th, 2002, 09:23 AM
#2
Code:
#include <iostream.h>
void main()
{
int a;
cout << "Enter a: ";
a=0;
cin >> a;
if(a) {
a = a* (-1);
cout << a;
}
return;
}
-
May 29th, 2002, 09:29 AM
#3
Code:
#include <iostream.h>
void main()
{
int a;
cout << "Enter a: ";
a=0;
cin >> a;
if(a) {
a = a* (-1);
cout << a;
}
return;
}
-
May 29th, 2002, 07:50 PM
#4
Frenzied Member
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;
}
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
May 29th, 2002, 08:59 PM
#5
Yes. if(a) {} should really puke when value is zero.
The 'specs' are a bit scanty to say the least
-
May 31st, 2002, 03:07 AM
#6
unary - operator does exactly that. And for the if(a):
if(cin.good()) {}
checks if cin was able to read a number.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|