遇到布尔Book :: isEmpty()const麻烦

时间:2019-01-30 00:52:08

标签: c++ boolean

我在为作业分配正确的输出时遇到麻烦。

我的密码:

Book.h

DECLARE @x XML = 'bla bla bla'
DECLARE @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT ,@x 

SELECT * INTO #t
FROM OPENXML (@idoc, '/',2)
EXEC sp_xml_removedocument @idoc 

SELECT [ogrid_cde] = t3.text, [role] = t4.text
FROM #t as t1
INNER JOIN #t as t2 ON t1.parentid = t2.parentid
INNER JOIN #t as t3 ON t1.id = t3.parentid
INNER JOIN #t as t4 ON t2.id = t4.parentid
WHERE t1.localname = 'ogrid_cde' and t2.localname = 'role'

Book.cpp

#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

还有我学校的主要源文件

#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;
}

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

是否可以将最后一行分隔为“正确”?

我尝试了其他方法,但没有成功。

1 个答案:

答案 0 :(得分:1)

在这里可能看不到错误,因为这不是全部代码。如果您要使用C ++,我建议您研究std::string并摆脱C风格的const char[]。从我们可以看到,您正在将输入复制到未初始化的内存中,并且程序不会崩溃的事实可能只是运气。在一定程度上,您的isEmpty似乎倒退了。您可能需要!isEmpty()