Im trying to get the smallest number out of a series of user generated numbers.
i can correctly get the largest number but i always get the negative 99 for the smallest.
im working out of a c++ book that does not provide solutions, i tried to catch the -99 but it didn't work
as you can see i commented it out.


i know someone is going to suggest it (dont use a neg number or try an array)
but the challenge says to do it WITHOUT using arrays and i HAVE to use -99 to quit.


any suggestions

int main()
{
    int counter, input, largest = 0, smallest = 0;

 cout << "Please enter a series of numbers so i can find the largest and smallest." << endl;
 cout << "Enter -99 to indicate you are done entering numbers." << endl;



 do
    {

        cout << "number: ";
        cin >> input;
        
       


//        if (input == -99)
//        {
//            input = 0;
//        }

        

        if (input < smallest)

        {
            smallest = input;
        }



        if (input > largest )
        {
            largest = input;
        }



   } while (input != -99);


    //always gives -99
    cout << "After searching the smallest is: " << smallest << endl;
    // correct largest
    cout << "After searching largest is: " << largest << endl;


    return 0;
}

Recommended Answers

All 5 Replies

That would mean you should be searching for numbers that have nothing to do with -99, and when you want to quit the application -99 then becomes important. Your code looks like you are a C++ beginner, and I think you don't rest too much in a simple question, I would bet you are best to find your own way, and I would provide these most helpful references, please do become a better programmer. For our and your own sake.

http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/program_structure/
http://www.cplusplus.com/doc/tutorial/variables/

Firstly, whatever compiler you're using seems to include <iostream> without you asking it to, and also ignores namespaces. You should get a better compiler. There are many free options.

As for why the smallest comes out to -99 every time; it's because the smallest number you enter is -99 (here's an experiment - try entering one of the number as something even lower, like -100). The check for when to exit the loop happens after running through the loop.

Do not also forget to test values where largest is less than -99.

First of all, is this the cropped version of the code, or you didn't include <iostream> header and the std namespace? You'll need to include this in order for cout, cin and endl to work properly.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

And in your do-while loop, when you enter a value, let's say -1, the number -1 is assigned to variable "smallest". And in second round of the loop, you enter -99. Now -99 will be assigned to variable "smallest", before the do-while loop terminates. That's why the final value of variable "smallest" is always -99.

Of course, your way of "catching" the -99 is not going to work, cause whenever you enter -99, input's value will be changed to 0, thus the do-while loop's not going to stop.

One of the ways to solve this problem is, you can change the do-while loop to a infinite while loop, and when -99 is entered, use the "break" keyword to terminate the infinite while loop.

Example:

while(true){

     if(some_value == 1) break;
     else { 

              // blabla something something

     }

}

You are on the right track but you don't want to change what input is with your if statement. If you change line 21 to break; it should do what you want it to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.