Trouble with add function in a class

In this project I have a PhoneBook class that accepts Entries. Entry is another class that consists of strings or names and phone numbers and the accessors and mutators and whatnot. This program reads from a text file a bunch of names and numbers and is supposed to add those to the phone book. Right now my Entry class works fine but for whatever reason my add function in the PhoneBook class isn't working properly. Any help would be appreciated.

main
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
  #include <iostream>
#include <fstream>
#include "phonebook.h"

using namespace std;

int main()
{
    string firstName, lastName, fullName, phoneNumber;
    Entry entry;
    PhoneBook phonebook;

    ifstream infile("C:\\Users\\Gregory\\Desktop\\Names_Numbers.txt");
    if (!infile)
        cerr << "File unable to be opened";

    /*
    declare entry object, read in values from file, save values within entry object,
    add entry object to phonebook
    */

    if (infile.is_open()) {
        while(infile >>  firstName >> lastName >> phoneNumber){
            fullName = firstName + " " + lastName;
            entry.setName(fullName);
            entry.setPhoneNumber(phoneNumber);
            //entry.print();
            phonebook.add(entry);
        }
    }
    phonebook.print();

    return 0;
}

}


entry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef ENTRY_H
#define ENTRY_H

#include <string>

class Entry {
private:
    std::string name;
    std::string phoneNumber;
public:
    Entry(); //default constructor
    Entry(std::string n, std::string pN); //constructor
    void setName(std::string n);
    void setPhoneNumber(std::string n);
    std::string getName() const;
    std::string getPhoneNumber() const;
    void print();
};


#endif

entry.cpp
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
#include "entry.h"
#include <iostream>

using namespace std;

Entry::Entry(){
    name=" ";
    phoneNumber=" ";
}

Entry::Entry(string n, string pN) {
    name=n;
    phoneNumber=pN;
}

void Entry::setName(string n){
    name=n;
}

void Entry::setPhoneNumber(string pN){
    phoneNumber=pN;
}

string Entry::getName() const{
    return name;
}
string Entry::getPhoneNumber() const {
    return phoneNumber;
}

void Entry::print() {
    cout << name << " " << phoneNumber << endl;
}


phonebook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include "entry.h"
#include <string>


class PhoneBook {
private:
    static const int CAPACITY=100;
    Entry entry[CAPACITY];
    int count; //holds current number of entries
public:
    PhoneBook();
    void add(const Entry& newEntry); //add phone number and name to book
    std::string find(std::string pN); //given a phone number returns name belonging to it
    void print(); //print out entire phonebook
};

#endif 


phonebook.cpp
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
#include "phonebook.h"
#include <iostream>
#include <string>
using namespace std;

PhoneBook::PhoneBook() {
    count = 0;
}

void PhoneBook::add(const Entry& newEntry) {
    entry[count]=newEntry; //set current open entry spot to parameter
    count++;
}

string PhoneBook::find(string pN) {
    for (int i=0;i<count;i++) {
        if (entry[count].getPhoneNumber().compare(pN)==0)
            return entry[count].getName();
    }
    string error="Phone number not found in phone book.";
    return error;
}

void PhoneBook::print() {
    for (int i=0;i<count;i++)
        entry[count].print();
}


Last edited on
#include "Phonebook.h"

You should have a capital letter for Phonebook.h and Entry.h.. It is a standard naming practice.


Please post your header files.
Last edited on
Can you show us how entry is defined inside of PhoneBook? Looking at your code, the only possible way that could work safely is if you were using a std::map (which is silly to do anyway), and I have a suspicion you're not.
sorry, added the .h files
novellof wrote:
You should have a capital letter for Phonebook.h and Entry.h.. It is a standard naming practice.
Unlike Java, C++ does not have any such standard for naming conventions.
pepstein wrote:
sorry, added the .h files
OK, you're doing what I was afraid of, but it will at least work for a few nanoseconds. It's not what's causing your issue. Could you please elaborate on what you mean by "my add function in the PhoneBook class isn't working properly"? Does it send insulting emails to your professor? Does it cause a robot to step on kittens? Does it increase the likelihood of world war 3? You need to be specific.
Last edited on
I updated my phonebook.h and phonebook.cpp to fix the count stuff, didn't really affect how it functioned. If i add a phonebook.print() call once the file is fully read it just prints out a bunch of blank lines and completes. The strings are fine in the entry class, I can print those but once they are transferred to the PhoneBook class the PhoneBook class isn't receiving them correctly via the add function. Do i need to overload the assignment operator in the entry class?
Last edited on
The default copy constructor generated by your compiler is sufficient here. I am actually not sure what is wrong with your program, but it sounds like memory corruption. How many records are you reading from the file?
only 56, format is
1
2
John Doe 323-816-9613
Jane Doe 323-816-9613
Last edited on
Try printing the value of count inside your add function.
when adding "cout << count << endl;" after "count++" upon compilation it prints out the current count correctly line after line and then for what may be 56 lines there is black space. Keep in mind my main still has a phonebook.print() call. Like so:

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
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

























































