C++ 使用C++;接收整数输入并显示较大和较小的数字

C++ 使用C++;接收整数输入并显示较大和较小的数字,c++,C++,说明是 编写一个由while循环组成的程序,该循环(每次循环)读取两个整数,然后打印它们。输入终止“I”时退出程序 更改程序,写出较小的值为:后接较小的NWNBER,较大的值为:后接较大的值 我让程序运行,但它终止时出现了一个范围错误:有人能纠正我在这段代码中的错误吗 /*a drill in the Programming Principles and Practice Using C++ by Bjarne Stroustrup*/ #include "std_lib_facilities.

说明是

  • 编写一个由while循环组成的程序,该循环(每次循环)读取两个整数,然后打印它们。输入终止“I”时退出程序
  • 更改程序,写出较小的值为:后接较小的NWNBER,较大的值为:后接较大的值
  • 我让程序运行,但它终止时出现了一个范围错误:有人能纠正我在这段代码中的错误吗

    /*a drill in the Programming Principles
    and Practice Using C++ by Bjarne Stroustrup*/
    #include "std_lib_facilities.h"     /*standard library from the author's website*/
    int main()
    {
        vector<int>values;
        int a, b;       //ints declared
        while(cin>>a>>b){   //ints read
            values.push_back(a);    //ints put into vector
            values.push_back(b);    //********************
        }
        for(int i = 0; i < values.size(); ++i){     //loop
            if(values[i]>values[i+1]){
                cout << "The larger value is: " << values[i] << endl;   /*print larger value on screen*/
            }
            else
                cout << "The smaller value is: " << values[i] << endl;  /*prints smaller value on screen*/
        }
        return 0;
    }
    
    /*编程原理中的演练
    用C++语言实现Bjarne Stroustrup
    #包括作者网站上的“std_lib_facilities.h”/*标准库*/
    int main()
    {
    向量值;
    int a,b;//声明的int
    而(cin>>a>>b){//ints读取
    value.push_back(a);//输入向量的整数
    值。推回(b)//********************
    }
    对于(int i=0;i值[i+1]){
    
    cout
    值[i+1]
    超出最后一个值的界限,因此您需要更改
    的循环条件

    for(int i = 0; i < values.size() - 1; ++i){ 
                                  // ^^^
    
    for(inti=0;i
    1.编写一个由while循环组成的程序,每次循环 循环)读入两个整数,然后打印。退出程序 当输入终止“I”时

    int a, b = 0; 
    //  anything that isn't a int terminate the input e.g. 2 3 |
    while (cin >> a >> b)  
      cout << a << " " << b << "\n";
    
    inta,b=0;
    //任何非整数的东西都会终止输入,例如2 3|
    而(cin>>a>>b)
    法院(b){
    if(a不欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它与预期的偏差。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:。顺便说一句,您的算法会打印每个NUB呃,不仅仅是最小的/最大的。
    std::minmax_元素
    可以解决整个问题。
    int a, b;
    const string smaller = "smaller value is: ";
    const string larger = "larger value is: ";
    
    while (cin >> a >> b) {
        if (a < b)
            cout << smaller << a << larger << b << endl;
        else
            cout << smaller << b << larger << a << endl;
    } 
    
    if (cin.fail()) {
        cin.clear();
        char ch;
        if (!(cin >> ch && ch == '|'))
            throw runtime_error(string {"Bad termination"});
    }