0
當用戶在數字之間輸入空格以創建引用字符串時,它會拋出所有內容,甚至會將數字帶到下一個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);
}
你調試了嗎?並且plz不要在這裏發佈的代碼中使用標籤! –
你可以減少這個更簡潔的例子,說明你的問題? – Barmar
這是太多的代碼來顯示你的問題。但是,一般來說,如果你需要解析用戶輸入,你應該使用'getline'並解析輸入字符串,而不是依靠用戶來獲取輸入格式的細節。 –