C++ c++;:使用新算子的动态数组

C++ c++;:使用新算子的动态数组,c++,arrays,pointers,memory-leaks,global-variables,C++,Arrays,Pointers,Memory Leaks,Global Variables,用户输入我必须以数组形式收集的书籍。 你能帮我理解为什么当我进入第二本书时,第一本是埃里森吗? 我定义了一本书 你能帮我理解为什么我不能收集吗 #define stop __asm nop #include "book.h" #include <iostream> #include <cstdio> using namespace std; int counter = 0; BOOK aux [1]; void print_catalogue() { } v

用户输入我必须以数组形式收集的书籍。 你能帮我理解为什么当我进入第二本书时,第一本是埃里森吗? 我定义了一本书

你能帮我理解为什么我不能收集吗

#define   stop __asm nop
#include "book.h"
#include <iostream>
#include <cstdio>

using namespace std;

int counter = 0;
BOOK aux [1];

void print_catalogue()
{

}

void print_book(BOOK aBook)
{
    cout << endl;
    cout <<aBook.Autor<<", "<<aBook.Title<<", "<<aBook.Year<<", "<<aBook.PageCount<<", "<<aBook.Cost<<endl; 
}


void new_book()
{
    BOOK temp;
    system("cls");
    cin.getline (temp.Autor, 20);
    cout <<"ENTERING NEW BOOK: " << endl <<endl;
    cout <<"Input the author: ";
    cin.getline (temp.Autor, 20);
    cout  <<"Input the title: ";
    cin.getline (temp.Title, 50);
    cout  <<"Input the year of publishing: ";
    cin >>  temp.Year;
    cout  <<"Input the number of pages: ";
    cin >>  temp.PageCount;
    cout  <<"Input the cost: ";
    cin >>  temp.Cost;
    cout << endl;   

    counter++;

    BOOK * pn = new BOOK [counter];
    if (counter > 0)
    {
        memcpy(pn, aux, counter * sizeof(BOOK));    
    }
    pn[counter - 1] = temp;

    BOOK * aux = new BOOK[counter];
    memcpy(aux,pn, counter * sizeof(BOOK));

    delete[] pn;

    for (int i = 0; i < counter; i++)
    {
        print_book(aux[i]);
    }   

    system("pause");
    return;
}

void delete_books()
{

}

 void write_catalogue()
 {

 }

 void read_catalogue()
 {

 }

void menu()
{
    char command;
    do
    {       
        system("cls");

        cout << "p - Print the whole catalogue."<< endl;
        cout << "n - Input a new book." << endl;
        cout << "d - Delete existing book(s)." << endl;
        cout << "w - Write the catalogue to a file." << endl;
        cout << "r - Read the catalogue from a file." << endl;
        cout << "Input a new command: ";
        cin >> command;


        cout << endl << endl;
        switch (command) 
        {
            case 'p': print_catalogue(); break;
            case 'n': new_book(); break;
            case 'd': delete_books(); break;
            case 'w': write_catalogue(); break;
            case 'r': read_catalogue(); break;
            case 'q': return;
            default : 
                {
                    cout << "Please, enter a correct command." << endl;
                    system("pause");
                }
        }

    } while(true);
}

void main()
{
    menu();

}
#定义停止(asm nop)
#包括“book.h”
#包括
#包括
使用名称空间std;
int计数器=0;
图书辅助[1];
作废印刷品目录()
{
}
作废印刷书(书籍A)
{

cout您的全局aux是指向一本书的常量指针–aux[0],不能超过。您不能复制多本书。 您重新定义了一个本地aux,并隐藏了全局。在函数new_book()返回后,您在其中放置的是“泄漏”


使用std::string和容器,不要滥用指针和全局变量。

为什么使用原始指针?为什么使用memcpy?为什么使用系统(“暂停”)?你为什么不使用
向量
?你使用过调试器来查看内存状态吗?这会对你有很大帮助。你所有的问题都表明你真的没有听我的建议,并尝试实际阅读一本书。你为什么如此反对知识?你的私人导师也不是这样。这段代码有很多错误…从介绍v开始书的作者。别到处抄袭了。这让我发疯了。