2009-07-17 55 views
2

標題不言自明。出於某種原因,在int main()的循環中,如果用戶想要輸入另一本書,循環會跳過第一個函數的輸入並直接詢問作者的名字。例如:循環後跳過第一個功能輸入

標題:指環王

作者:J.R.R.托爾金

版權所有:1954年

回車ISBN編號爲用空格分隔:?1 2 3×

經過了(是或否):Y

你完成(是或否)? :N //這個循環開始了,Y會發出破

標題://居然有這條線而下,這應該有

驗證之間沒有空格或://它跳過到這一行,而不是允許用戶輸入標題,這個週期繼續,如果用戶繼續輸入信息 - 總是跳過輸入標題

代碼:

#include "std_lib_facilities.h" 

class Book{ 
public: 
     vector<Book> books; // stores book information 
     Book() {}; // constructor 
     string what_title(); 
     string what_author(); 
     int what_copyright(); 
     void store_ISBN(); 
     void is_checkout(); 
private: 
     char check; 
     int ISBNfirst, ISBNsecond, ISBNthird; 
     char ISBNlast; 
     string title; 
     string author; 
     int copyright; 
}; 

string Book::what_title() 
{ 
     cout << "Title: "; 
     getline(cin,title); 
     cout << endl; 
     return title; 
} 

string Book::what_author() 
{ 
     cout << "Author: "; 
     getline(cin,author); 
     cout << endl; 
     return author; 
} 

int Book::what_copyright() 
{ 
    cout << "Copyright Year: "; 
    cin >> copyright; 
    cout << endl; 
    return copyright; 
} 

void Book::store_ISBN() 
{ 
    bool test = false; 
    cout << "Enter ISBN number separated by spaces: "; 
    while(!test){ 
    cin >> ISBNfirst >> ISBNsecond >> ISBNthird >> ISBNlast; 
    if((ISBNfirst<0 || ISBNfirst>9) || (ISBNsecond<0 || ISBNsecond>9) || (ISBNthird<0 || ISBNthird>9)) 
        error("Invalid entry."); 
    else if(!isdigit(ISBNlast) && !isalpha(ISBNlast)) 
      error("Invalid entry."); 
    else test = true;}  
    cout << endl; 
} 

void Book::is_checkout() 
{ 
    bool test = false; 
    cout << "Checked out?(Y or N): "; 
    while(!test){ 
    cin >> check; 
    if(check == 'Y') test = true; 
    else if(check == 'N') test = true;         
    else error("Invalid value.");} 
    cout << endl; 
} 

int main() 
{ 
    Book store; 
    char question = '0'; 
    while(true){ 
     store.what_title(); 
     store.what_author(); 
     store.what_copyright(); 
     store.store_ISBN(); 
     store.is_checkout(); 
     store.books.push_back(store); 
     cout << "Are you finished?(Y or N): "; 
     cin >> question; 
     if(question == 'Y') break; 
     else if(question == 'N') cout << endl; 
     else error("Invalid value."); 
     } 
    cout << endl; 
    keep_window_open(); 
} 

的如果你對像keep_window_open()和error()這樣的函數感興趣,可以在這裏找到頭文件信息,但是它並不涉及這個問題。 - http://www.stroustrup.com/Programming/std_lib_facilities.h

任何幫助將不勝感激 - 謝謝。

回答

5

下面的行:

cin >> question; 

在 'Y' 或 'N' 的字符進行讀取。當你輸入輸入時,你也可以輸入'return'或'enter'。該返回/輸入仍在緩衝區中。當你得到getline(cin,title); 第二次通過循環,返回/輸入仍然在緩衝區中被讀入並解釋爲整行。

你需要做的是用cin.flush()清除輸入緩衝區;或者您需要以字符串形式閱讀而不是字符。

這裏是你的代碼應該是什麼樣子

int main() 
{ 
    Book store; 
    char question = '0'; 
    while(true){ 
     store.what_title(); 
     store.what_author(); 
     store.what_copyright(); 
     store.store_ISBN(); 
     store.is_checkout(); 
     store.books.push_back(store); 
     cout << "Are you finished?(Y or N): "; 
     cin >> question; 
     if(question == 'Y') break; 
     else if(question == 'N') 
     { 
      cout << endl; 
      cin.flush(); 
     } 
     else error("Invalid value."); 
     } 
    cout << endl; 
    keep_window_open(); 
} 
+0

作爲字符串閱讀問題不起作用,但我試過cin.flush(),istream沒有名爲flush的成員。我需要自己定義它還是缺少標題? – trikker 2009-07-17 16:48:52

+0

我嘗試使用cin.ignore()而不是cin.flush(),它工作。我將把它作爲答案。 – trikker 2009-07-17 16:55:20

1

嘗試

std::cin.ignore(INT_MAX);(); 

因此,像這樣:

string Book::what_title() 
{ 
     cout << "Title: "; 
     std::cin.ignore(INT_MAX); 
     getline(cin,title); 
     cout << endl; 
     return title; 
} 

如果不幫助檢查出How do I flush the cin buffer?

讀全線(使用函數getline),因爲一些其他答案表明還應該照顧輸入流中的流氓EOL字符。

編輯:自認爲,我認爲是沒有標準刪除了cin.flush。

+0

我試過cin.flush,但無法識別。我也試過cin.ignore(INT_MAX),它只是讓程序掛起。 – trikker 2009-07-17 16:49:29

1

你按下回車鍵在回答

Are you finished?(Y or N): 

可以更換

cin >> question; 

getline(cin,question); 

+0

getline在命中後給出運行時錯誤 – trikker 2009-07-17 16:51:28

相關問題