输入解析问题

时间:2013-11-22 17:51:33

标签: c++ input

当用户在数字之间输入空格以创建引用字符串时,它会抛出所有内容,甚至会将数字转移到下一个cin。但是,如果它们之间有字母而没有空格则可以正常工作。作业是用于替换页面的。

代码(这只是应该影响我的问题的代码的一部分):

void main()
{
    bool again = false;
    bool reuse = false;
    bool valid;
    int use;
    int refLength;
    vector<int> ref(0);
    vector<frame> frames(0);
    string refString;
    string user;

    //ask how long the user wants the ref string to be
    do{
        valid = false;
        while (!valid&&!reuse)//take choice of ref string
        {
            cout << "How long do you wish the reference string to be? ";
            if (!(cin >> refLength))
            {
                cin.clear();//.clear and .ignore keep cin from errors and forced infinite loop repeating following cout
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "The length must be enter as an integer value between ";
                valid = false;

            }

            else
            {
                cout << "You have chosen to have a Ref string of " << refLength << " length " << endl;
                valid = true;
            }
        }
        valid = false;

        while (!valid&&!reuse)
        {
            cout << "Do you want to enter a ref string or randomly generate one" << endl;
            cout << "of your chosen length? Enter 1 to generate and 2 to input the string ";
            if (!(cin >> use) | (use<0 || use>2))
            {
                cin.clear();//.clear and .ignore keep cin from errors and forced infinite loop repeating following cout
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "You must enter an integer between 1 and 2";

            }
            else
            {
                valid = true;
                if (use == 1)
                {
                    make(ref, refLength);
                }
                else
                {
                    cout << "please enter a ref string of chosen length entering" << endl;
                    cout << "fewer digits will cause 0's to be added. Entering more" << endl;
                    cout << "will cause those past the chosen length to be dropped " << endl;
                    cout << "any letters will be ignored but spaces throw things off" << endl;
                    cout << "Also all entries must be single digit integers (0-9) " << endl;
                    cin >> refString;
                    make(ref, refLength, refString);
                }
                use = 0;
            }

        }
        cout << endl;
        /*for(int i=0;i<ref.size();i++)
        {
        cout<<ref[i]<<" ";
        }*/
        valid = false;
        while (!valid)
        {
            cin.clear();
            //errors ********************************************88
            cout << "How many frames do you want (1-7) ";
            if (!(cin >> use) | (use<0 || use>7))
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "You must enter an integer between 1 and 7 ";
                valid = false;
            }
            else
            {
                valid = true;
                setUpFrames(use, frames);
                use = 0;
            }
        }
        valid = false;
        while (!valid)
        {
            cout << "Enter 1 for FIFO or 2 for LRU pageRep algo or 3 for Optimal";

            if (!(cin >> use) | (use<0 || use>3))
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Must be int between 1 and 3";
            }
            else if (use == 1)
            {
                cout << endl << "# of Page Faults ";
                cout << FIFO(ref, frames);
                valid = true;
            }
            else if (use == 2)
            {
                cout << endl << "# of Page Faults ";
                cout << LRU(ref, frames);
                valid = true;
            }
            else
            {
                cout << endl << "# of Page Faults ";
                cout << Optimal(ref, frames);
                valid = true;
            }

        }
        cout << endl;
        cout << "do you want to try again ? Enter y for yes anything else for no" << endl;
        cin >> user;
        if (user == "y")
        {
            again = true;
            cout << "do you want to use the same reference string? y for yes anything else for no" << endl;
            cin >> user;
            if (user == "y")
            {
                reuse = true;
            }
            else
            {
                reuse = false;
            }
        }
        else
        {
            again = false;
        }    
    } while (again);    
}

1 个答案:

答案 0 :(得分:0)

如果你想要cin / cout交互词/逐行/行使用getline,否则会出现混淆。