Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用cin::fail()从标准数据中读取无限循环_C++_Function_Vector_Push Back - Fatal编程技术网

C++ 使用cin::fail()从标准数据中读取无限循环

C++ 使用cin::fail()从标准数据中读取无限循环,c++,function,vector,push-back,C++,Function,Vector,Push Back,我正在努力使用向量push_-back函数。 我们的目标是拥有一个函数,它可以推动n个元素,直到您决定停止为止。 所以我的“停止”概念是cin.fail() 有故障的功能是 void pushbackVector(vector<double> &data) { double input; cin >> input; for (int i = 0; i < 100; i++) { if (cin.fail())

我正在努力使用向量push_-back函数。 我们的目标是拥有一个函数,它可以推动n个元素,直到您决定停止为止。 所以我的“停止”概念是cin.fail()

有故障的功能是

void pushbackVector(vector<double> &data)
{
    double input;
    cin >> input;

    for (int i = 0; i < 100; i++)
    {
        if (cin.fail())
        {
            cout << "Ending input.\n";
            return;
        }
        else
        {
            data.push_back(input);
        }
    }
void pushbackVector(向量和数据)
{
双输入;
cin>>输入;
对于(int i=0;i<100;i++)
{
if(cin.fail())
{

cout您只读取了一次,请将读取内容移动到循环中:

void pushbackVector(vector<double> &data)
{
    double input;
    // cin >> input;   --------------
                                    //
    for (int i = 0; i < 100; i++)   //
    {                               //
        cin >> input;   // <---------

        if (cin.fail())
        {
            cout << "Ending input.\n";
            return;
        }
        else
        {
            data.push_back(input);
        }
    }

流可以在布尔表达式中使用-它们转换为
!fail()
-的结果,这是控制循环的一种方便而惯用的方法。

如果只读取一次,请将读取内容移动到循环中:

void pushbackVector(vector<double> &data)
{
    double input;
    // cin >> input;   --------------
                                    //
    for (int i = 0; i < 100; i++)   //
    {                               //
        cin >> input;   // <---------

        if (cin.fail())
        {
            cout << "Ending input.\n";
            return;
        }
        else
        {
            data.push_back(input);
        }
    }

流可以在布尔表达式中使用-它们转换为
!fail()
-的结果,这是控制循环的一种方便而惯用的方法。

无限循环是由以下事实引起的:

cin >> input;
一次,然后输入一个
while
循环(在
main
中),该循环将一直持续(除非输入最初等于
6

更改:

cin >> input;

bool exit = 0;

while (!exit)
{
    // ...
致:


根据您的逻辑,在以下位置的
pushbackVector
函数中也会发生同样的情况:

double input;
cin >> input;

for (int i = 0; i < 100; i++)
{
    // ...
双输入;
cin>>输入;
对于(int i=0;i<100;i++)
{
// ...
您可能希望将其更改为:

double input;

for (int i = 0; i < 100; i++)
{
    cin >> input;
    // ...
双输入;
对于(int i=0;i<100;i++)
{
cin>>输入;
// ...

无限循环是由以下事实引起的:

cin >> input;
一次,然后输入一个
while
循环(在
main
中),该循环将一直持续(除非输入最初等于
6

更改:

cin >> input;

bool exit = 0;

while (!exit)
{
    // ...
致:


根据您的逻辑,在以下位置的
pushbackVector
函数中也会发生同样的情况:

double input;
cin >> input;

for (int i = 0; i < 100; i++)
{
    // ...
双输入;
cin>>输入;
对于(int i=0;i<100;i++)
{
// ...
您可能希望将其更改为:

double input;

for (int i = 0; i < 100; i++)
{
    cin >> input;
    // ...
双输入;
对于(int i=0;i<100;i++)
{
cin>>输入;
// ...

改为这样做:

void pushbackVector(vector<double> &data)
{
    double input;

    while (cin >> input) //will return true when there's valid input, otherwise false
    {
        if (input == -1)
        {
            cout << "Ending input.\n";
            return;
        }
        else
        {
            data.push_back(input);
        }
    }
void pushbackVector(向量和数据)
{
双输入;
while(cin>>input)//在有有效输入时返回true,否则返回false
{
如果(输入==-1)
{

不能这样做:

void pushbackVector(vector<double> &data)
{
    double input;

    while (cin >> input) //will return true when there's valid input, otherwise false
    {
        if (input == -1)
        {
            cout << "Ending input.\n";
            return;
        }
        else
        {
            data.push_back(input);
        }
    }
void pushbackVector(向量和数据)
{
双输入;
while(cin>>input)//在有有效输入时返回true,否则返回false
{
如果(输入==-1)
{

cout将错误的读数放在一边,如果您输入了不应该输入的内容,则需要清除输入

如果需要,首先添加te块

cin.clear(); 
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cin.clear();
cin.ignore(数值限制::max(),'\n');

将错误的读数放在一边,如果您输入了不应该输入的内容,则需要清除输入

如果需要,首先添加te块

cin.clear(); 
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cin.clear();
cin.ignore(数值限制::max(),'\n');

我将按以下方式编写函数

void pushbackVector( std::vector<double> &data )
{
    cin.clear(); 
    cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );

    data.insert( data.end(), std::istream_iterator<double>( std::cin ), std::istream_iterator<double>() ); 
}
void pushbackVector(标准::矢量和数据)
{
cin.clear();
cin.ignore(std::numeric_limits::max(),'\n');
insert(data.end(),std::istream_迭代器(std::cin),std::istream_迭代器());
}

我将按以下方式编写函数

void pushbackVector( std::vector<double> &data )
{
    cin.clear(); 
    cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );

    data.insert( data.end(), std::istream_iterator<double>( std::cin ), std::istream_iterator<double>() ); 
}
void pushbackVector(标准::矢量和数据)
{
cin.clear();
cin.ignore(std::numeric_limits::max(),'\n');
insert(data.end(),std::istream_迭代器(std::cin),std::istream_迭代器());
}

前几天我做完了,但我忘了发布我的答案。 谁知道我可以说如果cin失败,停止输入,但不要结束程序,哈哈

    #include<iostream>
    #include<vector>
    #include<algorithm> // for sort algorithms
    #include<iomanip>  // for setprecision

    using namespace std;

    // function prototypes
    void menu();
    void printVector(const vector<double> &data);
    void pushbackVector(vector<double> &data);
    void sortVector (vector<double> &data);

    int main()
    {
        vector<double> row1;
        vector<double> row2;
        vector<double> row3;

        int input;
        bool exit = false;

        while(!exit)
        {
            menu();
            cin >> input;

            switch (input)
            {
                case 1:
                    cout << "Entering vector 1\n";
                    pushbackVector(row1);
                    sortVector(row1);

                    cout << "\n";
                    break;

                case 2:
                    cout << "Entering vector 2\n";
                    pushbackVector(row2);
                    sortVector(row2);
                    reverse(row2.begin(), row2.end());

                    cout << "\n";
                    break;

                case 3:
                    cout << "Printing out vector 1\n";
                    printVector(row1);

                    cout << "\n";
                    break;

                case 4:
                    cout << "Printing out vector 2\n";
                    printVector(row2);

                    cout << "\n";
                    break;

                case 5:
                    // reserve enough space for all of row1's and row2's elements
                    row3.reserve(row1.size() + row2.size());
                    // insert row1's elements at the end of row3
                    row3.insert(row3.end(), row1.begin(), row1.end());
                    // insert row2's elements at the end of row3
                    row3.insert(row3.end(), row2.begin(), row2.end());


                    cout << "Printing out the contents of vector 1\n";
                    printVector(row1);
                    cout << "Printing out the contents of vector 2\n";
                    printVector(row2);
                    cout << "Printing out the contents of vector 3\n";
                    printVector(row3);


                    cout << "\n";
                    break;

                case 6:
                    cout << "Exitting\n";
                    exit = true;
                    break;

                default:
                    cout << "Invalid choice\n";
            }
        }


        return 0;
    }

    void menu()
    {
        cout << "Choose an option\n";
        cout << "1) Enter first vector\n";
        cout << "2) Enter second vector\n";
        cout << "3) Print out the first vector\n";
        cout << "4) Print out the second vector\n";
        cout << "5) Print out all three vectoros\n";
        cout << "6) Exitting the program\n";
    }


    void printVector(const vector<double> &data)
    {
        for(int i = 0; i < data.size(); i++)
        {
            cout << setprecision(4) << data[i] << " ";
        }
        cout << "\n";
    }

    void pushbackVector(vector<double> &data)
    {
        double input;
        int numOfItems;
        cout << "How many items you want to add into vector?: ";
        cin >> numOfItems;

        for (int i = 0; i < numOfItems; i++)
        {
            cin >> input;
            if (cin.fail())
            {
                cout << "Ending input.\n";
                return;
            }
            else
            {
                data.push_back(input);
            }
        }
    }

    void sortVector(vector<double> &data)
    {
        cout << "Sorting your vector \n";
        sort(data.begin(), data.end());
    }
#包括
#包括
#包含//用于排序算法
#包含//用于设置精度
使用名称空间std;
//功能原型
无效菜单();
无效打印向量(常量向量和数据);
无效回推向量(向量和数据);
void sortVector(矢量和数据);
int main()
{
向量行1;
矢量row2;
矢量row3;
int输入;
bool exit=false;
当(!退出)
{
菜单();
cin>>输入;
开关(输入)
{
案例1:

我几天前做完了,但我忘了把答案贴出来。 谁知道我可以说如果cin失败,停止输入,但不要结束程序,哈哈

    #include<iostream>
    #include<vector>
    #include<algorithm> // for sort algorithms
    #include<iomanip>  // for setprecision

    using namespace std;

    // function prototypes
    void menu();
    void printVector(const vector<double> &data);
    void pushbackVector(vector<double> &data);
    void sortVector (vector<double> &data);

    int main()
    {
        vector<double> row1;
        vector<double> row2;
        vector<double> row3;

        int input;
        bool exit = false;

        while(!exit)
        {
            menu();
            cin >> input;

            switch (input)
            {
                case 1:
                    cout << "Entering vector 1\n";
                    pushbackVector(row1);
                    sortVector(row1);

                    cout << "\n";
                    break;

                case 2:
                    cout << "Entering vector 2\n";
                    pushbackVector(row2);
                    sortVector(row2);
                    reverse(row2.begin(), row2.end());

                    cout << "\n";
                    break;

                case 3:
                    cout << "Printing out vector 1\n";
                    printVector(row1);

                    cout << "\n";
                    break;

                case 4:
                    cout << "Printing out vector 2\n";
                    printVector(row2);

                    cout << "\n";
                    break;

                case 5:
                    // reserve enough space for all of row1's and row2's elements
                    row3.reserve(row1.size() + row2.size());
                    // insert row1's elements at the end of row3
                    row3.insert(row3.end(), row1.begin(), row1.end());
                    // insert row2's elements at the end of row3
                    row3.insert(row3.end(), row2.begin(), row2.end());


                    cout << "Printing out the contents of vector 1\n";
                    printVector(row1);
                    cout << "Printing out the contents of vector 2\n";
                    printVector(row2);
                    cout << "Printing out the contents of vector 3\n";
                    printVector(row3);


                    cout << "\n";
                    break;

                case 6:
                    cout << "Exitting\n";
                    exit = true;
                    break;

                default:
                    cout << "Invalid choice\n";
            }
        }


        return 0;
    }

    void menu()
    {
        cout << "Choose an option\n";
        cout << "1) Enter first vector\n";
        cout << "2) Enter second vector\n";
        cout << "3) Print out the first vector\n";
        cout << "4) Print out the second vector\n";
        cout << "5) Print out all three vectoros\n";
        cout << "6) Exitting the program\n";
    }


    void printVector(const vector<double> &data)
    {
        for(int i = 0; i < data.size(); i++)
        {
            cout << setprecision(4) << data[i] << " ";
        }
        cout << "\n";
    }

    void pushbackVector(vector<double> &data)
    {
        double input;
        int numOfItems;
        cout << "How many items you want to add into vector?: ";
        cin >> numOfItems;

        for (int i = 0; i < numOfItems; i++)
        {
            cin >> input;
            if (cin.fail())
            {
                cout << "Ending input.\n";
                return;
            }
            else
            {
                data.push_back(input);
            }
        }
    }

    void sortVector(vector<double> &data)
    {
        cout << "Sorting your vector \n";
        sort(data.begin(), data.end());
    }
#包括
#包括
#包含//用于排序算法
#包含//用于设置精度
使用名称空间std;
//功能原型
无效菜单();
无效打印向量(常量向量和数据);
无效回推向量(向量和数据);
void sortVector(矢量和数据);
int main()
{
向量行1;
矢量row2;
矢量row3;
int输入;
bool exit=false;
当(!退出)
{
菜单();
cin>>输入;
开关(输入)
{
案例1:

我真的怀疑你用
for
循环进入无限循环。修复了误导性标题。这真的与
推回
无关。我真的怀疑你用
for
循环进入无限循环。修复了误导性标题。这真的与
推回
无关。仍在输入infinite loop>@Antononov您是否正在输入100个值?我看不出有任何机会使用pushbackVector进行无限循环。我的意思是,它最多可以接受100个值,并且不能接受更多值。如果有无限循环,则它在代码中的其他地方。无限循环肯定在主循环中。您还需要在循环中执行cin。@drescherjm是的,我执行了sa是的。仍然在输入无限循环>@AntonAntonov你在输入100个值吗?我看不出有任何机会使用pushbackVector进行无限循环。我的意思是,它最多可以接受100个值,不能接受更多。如果有无限循环,它的某些部分