【问题标题】:C++ trying to see if an input string is equal to a array string of a classC ++试图查看输入字符串是否等于类的数组字符串
【发布时间】:2021-06-04 14:42:13
【问题描述】:

我正在尝试按标题或 ISBN 在我的数组中搜索一本书。但是,我无法使用我的搜索功能找到已经在数组中的书籍。我不知道为什么它不起作用。我认为代码是正确的。前几个条目的 ISBN 搜索工作,然后其余的搜索失败。但是,所有标题的搜索都失败了。由于每个人的代码几乎相同,我不知道为什么它部分适用于其中一个而根本不适用于另一个。有什么提示可以让搜索正常工作吗?

// BooksProgram.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>


using namespace std;

void readData(Book_Class books[]);
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[]);

int main()
{
    
    int menu = 0;
    
    Book_Class books[100];
    readData(books);
        
   
    while (menu != 5)
    {
        MainMenu(menu);
        if (menu == 1)
        {
            searchLibrary(books);
        }
        else if (menu == 2)
        {
            
        }
        else if (menu == 3)
        {
            
        }
        else if (menu == 4)
        {
            
        }
        else if (menu == 5)
        {
            
        }
      
    }
    
}
void readData(Book_Class books[])
{
    ifstream in;
    string line;
    int numAuthors = 0;
    int i = 0;
    int year = 0;
    double price = 0;
    int copies = 0;
    string author1;
    string author2;
    string author3;
    string author4;
    in.open("Books.txt");
    if (in.fail())
    {
        cout << "File did not open." << endl;
    }

    while (!in.eof())
    {
        getline(in, line);
        numAuthors = stoi(line);
        books[i].setNumAuthors(numAuthors);
        if (numAuthors >= 5)
        {
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 4)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            getline(in, line);
            author3 = line;
            getline(in, line);
            author4 = line;
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 3)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            getline(in, line);
            author3 = line;
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 2)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            author3 = "";
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 1)
        {
            getline(in, line);
            author1 = line;
            author2 = "";
            author3 = "";
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }

        i++;
    }
    in.close();
}
void MainMenu(int& menu)
{
    cout << setw(20) << "Main Menu" << endl;
    cout << setw(36) << setfill('*') << "\n" << endl;
    cout << "1. Search for book by Title or ISBN." << endl;
    cout << "2. Add a new book to inventory." << endl;
    cout << "3. Update the number of copies on hand." << endl;
    cout << "4. Update the price of a book." << endl;
    cout << "5. Exit" << endl;
    cout << setw(36) << "\n" << endl;
    cout << "Please enter a menu number to proceed: " << endl;
    cin >> menu;
    while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
    {
        cout << "Menu options are 1 - 6" << endl;
        cout << "Please enter a menu number to proceed: " << endl;
        cin.clear();
        cin.ignore(100, '\n');
        cin >> menu;
    }
    cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[])
{
    int search;
    int i = 0;
    int numAuthors = 0;
    bool flag = false;
    string title;
    string ISBN;
    cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
    cin >> search;
    while (search != 1 && search != 2)
    {
        cout << "Invalid Selection." << endl;
        cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
        cin.clear();
        cin.ignore(100, '\n');
        cin >> search;
    }
    if (search == 1)
    {
        cout << "Please enter the title of the book: " << endl;
        cin >> title;
       
        while (i < sizeof(books) -1)
        {
           
            if (title == books[i].getTitle())
            {
                flag = true;
                books[i].showTitle();
                numAuthors = books[i].getNumAuthors();
                books[i].showAuthor(numAuthors);
                books[i].showPublisher();
                books[i].showYear();
                books[i].showPrice();
                books[i].showInStock();
                break;
            }
            else
            {
                flag = false;
                i++;
            }
           
        }
    }
    else if (search == 2)
    {
        cout << "Please enter the ISBN: " << endl;
        cin >> ISBN;
        while (i < sizeof(books) - 1)
        {
            if (ISBN == books[i].getISBN())
            {
                flag = true;
                books[i].showTitle();
                numAuthors = books[i].getNumAuthors();
                books[i].showAuthor(numAuthors);
                books[i].showPublisher();
                books[i].showYear();
                books[i].showPrice();
                books[i].showInStock();
                break;
            }
            else
            {
                flag = false;
                 i++;
            }
            
        }
    }
    if (!flag)
    {
        cout << "The book is not in stock." << endl;
    }
    return flag;
}

这是我的课:

#pragma once
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

