繁体   English   中英

循环后跳过第一个功能输入

[英]first function input being skipped after loop

标题是自我解释的。 由于int main()循环中的某些原因,如果用户想输入另一本书,则循环将跳过第一个函数的输入,并直接询问作者的名字。 例如:

标题:指环王

作者:JRR Tolkien

版权:1954

输入由空格隔开的编号的ISBN:1 2 3 x

已签出?(是或否):是

完成(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 >> 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();
}

尝试

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

所以像这样:

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

如果那没有帮助,请查看如何冲洗cin缓冲液?

读取整行(使用getline )(如其他答案所示)时,也应注意输入流中的流氓EOL字符。

编辑:删除了cin.flush,因为它不像我想的那样标准。

你在回答时打回车吗

    Are you finished?(Y or N):

你能代替吗

cin >> question;

getline(cin,question);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM