Results 1 to 6 of 6

Thread: Negative Numbers

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Talking 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
    }
    }

  2. #2
    jim mcnamara
    Guest
    Code:
    #include <iostream.h> 
    
    void main() 
    {
     int a; 
     cout << "Enter a: "; 
      a=0;
      cin >> a; 
      if(a) {
         a = a* (-1);
         cout << a;
      }
      return;
    }

  3. #3
    jim mcnamara
    Guest
    Code:
    #include <iostream.h> 
    
    void main() 
    {
     int a; 
     cout << "Enter a: "; 
      a=0;
      cin >> a; 
      if(a) {
         a = a* (-1);
         cout << a;
      }
      return;
    }

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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 << -<< endl;

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    jim mcnamara
    Guest
    Yes. if(a) {} should really puke when value is zero.
    The 'specs' are a bit scanty to say the least

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width