简体   繁体   English

C ++继续经过while循环

[英]C++ continuing past the while loop

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. 最后的输出只是我尝试测试是否在尝试使用break / continue之后将“ Test”中继回我。 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. 我希望程序通过将所有字符串输出放在while循环之后来绕过显示所有工作,这样无论何时输入更高或更低的数字,它都不会实时更新。

For clarity issues, say you entered 2.6, 3.5, 6, 1.2, 9.2. 为了清楚起见,假设您输入了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. 它会不断更新,到目前为止,最高是2.6,然后是3.5,然后是6,依此类推。我希望跳过该过程,并只显示程序,最后,最高是9.2,最高是1.2。最低的。 Thank you for any assistance you can give me. 谢谢您能给我的任何帮助。

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. 因此,主要思想是在while循环之外使用排序和打印。 You should only use the while loop to push your inputs to the vector, also remember to use a break condition like this. 您只应使用while循环将输入推入向量,也请记住使用这样的中断条件。

As already mentioned by @immibis , output low and high after the while loop: 正如@immibis已经提到的那样 ,在while循环之后输出lowhigh

//cout << "Test";
cout << low << " is the new low" << '\n';
cout << high << " is the new high" << '\n';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM