【问题标题】:C++ Program fails if directly fed input in one go [closed]如果直接一次性输入输入,C++程序将失败[关闭]
【发布时间】:2015-04-10 23:30:05
【问题描述】:

对不起,如果标题不完美,不知道如何措辞。

我正在为课程做一个程序,它通过了我所有的测试,但没有通过一些测试用例。一些研究表明,输入的方式很重要。

如果我输入这样的内容: 0 0 第一的 0 第二 0 第三 0 第四 0 第五 1 第六 1 第七 1 第八 (进入) 1 第九 4 (输入)

或者在每行之间按回车,一切都很好。但是,如果我复制/粘贴整个输入集(或使用 cin>> 将其输入,就像我们为分级所做的那样),我会得到随机输出(4 个触发器输出)或 segfault 11 之间变化的怪异。

这里是主要方法(不能改变这个)

    #include"tDeque.h"
    using namespace std;

    template <typename T>

    void test (T s) {
    Deque<T> *DQ = new Deque<T>();
    T input;
    int op=0;

    while (op<7)
    {

        std::cin>> op;
        switch(op) {
            case 0:
                std::cin>>input;
                try {
                    DQ->push_front(input);
                } catch (exception e) {
                    cout<<"Out of Memory Exception!"<<endl;
                }
                break;
            case 1:
                std::cin>>input;
                try{
                    DQ->push_back(input);
                } catch (exception e) {
                    cout<<"Out of Memory Exception!"<<endl;
                }
                break;
            case 2:
                try{
                    std::cout<<DQ->pop_front()<<std::endl;
                } catch (exception e) {
                    cout<<"Caught Exception for empty stack!"<<endl;
                }
                break;
            case 3:
                try{
                    std::cout<<DQ->pop_back()<<std::endl;
                } catch (exception e) {
                    cout<<"Caught Exception for empty stack!"<<endl;
                }
                break;
            case 4:
                std::cout<<DQ->toStr();
                break;
            case 5:
                std::cout<<DQ->size()<<std::endl;
                break;
            case 6:
                std::cout<<DQ->empty()<<std::endl;
                break;
        }
    }
}


int main(int argc, char **argv) {
    int op=0;
    std::string input;
    int type;
    cin>>type;
    string s = "tDeque";
    switch(type) {
        case 0:
            test(s);
            break;
        case 1:
            test(3.2);
            break;
        case 2:
            test(1);
            break;
        default:
            return 1;
    }

    return 0;

}

这是我的一些代码(我一直在尝试在代码中添加一个中断。它有帮助,但不能解决我的问题)

void Deque<T>::needIGrow()
{
    try {


        if(front==arrSize-1 &&back==0)
        {   //we need a new array double in size
            T *arr2=new T[arrSize*2];
            //we watn to be 1/3 of the way up the new array to start, and the new array is double in size.
            int newBack=arrSize*2/3;
            int oldFront=front;
            for(int i=back;i<=oldFront;i++)
            { //Transfer old contents to new array
                arr2[i+newBack]=arr[i];
                front=newBack+i;
            }
            // cout << arr2[newBack];
            back=newBack;
            //saftey check. Rounded numbers can be a pain

            //  delete [] arr;
            //update arraysize
            arrSize=arrSize*2;
            //transfer over the array
            arr=arr2;
            // delete [] arr2;
        }
    }
    catch (bad_alloc ex) {
        delete[] _emergencyMemory;

        cerr << "Out of memmory while growing array";
        exit(1);
    }


}

template <typename T>
void Deque<T>::needIShiftLeft()
{
    try
    {
        //if the left has room but the right doesnt, shift left. Do so far enough to move all the way to the middle to minimize this opperation.
        if(front>=arrSize-2)
        {
            int shiftLeftBy=back/2;
            for(int i=0;i+shiftLeftBy<arrSize;i++)
            {
                arr[i]=arr[i+shiftLeftBy];
            }


            back=back-shiftLeftBy;
            front=front-shiftLeftBy;
        }
    }
    catch (bad_alloc ex) {
        delete[] _emergencyMemory;

        cerr << "Out of memmory while shifting array left";
        exit(1);
    }
}

template <typename T>
// Inserts the element at the front of the queue.
void Deque<T>::push_back(T item) {
    sleepToFix();
    try{
        //ignore emtpy Ts. they will cause havok
        //check if we need to grow. the false lets the method we are coming from a back push, so it knows what to check.
        needIGrow();
        //do we need to shift?
        needIShiftLeft();
        if(mySize==0)
        {
            arr[front]=item;
        }
        else{
            front=front+1;
            arr[front]=item;
        }
        mySize++;
    }
    catch (bad_alloc ex) {
        delete[] _emergencyMemory;

        cerr << "Out of memmory while pushing back";
        exit(1);
    }
}




// Removes and returns the element at the back of the queue.
template <typename T>
T Deque<T>::pop_front() {
        sleepToFix();
    if (mySize==0) {
        throw range_error("Tried to pop front on empty stack");
    }
    try{
        //same as pop back
        T s=arr[back];
        //arr[back]=NULL;
        //handle the initial case of front=back, dont seperate them yet
        if(front!=back)
        {
            back=back+1;
        }
        needIShrink();
        if(mySize!=0)
            mySize=mySize-1;
        return s;}
    catch (bad_alloc ex) {
        delete[] _emergencyMemory;

        cerr << "Out of memmory while popping front";
        exit(1);
    }
}

感谢您的帮助。

【问题讨论】:

  • 那是一大段代码。尝试将其缩减为minimal complete example;这样你很可能会发现错误,如果你不这样做,你仍然有一个更简单的问题要向我们展示。

标签: c++ c input


【解决方案1】:

您不能使用cin &gt;&gt; var;输入多个单词

您必须改用std::getline( std::cin , var ) 并相应地修改您的代码。

【讨论】:

  • 不幸的是,我无法更改主文件中的 cin 部分。不过,同样的主文件也适用于一些同学。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2011-05-09
  • 2014-04-05
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多