class Book_Class
{
    
private:
    string title;
    string ISBN;
    string publisher;
    double price;
    int year;
    string author1;
    string author2;
    string author3;
    string author4;
    int inStock;
    int numAuthors;

public:
    void showTitle()
    {
        cout << left << setw(10) << left << setw(12);
        cout << "Title: " << title << endl;
    }
    string getTitle()
    {
        return title;
    }
    string getISBN()
    {
        return ISBN;
    }
    void setTitle(string bookTitle)
    {
        title = bookTitle;
    }
    void showISBN()
    {
        cout << left << setw(10) << left << setw(12);
        cout << "ISBN: " << ISBN << endl;
    }
    void setISBN(string isbn)
    {
        ISBN = isbn;
    }
    void showPublisher()
    {
        cout << left << setw(10) << left << setw(12);
        cout << "Publisher: " << publisher << endl;
    }
    void setPublisher(string pub)
    {
        publisher = pub;
    }
    void showPrice()
    {
        cout << left << setw(10) << left << setw(12);
        cout << fixed << showpoint << setprecision(2);
        cout.imbue(locale("American"));
        cout << showbase;
        cout << "Price: " << put_money(price*100) << endl;
    }
    void setPrice(double amount)
    {
        price = amount;
    }
    void showYear()
    {
        cout << left << setw(10) << left << setw(12);
        cout.imbue(locale());
        cout << "Year: " << year << endl;
    }
    void setYear(int yearPub)
    {
        year = yearPub;
    }
    void showAuthor(int numAuthors)
    {
        cout << left << setw(10) << left << setw(4);
        if (numAuthors == 4)
        {
            cout << "Author(s):  " << author1 << endl;
            cout << "Author(s):  " << author2 << endl;
            cout << "Author(s):  " << author3 << endl;
            cout << "Author(s):  " << author4 << endl;
        }
        else if (numAuthors == 3)
        {
            cout << "Author(s):  " << author1 << endl;
            cout << "Author(s):  " << author2 << endl;
            cout << "Author(s):  " << author3 << endl;
        }
        else if (numAuthors == 2)
        {
            cout << "Author(s):  " << author1 << endl;
            cout << "Author(s):  " << author2 << endl;
        }
        else
        {
            cout << "Author(s):  " << author1 << endl;
        }
    }
    void setAuthor(string auth1, string auth2, string auth3, string auth4)
    {
        author1 = auth1;
        author2 = auth2;
        author3 = auth3;
        author4 = auth4;
    }
    void showInStock()
    {
        cout << left << setw(10) << left << setw(12);
        cout << "In Stock: " << inStock << endl;
    }
    void setInStock(int copies)
    {
        inStock = copies;
    }
    void setNumAuthors(int numbAuthors)
    {
        numAuthors = numbAuthors;
    }
    int getNumAuthors()
    {
        return numAuthors;
    }
    Book_Class()
    {
        title = "";
        ISBN = "";
        publisher = "";
        price = 0.0;
        year = 0;
        author1 = "";
        author2 = "";
        author3 = "";
        author4 = "";
        inStock = 0;
        numAuthors = 0;
    }
    Book_Class(string name, string number, string pub, double amount, int yearPub, string auth1, string auth2, string auth3, string auth4, int copies, int numOfAuth)
    {
        title = name;
        ISBN = number;
        publisher = pub;
        price = amount;
        year = yearPub;
        author1 = auth1;
        author2 = auth2;
        author3 = auth3;
        author4 = auth4;
        inStock = copies;
        numAuthors = numOfAuth;
    }
};

这是 Books.txt 文件

5
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1
Hemingway, Ernest
The Sun Also Rises
978-8087888155
Scribner's
1954
19.99
10
1
Hemingway, Ernest
The Old Man and the Sea
978-1781396803
Benediction Books
1952
17.99
25
1
Carroll, Lewis
Alice in Wonderland
9783959401807
MacMillan Publishers
1865
10.00
15
1
Tolkein, J.R.R.
The Fellowship of the Ring
978-0544003415
Mariner Books
1954
29.99
102
1
Tolkein, J.R.R.
The Two Towers
978-0345339713
Mariner Books
1954
19.99
54

【问题讨论】:

  • 您可以使用 for 循环来读取适当数量的作者,而不是每个可能的作者数量的 if/else 情况。
  • 感谢您的提示。我能做到。我明白你在说什么。我遇到的问题是我的搜索功能无法正确搜索标题或 ISBN。它适用于 ISBN,就像前 3 个数组元素一样,就是这样。根本不适用于标题。
  • 不确定您的数据是什么,但一般来说,作者通常超过四个;如果您想对您的代码进行未来验证,我会考虑将另一个规范化为表格。 Higgs at the LHC 拥有超过 5000 位作者。
  • 这是一个课堂作业,他们希望我们使用不超过四个作者。

标签: c++ arrays string class


【解决方案1】:

我认为获取书籍大小的表达式 sizeof(books) 不像您期望的那样工作。试试这个在函数 readData 中计算大小的程序。

#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>

using namespace std;

int readData(Book_Class books[]);   //Changed
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[], int sizeOfBooks); //Changed



int main()
{
    
    int menu = 0;
    
    Book_Class books[100];
    int sizeOfBooks = readData(books); //Changed
        
   
    while (menu != 5)
    {
        MainMenu(menu);
        if (menu == 1)
        {
            searchLibrary(books, sizeOfBooks); //Changed
        }
        else if (menu == 2)
        {
            
        }
        else if (menu == 3)
        {
            
        }
        else if (menu == 4)
        {
            
        }
        else if (menu == 5)
        {
            
        }
      
    }
    
}
int readData(Book_Class books[]) //Changed
{
    ifstream in;
    string line;
    int numAuthors = 0;
    int i = 0;
    int year = 0;
    double price = 0;
    int copies = 0;
    string author1;
    string author2;
    string author3;
    string author4;
    in.open("Books.txt");
    if (in.fail())
    {
        cout << "File did not open." << endl;
    }

    while (!in.eof())
    {
        getline(in, line);
        numAuthors = stoi(line);
        books[i].setNumAuthors(numAuthors);
        if (numAuthors >= 5)
        {
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 4)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            getline(in, line);
            author3 = line;
            getline(in, line);
            author4 = line;
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 3)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            getline(in, line);
            author3 = line;
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 2)
        {
            getline(in, line);
            author1 = line;
            getline(in, line);
            author2 = line;
            author3 = "";
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }
        else if (numAuthors == 1)
        {
            getline(in, line);
            author1 = line;
            author2 = "";
            author3 = "";
            author4 = "";
            books[i].setAuthor(author1, author2, author3, author4);
            getline(in, line);
            books[i].setTitle(line);
            getline(in, line);
            books[i].setISBN(line);
            getline(in, line);
            books[i].setPublisher(line);
            getline(in, line);
            year = stoi(line);
            books[i].setYear(year);
            getline(in, line);
            price = stod(line);
            books[i].setPrice(price);
            getline(in, line);
            copies = stoi(line);
            books[i].setInStock(copies);
        }

        i++;
    }
    in.close();

    return i; //Changed
}
void MainMenu(int& menu)
{
    cout << setw(20) << "Main Menu" << endl;
    cout << setw(36) << setfill('*') << "\n" << endl;
    cout << "1. Search for book by Title or ISBN." << endl;
    cout << "2. Add a new book to inventory." << endl;
    cout << "3. Update the number of copies on hand." << endl;
    cout << "4. Update the price of a book." << endl;
    cout << "5. Exit" << endl;
    cout << setw(36) << "\n" << endl;
    cout << "Please enter a menu number to proceed: " << endl;
    cin >> menu;
    while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
    {
        cout << "Menu options are 1 - 6" << endl;
        cout << "Please enter a menu number to proceed: " << endl;
        cin.clear();
        cin.ignore(100, '\n');
        cin >> menu;
    }
    cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[], int sizeOfBooks) //Changed
{
    int search;
    int i = 0;
    int numAuthors = 0;
    bool flag = false;
    string title;
    string ISBN;
    cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
    cin >> search;
    while (search != 1 && search != 2)
    {
        cout << "Invalid Selection." << endl;
        cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
        cin.clear();
        cin.ignore(100, '\n');
        cin >> search;
    }
    if (search == 1)
    {
        cout << "Please enter the title of the book: " << endl;
        cin >> title;
       
        while (i < sizeOfBooks -1)    //Changed
        {
           
            if (title == books[i].getTitle())
            {
                flag = true;
                books[i].showTitle();
                numAuthors = books[i].getNumAuthors();
                books[i].showAuthor(numAuthors);
                books[i].showPublisher();
                books[i].showYear();
                books[i].showPrice();
                books[i].showInStock();
                break;
            }
            else
            {
                flag = false;
                i++;
            }
           
        }
    }
    else if (search == 2)
    {
        cout << "Please enter the ISBN: " << endl;
        cin >> ISBN;
        while (i < sizeOfBooks - 1)   //Changed
        {
            if (ISBN == books[i].getISBN())
            {
                flag = true;
                books[i].showTitle();
                numAuthors = books[i].getNumAuthors();
                books[i].showAuthor(numAuthors);
                books[i].showPublisher();
                books[i].showYear();
                books[i].showPrice();
                books[i].showInStock();
                break;
            }
            else
            {
                flag = false;
                 i++;
            }
            
        }
    }
    if (!flag)
    {
        cout << "The book is not in stock." << endl;
    }
    return flag;
}

【讨论】:

  • 我无法让它工作。它不会让我将 void 函数 readData 初始化为 int。
  • 另外我继续在搜索函数中将 99 硬编码到我的 while 循环中,因为我知道我的数组的大小并且搜索函数仍然找不到标题,只有前三个国际标准书号。不知道为什么它只捕获前 3 个 ISBN 而不是其余的。
  • 好的。你说的我都试过了,还是不行。我们只应该在启动程序时在数组中填充 10 个条目,但数组应该有 100 个元素。
猜你喜欢
  • 2022-01-10
  • 2021-09-11
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
相关资源
最近更新 更多