Проблемы с анализом ввода

0

Когда пользователь вводит пробелы между цифрами для создания ссылочной строки, он отбрасывает все и даже переносит числа на следующий cin. Если, однако, у них есть буквы между ними и нет пробелов, он отлично работает. Назначение предназначалось для замены страницы sim.

Код (это только часть кода, которая должна повлиять на мою проблему):

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 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);    
}
  • 0
    Вы отладили это? И, пожалуйста, не используйте вкладки в коде, размещенном здесь!
  • 2
    Можете ли вы свести это к более компактному примеру, который демонстрирует вашу проблему?
Показать ещё 2 комментария
Теги:
input

1 ответ

0

Если вы хотите, чтобы слово/строка взаимодействия cin/cout по слову/строке использовало getline, в противном случае возникнет путаница.

  • 0
    Ах, давай дадим ему короткий образец 3 лайнера ...

Ещё вопросы

Сообщество Overcoder
Наверх
Меню