2009-07-17 7 views
2

タイトルは自明です。何らかの理由でint main()のループで、別のブックを入力したい場合、ループは最初の関数の入力をスキップし、著者の名前を要求するようにまっすぐに進みます。ループの後に最初の関数入力をスキップしています

タイトル:ロードオブザリング

著者:たとえばJ.R.R.トールキン

著作権:1954

はISBNをスペースで区切っ番に入力します。?1 2 3×

アウトチェック(YまたはN):Y

はあなたが終了(YまたはN)はありますか? :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」と入力します。そのreturn/enterはまだバッファに残っています。 getlineになると(cin、title); 2回目のループでは、まだバッファにあるreturn/enterが読み込まれ、行全体として解釈されます。

あなたがする必要があるのは、入力バッファを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

+0

cin.flush()の代わりにcin.ignore()を使ってみました。 – trikker

1

は試してみてください

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

したがって、このような何か:それはHow do I flush the cin buffer?

フルラインを読み込むをチェックアウトしても解決できない場合

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

(使用のgetline)の一部として、他の回答は、入力ストリームの不正なEOL文字を処理する必要があることを示しています。

編集:私が思ったように標準ではないので、cin.flushを削除しました。

+0

cin.flushを試したところ、認識されませんでした。私はまた、プログラムがぶら下がっているcin.ignore(INT_MAX)を試しました。 – trikker

1

どのように見えるかであるあなたは

getline(cin,question); 

cin >> question; 

を置き換えることができます

Are you finished?(Y or N): 

に答えながら、エンターキーを押します

+0

getlineはEnterキーを押した後にランタイムエラーを出します。 – trikker

関連する問題