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.
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include "entry.h"
#include <string>
class PhoneBook {
private:
staticconstint 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
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.
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.
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?
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?
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:
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.