Can't find a logic error

This program is supposed to accept integer inputs until -99 is input, at which point it's supposed to output the greatest and least values input. But when I run this, it only works if I enter two values or less. Entering "1", "2", "10", and "-99" in that order results in the 10 being assigned to greatest, and 2 to least. I'm sure it's just a simple logic error somewhere, but I cannot find it. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 #include <iostream>
using namespace std;

int main()
{
	int greatest = -99, least = -99, temp = 0;

	while (temp != -99)
	{
		cout << "Please enter an integer (-99 to end)" << endl;
		cin >> temp;

		if (greatest == -99)
			greatest = temp;		//Initial plug-in of values to variables greatest and least. 
		else if (least == -99)		//This should only happen for the first two input values.
			{
				least = temp;
				if (greatest << least)		//Initial test to make sure greatest > least.
				{							//If greatest < least, it switches the values.  
					temp = greatest;
					greatest = least;
					least = temp;
				}
			}
		else if (temp >> greatest && temp != -99)		//Replaces greatest with temp if greatest < temp.
			greatest = temp;
		else if (temp << least && temp != -99)			//Replaces least with temp if least > temp.
			least = temp;
	}

	cout << "The largest number you entered was " << greatest 
	<< ", and the lowest number was " << least << endl;

	return 0;
}
		
Last edited on
It should be
18
19
20
21
22
23
24
25
26
27
28
        if (greatest < least) // < , not <<
        {                   
            temp = greatest;
            greatest = least;
            least = temp;
        }
    }
else if (temp > greatest && temp != -99) // > , not >>
    greatest = temp;
else if (temp < least && temp != -99) // < , not <<
    least = temp;
if (greatest << least)

You have double << in a couple of places where I think you just want to compare quantities with a single < or >.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>
using namespace std;

int main()
{
       // http://www.cplusplus.com/reference/limits/numeric_limits/
	int greatest = std::numeric_limits<int>::min() ;
	int least = std::numeric_limits<int>::max() ;

	int temp = 0;

	while( cout << "Please enter an integer (-99 to end)\n" &&
               cin >> temp && temp != -99 ) // for each number till -99 is entered
	{
               if( temp /*>>*/ > greatest ) greatest = temp;
               if( temp /*<<*/ < least ) least = temp;
	}

	cout << "The largest number you entered was " << greatest
	      << ",\nand the lowest number was " << least << '\n';
}
@JLBorges

can i ask..?

im a beginner here..

im confuse..

how can you compare the two integer if you just ask the user to type a number once..

can you please explain to me please.. thank you guys..
Numbers are read in a loop.

Let us take it step by small step:

This small program will, in a loop, read in integers one by one and print them out.

Till std::cin >> number evaluates to false
(That will happen when the input fails; we enter, say abcd when an int is expected.)

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    int number = 0 ;

    while( std::cin >> number ) // loop as long as we have read a number
    {
        std::cout << "you entered " << number << '\n' ;
    }
}




Now, let us extend it a little.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    int number = 0 ;

    while( std::cin >> number && number != -99 )
    {
        std::cout << "you entered " << number << '\n' ;
    }
}

This will work like the previous program.
In addition, the loop will be exited if we enter -99 ie. number != -99 evaluates to false.



Finally
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int number = 0 ;

    while( std::cout << "enter a number (-99 to quit): " &&
           std::cin >> number && number != -99 )
    {
        std::cout << "you entered " << number << '\n' ;
    }
}

Works like the program above.
In addition, each time through the loop, it will first prompt the user to enter a number.
Topic archived. No new replies allowed.