タイトルは自明です。何らかの理由で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
ご協力いただきありがとうございます - ありがとうございます。
文字列として質問を読み込むことはできませんが、cin.flush()を試しましたが、istreamにはflushという名前のメンバーはありません。私はそれを自分で定義する必要がありますか、ヘッダーがありませんか? – trikker
cin.flush()の代わりにcin.ignore()を使ってみました。 – trikker