address book

How can i make it so that a user can create as many new entries as they want, then delete any that they don't need anymore. I know this has to do with construtors and deconstructors, but i don't know how to get them to work. here's my code as of now.
thanks in advance
-ASCII14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
//  main.cpp
//  address book
//
//  Copyright 2014 jesse smith. All rights reserved.


#include <string>
#include <iostream>
using namespace std;

int c;
int total_a = 0;
string user_c;

class addr
{
public:
    addr()
    {
        
    }
    
    addr(string first_n, string last_n, double p_numb, char sex)
    {
        f_name = first_n;
        l_name = last_n;
        phone = p_numb;
        gender = sex;
    }
    ~addr()
    {
        delete f_name;
        delete l_name;
        delete phone;
        delete gender;
    }
private:
    string f_name;
    string l_name;
    double phone;
    char gender;
    
    
};
  

string intro(char first);
string intro(char first)
{
    if (first == 'f')
    {
        cout<<"create a new address"<<endl;
        cout<<"browse the addresses"<<endl;
        cout<<"delete an address"<<endl;
        cout<<"exit the program"<<endl;
    }
    if (first == 'n')
    {
        cout<<"create"<<endl;
        cout<<"browse"<<endl;
        cout<<"delete"<<endl;
        cout<<"exit"<<endl;    
    }
    cin >> user_c;
    return user_c;    
    
}

void decoder(string command);
void decoder(string command)
{
    if (command == "create")c = 1;
    else if (command == "browse")c = 2;
    else if (command == "delete")c = 3;
    else if (command == "exit")c = 4;
}

void pr_create();
void pr_create()
{
    total_a += 1;
    
}

void pr_browse();
void pr_browse()
{
    
}

void pr_delete();
void pr_delete()
{
    if (total_a == 0)
    {
        cout<<"you don't have any addresses to delete!" <<endl;
    }
    total_a -= 1;
}

int execute(int c);
int execute(int c)
{
    switch (c)
    {
        case 1:
            pr_create();
            return 1;
            break;
        case 2:
            pr_browse();
            return 1;
            break;
        case 3:
            pr_delete();
            return 1;
            break;
        case 4:
            return 0;
            break;
        default:
            return 0;
            break;
    }
}

int main ()
{
    bool pr_run = true;
    while (pr_run == true)
    {
        if (total_a == 0)
        {
            cout<<"you have no current addresses"<<endl;
            intro('f');
        }
        else
        {
            intro('n');
        }
        decoder(user_c);
        execute(c);
        if (execute(c) == 0){
            cout << "closing..." <<endl;
            pr_run = false;
        }
    }    
    return 0;
}
Sounds like your basic CreateReadUptadeDelete app.

There are 3 solutions that come to mind.

1. You can use a .txt file to mimic a Database table. You could create a new file for each user and store there addresses. This would allow them to build up their address book over time.

2. You could use dynamic memory to allocate a new Address struct or class on the fly. Since the addresses would be stored in memory, they must start from scratch every time they run the program.

3. You could preallocate a set amount of address objects and skip the whole dynamic allocation. This has the same weakness as #2 but also adds a memory overhead and potential limit on how many addresses they can store. (you could always try to allocate the max amount that the users hardware will allow to cancel out the limit but now your memory overhead is through the roof)


It looks like you already have your address class but you might want to correct some errors before you decide an approach to take.

- In that classes current state, there is no need for a destructor. If you decide approach #2 then lines 31- 37 will me important.

- You can remove the function declarations because you are defining your functions before you main(). You only need to declare your functions when your main() is before there implementation.

thanks, how would i go about doing #1?
Topic archived. No new replies allowed.