2009-07-17 3 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); 두 번째로 루프를 통과하면 버퍼에있는 return/enter가 읽혀 전체 행으로 해석됩니다.

당신이해야 할 일은 입력 버퍼를 cin.flush(); 또는 질문으로 문자열이 아닌 문자로 읽어야합니다.여기

Are you finished?(Y or N): 

에 대답하면서 코드를 입력 히트 않는

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

과 같아야 당신이 대체 할 수있는 것입니다

cin >> question; 

getline(cin,question); 
,536와

?

+0

getline은 Enter 키를 눌러 런타임 오류를 발생시킵니다. – trikker

관련 문제