Having trouble with bool Book::isEmpty() const
Having trouble with bool Book::isEmpty() const
我无法为作业获取正确的输出。
我的代码:
书。
#ifndef BOOK_H
#define BOOK_H
namespace sict {
#define MAX_NAME_SIZE 16
#define MAX_TITLE_SIZE 32
#define MIN_ISBN_VALUE 1000000000000
#define MAX_ISBN_VALUE 9999999999999
class Book {
char m_authorfamilyname[MAX_NAME_SIZE];
char m_authorgivenname[MAX_NAME_SIZE];
char m_title[MAX_TITLE_SIZE];
unsigned long long m_ISBN;
public:
void set(const char authorfamilyname[], const char authorgivenname[], const char title[], unsigned long long ISBN );
void display() const;
bool isEmpty() const;
};
}
#endif
这本书.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Book.h
#include <iostream>
#include <cstring>
using namespace std;
namespace sict {
void Book::set(const char authorfamilyname[], const char authorgivenname[], const char title[], unsigned long long ISBN)
m_authorfamilyname[0] = ' ';
m_authorgivenname[0] = ' ';
m_title[0] = ' ';
m_ISBN = 0;
strcpy(m_authorfamilyname, authorfamilyname);
strcpy(m_authorgivenname, authorgivenname);
strcpy(m_title, title);
m_ISBN = ISBN;
}
void Book::display() const {
if (isEmpty() && m_authorfamilyname[0] != ' ' && m_authorgivenname[0] != ' ' && m_title[0] != ' ' &&
m_ISBN >= MIN_ISBN_VALUE && m_ISBN <= MAX_ISBN_VALUE) {
cout << "Author: " << m_authorgivenname << ", " << m_authorfamilyname << endl;
cout << "Title: " << m_title << endl;
cout << "ISBN-13: " << m_ISBN << endl;
}
else {
cout << "The book object is empty!" << endl;
}
}
bool Book::isEmpty() const {
bool Empty;
Empty = true;
return Empty;
和我学校的主要源文件
#include <iostream>
#include "Book.h"
#include "Book.h"
using namespace std;
using namespace sict;
int main()
{
cout << "Book Management App" << endl;
cout << "===================" << endl;
Book aBook;
cout << "Testing that validation and display are correct:" << endl;
cout << "------------------------------------------------" << endl;
aBook.set("Frank", "Herbert", "Dune", 91780441172719LL);
aBook.display();
cout << "The Book::isEmpty() should return true --> "
<< (aBook.isEmpty() ? "correct" : "incorrect") << endl;
aBook.set("Frank", "Herbert", "Dune", 980441172719LL);
aBook.display();
cout << "The Book::isEmpty() should return true --> "
<< (aBook.isEmpty() ? "correct" : "incorrect") << endl;
aBook.set("Frank", "Herbert", "Dune", 9780441172719LL);
aBook.display();
cout << "The Book::isEmpty() should return false --> "
<< (aBook.isEmpty() ? "incorrect" : "correct") << endl;
return 0;
}
输出:
The book object is empty!
The Book::isEmpty() should return true --> correct
The book object is empty!
The Book::isEmpty() should return true --> correct
Author: Herbert, Frank
Title: Dune
ISBN-13: 9780441172719
The Book::isEmpty() should return false --> *incorrect*<= the problem
输出的最后一行应该是"正确"而不是"不正确"。
有没有办法将最后一行分开为"正确"?
我试过其他方法,但没有用。
这里可能有我们看不到的错误,因为这不是全部代码。如果你打算使用C++,我的建议是你研究std::string
并摆脱C风格的const char[]
。从我们所看到的,您正在将输入复制到未初始化的内存中,而您的程序没有崩溃的事实可能只是运气。在某种程度上,你的 isEmpty 似乎是倒退的。你会想要!isEmpty()