0

I'm having a bit of trouble finding a way to bypass the infinite input loop I've put myself in. Here's the code:

int main()
{
    vector<double>v;
    double input;
    double low = numeric_limits<double>::max();
    double high = numeric_limits<double>::min();
    cout << "Enter doubles in a sequence. << '\n';
    while (cin >> input) {
        v.push_back(input);
        if (input < low) {
            low = input;
            cout << low << "is the new low" << '\n';

        }
        if (input > high) {
            high = input;
            cout << high << "is the new high" << '\n';
        }


        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); ++i) {
            cout << v[i] << " ";

        }
        cout << '\n';
    }
    cout << "Test";
}

The last output was just me attempting to test whether it would relay the "Test" back to me after attempting to use break/continue. The code I wrote was just used to make a note of the all the numbers a person would input and then update whether it was the lowest or the highest number and sort it into a vector. I wanted the program to bypass showing all the work by putting the all the string outputs after the while loop so that it wouldn't update in real time whenever a higher number or lower number would be entered.

For clarity issues, say you entered 2.6, 3.5, 6, 1.2, 9.2. It would constantly update that 2.6 is the highest so far, then 3.5, then 6, etc. I'd like for that process to be skipped and for the program to just show, at the end, 9.2 is the highest number and 1.2 is the lowest. Thank you for any assistance you can give me.

blueyfooey
  • 11
  • 4
  • 4
    So move `cout << low << "is the new ...` and `cout << high << "is the new ...` to after the loop? And move the `sort` call and the printing of the numbers to there as well? – user253751 Feb 26 '16 at 11:27
  • 2
    And you can use `break` with a condition to exit from your loop (when 0 is given for example) – Garf365 Feb 26 '16 at 11:30
  • 1
    You can, simply, enter EOF symbol: http://stackoverflow.com/questions/1118957/c-how-to-simulate-an-eof – Algirdas Preidžius Feb 26 '16 at 11:31
  • 2
    Summing up what the folks told above, you may be interested in max_element and min_element from . – wesley.mesquita Feb 26 '16 at 11:35
  • I attempted to move the statements outside of the loop, but because it's while (cin >> input), it never seems to go past that, as it's always just waiting for a new input. – blueyfooey Feb 26 '16 at 12:02

2 Answers2

0

Since you do not want to update the highest and lowest values every time an input is given, you can try the following code:

while (cin >> input) {
        if(input==-1)
            break;
        v.push_back(input);
        }

sort(v.begin(), v.end());

for (int i = 0; i < v.size(); ++i)
    cout << v[i] << " ";

cout<<endl;
cout<<v[0]<<" is the lowest number."<<endl;
cout<<v[v.size()-1]<<" is the highest number."<<endl;

So, the main idea is to use the sorting and printing outside the while loop. You should only use the while loop to push your inputs to the vector, also remember to use a break condition like this.

Mert Şeker
  • 90
  • 1
  • 8
  • I'm still fairly new to C++, so I'm afraid I'm going to ask if there's something significant about if(input == -1). Would that just be its literal meaning that if I entered -1, it would just stop the program? – blueyfooey Feb 26 '16 at 12:06
  • Not quite, when you enter -1, the command break is executed, break just stops the while or for loop that it's inside. So, in this case when you enter -1, it would break the while(cin>>input) loop and you cannot enter any more values. In your original code, you did not use a condition like this and therefore you would just enter values until forever which is not a good idea. Using -1 in such cases like this (to stop a loop or stop a program) is just a programming convenience, you can use any other number or character as you wish. – Mert Şeker Feb 26 '16 at 20:35
  • Thank you for this! I assume that if I were to send this to someone to let them play with it, I'd just add cout << "Enter -1 to finish up when you're done"? – blueyfooey Feb 26 '16 at 22:13
  • Yes, you can just add a comment like that and you'll be good to go. :) – Mert Şeker Feb 26 '16 at 22:24
0

As already mentioned by @immibis, output low and high after the while loop:

//cout << "Test";
cout << low << " is the new low" << '\n';
cout << high << " is the new high" << '\n';
Community
  • 1
  • 1
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • I attempted to move the statements outside of the loop, but because it's while (cin >> input), it never seems to go past that, as it's always just waiting for a new input. – blueyfooey Feb 26 '16 at 12:03