c++ 为什么 while 循环实际上不循环?

问题描述 投票:0回答:1

我正在编写一个程序来跟踪您已读过、想读或未读完的书籍。当我第一次运行该程序时,我添加一本新书,然后查看列表,然后尝试再次添加一本新书,但它卡住了,询问同样的事情(您想:a:添加新书,b:查看列表吗? , :exit) 无论我多少次输入“a”。如果我之前已经查看过列表一次,那么当我尝试查看列表时,也会发生同样的情况。我已经研究了几个小时,很抱歉,如果这是显而易见的,但我做错了什么,我应该改变什么?这是我到目前为止所拥有的:

string AddNewBook() {
    string title;
    string author;
    string titleandauthor;
    
    cout << "\nWhat is the title of the book?\n   " ;
    getline(cin, title);
    
    cout << "\nWho is the author?\n   " ;
    getline(cin, author);
    
    titleandauthor = title + " by " + author;
        
    return titleandauthor;
}

int main() {
    string q1;
    string title;
    string author;
    string q2;
    string book;
    string addbookq = "y";
    vector<string> booksread;
    vector<string> tbr;
    vector<string> dnf;
    string seelistq;
    int readnument;
    int tbrnument;
    int dnfnument;
    int i;
    string seeanotherlistq = "y";
    
    cout << "Welcome to the Book Tracker!\n" << endl;
    
    cout << "Would you like to:\n   A: Add a new book\n   B: View a list\n   C: Exit" << endl;
    cout << "Enter letter: " ;
    cin >> q1;
    cin.ignore();
    while (q1 == "a" or q1 == "A" or q1 == "b" or q1 == "B") {
        if (q1 == "A" or q1 == "a") {
            while (addbookq == "Y" or addbookq == "y" or addbookq == "Yes" or addbookq == "yes") {
                book = AddNewBook();
                cout << "\nWould you like to:\n   A: Add this book to the Read list\n   B: Add this book to the To Be Read list\n   C: Add this book to the Did Not Finish list" << endl;
                cout << "Enter letter: " ;
                getline(cin, q2);
                while (q2 != "A" && q2 != "a" && q2 != "B" && q2 != "b" && q2 != "C" && q2 != "c") {
                    cout << "\nInvalid entry, please try again: " ;
                    getline(cin, q2);
                }
                if (q2 == "A" or q2 == "a") {
                    booksread.push_back(book);
                    cout << "Added to Read list." << endl;
                }
                else if (q2 == "B" or q2 == "b") {
                    tbr.push_back(book);
                    cout << "Added to the To Be Read List." << endl;
                }
                else if (q2 == "C" or q2 == "c") {
                    dnf.push_back(book);
                    cout << "Added to the Did Not Finish List." << endl;
                }
                cout << "\nAdd another book? " ;
                getline(cin, addbookq);
            }
        }
        else if (q1 == "B" or q1 == "b") {
            while (seeanotherlistq == "Y" or seeanotherlistq == "y" or seeanotherlistq == "Yes" or seeanotherlistq == "yes") {
                cout << "\nWhich list would you like to see:\n   A: Read list\n   B: To Be Read list\n   C: Did Not Finish list" << endl;
                cout << "Enter letter: " ;
                getline(cin, seelistq);
                if (seelistq == "a" or seelistq == "A") {
                    if (booksread.size() == 0) {
                        cout << "\nThere are no books in the Read List yet." << endl;
                    }
                    else if (booksread.size() == 1) {
                        cout << "\nThere is 1 book in the Read list: " << booksread.at(0) << endl;
                    }
                    else {
                        cout << "\nThere are " << booksread.size() << " books in the Read list. How many titles would you to see?" << endl;
                        cin >> readnument;
                        cout << endl;
                        for (i = 0; i < readnument; i++) {
                            cout << i + 1 << ". " << booksread.at(i) << endl;
                        }
                    }
                }
                else if (seelistq == "b" or seelistq == "B") {
                    if (tbr.size() == 0) {
                        cout << "\nThere are no books in the To Be Read list yet." << endl;
                    }
                    else if (tbr.size() == 1) {
                    cout << "\nThere is 1 book in the To Be Read list: " << tbr.at(0) << endl;
                    }
                    else {
                        cout << "\nThere are " << tbr.size() << " books in the To Be Read list. How many titles would you like to see?" << endl;
                        cin >> tbrnument;
                        cout << endl;
                        for (i = 0; i < tbrnument; i++) {
                            cout << i + 1 << ". " << tbr.at(i) << endl;
                        }
                    }
                }
                else if (seelistq == "c" or seelistq == "C") {
                    if (dnf.size() == 0) {
                        cout << "\nThere are no books in the To Be Read list yet." << endl;
                    }
                    else {
                        cout << "\nThere are " << dnf.size() << " books in the Did Not Finish list. How many titles would you like to see?" << endl;
                        cin >> dnfnument;
                        for (i = 0; i < dnfnument; i++) {
                            cout << i + 1 << ". " << dnf.at(i) << endl;
                        }
                    }
                }
                cout << endl;
                cout << "See another list? " ;
                getline(cin, seeanotherlistq);
            }
        }
        cout << "\nWould you like to:\n   A: Add a new book\n   B: View a list\n   C: Exit" << endl;
        cout << "Enter letter: " ;
        cin >> q1;
        cin.ignore();
    }

    cout << "\nThank you for using the Book Tracker!" << endl;
    
    return 0;
}

这是一个示例输出:

Welcome to the Book Tracker!

Would you like to:
   A: Add a new book
   B: View a list
   C: Exit
Enter letter: a

What is the title of the book?
   dark disciple

Who is the author?
   christie golden

Would you like to:
   A: Add this book to the Read list
   B: Add this book to the To Be Read list
   C: Add this book to the Did Not Finish list
Enter letter: b
Added to the To Be Read List.

Add another book? n

Would you like to:
   A: Add a new book
   B: View a list
   C: Exit
Enter letter: b

Which list would you like to see:
   A: Read list
   B: To Be Read list
   C: Did Not Finish list
Enter letter: b

There is 1 book in the To Be Read list: dark disciple by christie golden

See another list? n

Would you like to:
   A: Add a new book
   B: View a list
   C: Exit
Enter letter: a

Would you like to:
   A: Add a new book
   B: View a list
   C: Exit
Enter letter: b

Would you like to:
   A: Add a new book
   B: View a list
   C: Exit
Enter letter: c

Thank you for using the Book Tracker!
 
c++ loops while-loop
1个回答
0
投票

在主循环中选择

A
B
且下一个
while
循环完成其工作后,您不会重置终止循环的
addbookq
seeanotherlistq
变量,因此下一次再次到达循环,它没有任何工作要执行。

因此,要么重置变量,要么使用

do..while
循环。

© www.soinside.com 2019 - 2024. All rights reserved.