Thread: Negative numbers not working

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Negative numbers not working

    I have a program where the user inputs a line of numbers, and the two highest ones are displayed. It works fine, until negative values are entered at which point it shows 0 as the result. Can someone help?
    Code:
    #include <iostream>
    using namespace std;
    int main( ) {
    int num = 0;
    int highest = 0;
    int secondhighest = 0;
    int input = 0;
    cout << "How many numbers? "; 
    	 cin >> num; 
    	 cin >> input; 
    	 int input2 = input;
    //	 highest = input;
    	// secondhighest = input;
    for(int track = 0; track < num; track++) 
    {       
            if(track == 1) {
                     input = input2;
                     }
    else {                 
    cin >> input;
    }
    
    
    
    
    if (input > highest) {		  
    secondhighest = highest;
    highest = input;	
    }
    else if (input > secondhighest) {
    secondhighest = input;
    }
    }		
    cout << endl;
    cout << "Highest: " << highest << endl; 
    	
    cout << "Second: " << secondhighest; 	
    }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help?

  3. #3
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    That's because your variables to hold the users highest and second highest input are initialized to 0. So if the user enters a negative number it won't be greater than 0, so the contents of the variable won't change.

    In your other post I said that i initialized my variables to contain large negative numbers to prevent this from happening. I'm not sure what the largest number an int can hold is, but I'm sure a quick google will give you the right result.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Code:
    #include <limits>
    
    // and in main
    int highest = std::numeric_limits<int>::min();
    int secondhighest = std::numeric_limits<int>::min();
    This would be the standard C++ way of doing it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yeah, but I wouldn't use such a thing, since it bakes in type specific dependencies. Picture what would need to be done if, for some reason, the variables need to be changed so they are of some other type.


    Easier to initialise highest and secondhighest with the first two values input (and also write the code to cope if the user enters less than two values).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by grumpy View Post
    Picture what would need to be done if, for some reason, the variables need to be changed so they are of some other type.
    Typedef time!
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Typedefs wouldn't work in all cases.

    Consider what would happen if it was decided to use a "huge int" type (say a class named huge_int_type). Even if the implementer of such a type was to specialise std::numeric_limits<huge_int_type> appropriately (which is feasible) it would not necessarily be appropriate for std::numeric_limits<huge_int_type>::min() be provided.

    numeric_limits for unsigned types are also not required to provide a min().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Negative Numbers
    By r_james14 in forum C Programming
    Replies: 12
    Last Post: 12-20-2011, 03:01 AM
  2. Negative numbers in C
    By BlaX in forum C Programming
    Replies: 18
    Last Post: 06-29-2009, 06:30 PM
  3. strange negative numbers ?!!
    By Meshal in forum C Programming
    Replies: 6
    Last Post: 10-21-2007, 04:41 PM
  4. Negative Numbers
    By cgod in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2005, 08:57 AM
  5. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM