[英]C++ trying to see if an input string is equal to a array string of a class
我正在嘗試按標題或 ISBN 在我的數組中搜索一本書。 但是,我無法使用搜索 function 找到數組中已經存在的書籍。 我不知道為什么它不起作用。 我認為代碼是正確的。 前幾個條目的 ISBN 搜索工作,然后 rest 的搜索失敗。 但是,所有標題的搜索都失敗了。 由於每個人的代碼幾乎相同,我不知道為什么它部分適用於其中一個而根本不適用於另一個。 有什么提示可以讓搜索正常工作嗎?
// 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;
}
這是我的 class:
#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
我認為獲取書籍大小的表達式sizeof(books)並沒有像您期望的那樣工作。 試試這個程序,它計算 function 中的大小,讀取數據。
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.