使用数组和指针的动态内存分配无法打印

Dynamic memory allocation with array and pointers fail to print

本文关键字:分配 内存 打印 动态 数组 指针      更新时间:2023-10-16

我尝试使用带有指针的动态内存分配来打印字符数组。如果我评论一个 pf,变量可以工作,但是当我尝试打印两个程序时,程序停止。

这是主要的CPP。

#include <iostream>
#include "ContactInfo.h"
using namespace std;
int main() {
int n = 0;
char *ph, *nm;
ContactInfo *allcontacts;
cout << "How many people you want to add to phone book? ";
cin >> n;
allcontacts = new ContactInfo[n];
for (int i=0; i < n; i++) {
    cout << i + 1 << ") Name: ";
    cin >> nm;
    allcontacts[i].setName(nm);
    cout << i + 1 << ") Phone: ";
    cin >> ph;
    allcontacts[i].setPhone(ph);
}
cout << setw(8) <<"Name" << setw(8) << "Phonen";
cout << "------------------------------------------------------n";
for (int i=0; i < n; i++){
   allcontacts[i].display();
}
return 0;
}

.CPP

#include "ContactInfo.h"
void ContactInfo::setName(char *n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}
void ContactInfo::setPhone(char *p) {
phone = new char[strlen(p) + 1];
strcpy(phone, p);
}
ContactInfo::ContactInfo() {
// setName("");
// setPhone("");
}
ContactInfo::ContactInfo(char *n, char *p) {
setName(n);
setPhone(p);
}
ContactInfo::~ContactInfo() {
delete [] name;
delete [] phone;
name = nullptr;
phone = nullptr;
}
const char *ContactInfo::getName() const {
return name;
}
void ContactInfo::display() const {
cout << getName();
cout << getPhoneNumber();
cout << endl;
}
const char *ContactInfo::getPhoneNumber() const {
return phone;
}

页眉

 #include <iostream>
 #include <cstring> // Needed for strlen and strcpy
 using namespace std;
// ContactInfo class declaration.
class ContactInfo
{
private:
char *name; // The contact's name
char *phone; // The contact's phone number
public:
    ContactInfo(char *, char *);
    ContactInfo();
    void setName(char *);
    void setPhone(char *);
    ~ContactInfo();
    const char *getName() const;
    const char *getPhoneNumber() const;
    void display() const;
   };

输出

您要向电话簿添加多少人?2

1(名称:万维网

1( 电话: 22

进程已完成,退出代码为 0

您将 C 和 C++ 代码混合在一起。您必须使用 std::string 而不是 char* 输入。尝试以下代码:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
class ContactInfo {
private:
    std::string name;
    std::string phone;
public:
    ContactInfo() {
    }
    ContactInfo(const std::string &Name, const std::string &Phone) {
        name = Name;
        phone = Phone;
    }
    void display() const {
        std::cout << name << " " << phone << std::endl;
    }
};
int main() {
    int n = 0;
    std::cout << "How many people you want to add to phone book? ";
    std::cin >> n; std::cin.get();
    std::vector<ContactInfo> all_contacts;
    for (int i = 0; i < n; i++) {
        std::string name, phone;
        std::cout << i + 1 << ") Name: ";
        std::cin >> name;
        std::cout << i + 1 << ") Phone: ";
        std::cin >> phone;
        all_contacts.push_back(ContactInfo(name, phone));
    }
    std::cout << std::setw(8) << "Name" << std::setw(8) << "Phone" << std::endl;
    std::cout << "------------------------------------------------------" << std::endl;
    for (auto i : all_contacts) {
        i.display();
    }
    return 0;
}