제목 자체 설명입니다. 어떤 이유로 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