Process returned 0 (0x0)   execution time : 0.100 s
Press any key to continue.
Try this:
10
11
12
13
14
15
16
void PhoneBook::add(const Entry& newEntry) {
    newEntry.print();
    entry[count]=newEntry; //set current open entry spot to parameter
    entry[count].print();
    count++;
    std::cout << count << std::endl << std::endl;
}
This is basically poor man' debugging - ideally you would use a real debugger. But I can't test your code myself and I don't know what IDE you use.
With the function like this:
1
2
3
4
5
6
void PhoneBook::add(const Entry& newEntry) {
    entry[count]=newEntry; //set current open entry spot to parameter
    entry[count].print();
    count++;
    cout << count << endl;
}


and deleting out print statement in main it now runs and prints the entries with count number after it, so getting a little closer. thanks for help thus far.

output
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
Aleta Remick 720-318-9049
1
Bernarda Dave 415-846-1688
2
Leisa Reavis 310-808-5243
3
Birdie Nottingham 323-888-6200
4
Marva Koppes 323-888-6237
5
Selma Cater 323-888-6209
6
Karyl Bahe 323-888-6202
7
Rosella Hoffer 323-707-8576
8
Marianela Kennon 323-216-2201
9
Eduardo Rough 323-960-3100
10
Heriberto Tutt 727-455-4538
11
Lida Vitale 727-214-5862
12
Kera Godinez 727-455-5955
13
Letisha Stutz 424-209-0017
14
Krystina Lockwood 305-242-4044
15
Phyllis Settle 305-245-1641
16
Jerry Oritz 818-481-3655
17
Remona Eisenmenger 909-259-5674
18
Mason Lujan 469-438-2843
19
Tona Bosarge 323-216-9079
20
Evette Pennel 323-953-3340
21
Katherine Border 916-588-8042
22
Brady Woodring 727-324-7922
23
Dagny Malinowski 310-800-3391
24
Jami Kilbourn 415-797-8909
25
Keva Dille 727-639-0277
26
Lora Gassert 323-386-5239
27
Miguel Cambareri 213-840-1971
28
Williams Rockhill 424-200-7177
29
Therese Ambrose 323-816-9831
30
Shelton Shadley 310-404-5928
31
Claudio Mees 323-492-7681
32
Kimi Geiser 323-833-8515
33
Samira Vanlandingham 323-386-5428
34
Carissa Gainey 323-244-3832
35
Darby Amor 310-808-8752
36
Genevive Mahabir 323-855-0394
37
Wallace Vinzant 214-900-6004
38
Classie Olszewski 727-687-3465
39
Camilla Peel 646-879-9137
40
Deadra Coelho 323-953-3396
41
Kacie Sumler 323-816-9613
42
Rene Feher 323-816-7010
43
Tawanna Showers 323-273-5174
44
Ressie Braxton 323-386-5700
45
Shameka Silberman 323-386-5699
46
Florencio Scheerer 818-859-4757
47
Tracey Swilley 727-452-5725
48
Dario Humble 323-608-1510
49
Elna Pinion 408-829-4846
50
Sanjuana Valasquez 323-997-1080
51
Janis Mcquiggan 323-855-0338
52
Tijuana Zeno 323-717-8673
53
Jamila Overby 323-419-6398
54
Rudolf Hannigan 323-595-0799
55
Zoraida Carrozza 323-240-2739
56

Process returned 0 (0x0)   execution time : 0.122 s
Press any key to continue.
Ok, so we both made the same mistake and missed something obvious.
24
25
26
27
void PhoneBook::print() {
    for (int i=0;i<count;i++)
        entry[count].print();
}
Look very closely. Where do you use i? More specifically, where don't you use it?
Last edited on
DUDE. i am such an idiot, i'm smacking myself. thanks for pointing that out. its always the most obvious dumb stuff.
Topic archived. No new replies allowed.