2013-01-13 47 views
0

我需要防止垃圾留在緩衝區中,因爲在用戶輸入的菜單中調用的功能中使用了開關盒菜單的值。在cin緩衝區中留下的垃圾

菜單代碼

void menu() 
{ 
bool done = false; 
string input; 
while(!done) 
{ 
    cout << "Welcome to the DVD database." << endl; 
    cout << "1. Add A DVD" << endl; 
    cout << "2. Delete a DVD." << endl; 
    cout << "3. Edit a DVD." << endl; 
    cout << "4. List By Category." << endl; 
    cout << "5. Retrieve by a DVD by Title." << endl; 
    cout << "6. Display collection by year" << endl; 
    cout << "7. Display collection by title" << endl; 
    cout << "-999. Exit program" << endl; 
    cout << "Please choose an option by entering the corresponding number" << endl; 
    cin >> input; 
    int value = atoi(input.c_str()); 
    switch(value) 
    { 
     case 1:addDVD(); break; 
     case 2:deleteDVD(); break; 
     // case 3:editDVD(); break; 
     case 4:listByCategory();break; 
     case 6:displayByYear();break; 
     case 7:displayByTitle();break; 
     case -999: writeToFile(); exit(0); break; 
     default : cout <<"Invalid entry"<< endl; break; 
    } 
} 
} 

void retrieveByTitle() 
{ 
string search; 
int size = database.size(); 
int index = 0; 
bool found = false; 
cin.ignore(); 
cout << "Please enter the title of the DVD you would like to retrieve: " << endl; 
getline(cin,search); 
cout << search; 
while(!found && index<size) 
{ 
    if(database.at(index)->getTitle().compare(search)==0) 
    { 
     cout << database.at(index)->toString(); 
     break; 
    } 
} 
cout << endl; 
} 

如果5在菜單中進入,程序跳過

+0

你爲什麼要讀入一個字符串和使用的atoi,而不是僅僅讀入一個數? (int x; cin >> x;) – tohava

+0

當我在menu()方法中輸入5時(要進入retrieveByTitle()方法,當由retrieveByTitle()請求時,我無法輸入任何內容,當我在菜單()中做出選擇時,在retrieveByTitle中調用getline()時使用。 –

+0

剩下的是你正在按下來讀取輸入的'\ n'(或Enter),所以當程序要求標題爲' getline()'讀入一個換行符'\ n'。你需要考慮如何阻止這個... –

回答

0

此代碼工作方法的用戶輸入,但如果你消除它有你描述了同樣的問題「cin.ignore()」,從而消除由CIN >>運營商忽略了額外的分隔符:

#include <iostream> 
#include <climits> 
using namespace std; 

int main() { 
    string a, b; 
    while (true) { 

     cout << "write 'x' to exit: " << endl; 
     cin >> a; 
     if (a == "x") { 
      break; 
     }  
     cout << "read '" << a << "'" << endl; 

     cout << "now write a line: " << endl; 
     cin.clear();   // clears cin status 
     cin.ignore(INT_MAX); // clears existing, unprocessed input 

     getline(cin, a); 
     cout << "read '" << a << "'" << endl; 
    } 

    return 0; 
} 
0

交互處理ive用戶輸入您應該使用std :: getline()

每當您點擊<輸入>時,就會將std :: cin刷新到應用程序。因此,這是合乎邏輯的帆船,你應該在讀取用戶數據。

std::string answer; 
std::cout << "Question:\n"; 
std::getline(std::cin, answer); 

這可以讓你的一切在響應前面的問題提供給用戶。

一旦你有了輸入,你應該得到你認爲是輸入的值。一旦你有這個,你應該檢查是否有任何其他垃圾的輸入(如果有然後中止和重試),否則驗證你所期望的數據。

如果您希望得到一個整數;

std::stringstream linestream(answer); 
int    value; 
std::string  junk; 

if ((answer >> value)) && (!(answer >> junk))) 
{ 
    // If you got data 
    // and there was no junk on the line you are now good to go 
} 

在你的具體的例子已經有一個簡單的方法來做到這一點:

std::getline(std::cin, input); 
int value = boost::lexical_cast<int>(input); // throws an exception if there is not 
               // an int on the input (with no junk)