【问题标题】:Having trouble with bool Book::isEmpty() constbool Book::isEmpty() const 遇到问题
【发布时间】:2019-01-30 00:52:08
【问题描述】:

我无法为我的作业获得正确的输出。

我的代码:

书.h

#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] = '\0';
m_authorgivenname[0] = '\0';
m_title[0] = '\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] != '\0' && m_authorgivenname[0] != '\0' && m_title[0] != '\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

输出的最后一行应该是“正确”而不是“不正确”。

有什么方法可以将最后一行分隔为“正确”吗?

我试过 if else 还是不行。

【问题讨论】:

    标签: c++ boolean


    【解决方案1】:

    这里可能有我们看不到的错误,因为这不是所有代码。如果您要使用 C++,我建议您查看 std::string 并摆脱 C 风格的 const char[]。从我们可以看到,您正在将输入复制到未初始化的内存中,而您的程序没有崩溃的事实可能只是运气。在有意义的范围内,您的 isEmpty 似乎倒退了。你会想要!isEmpty()

    【讨论】:

    • 是的,使用字符串,完全使用 C++。否则你将永远被称为 C+ 编码员,C 和 C++ 世界之间的奇怪炼狱 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2021-08-09
    • 2017-05-02
    相关资源
    最近更新 更多