-
Small Class Example
Does anyone know why this will not display the name to the console? If I call A.getName(); in the addContact function itself it will retrieve the name correctly, however I believe it deletes itself after the function addContact ends. Can anyone spot where I went wrong?
Code:
#include <iostream>
using namespace std;
class Phonebook {
private:
string Name;
public:
void menu();
void setName(string);
string getName(){return Name;}
void addContact(Phonebook);
void printContacts(Phonebook);
};
void Phonebook::menu(){
cout << "Phonebook Menu" << endl;
cout << "(1) Add Contact" << endl;
cout << "Choose an option: ";
}
void Phonebook::setName(string s){
Name = s;
}
void Phonebook::addContact(Phonebook A){
string s;
cout << "\n\n[Add Contact]" << endl;
cout << "Name: ";
cin >> s;
A.setName(s);
}
void Phonebook::printContacts(Phonebook A){
cout << "\n\n[Contact List]" << endl;
cout << A.getName() << endl;
}
int main(){
Phonebook A;
int option = 0;
A.menu();
cin >> option;
switch(option){
case 1:
A.addContact(A);
A.printContacts(A);
break;
}
return 0;
}
Edit: I was able to make it print the name correctly, I still have a question though. How can I make it store more than one name?
New Code:
Code:
#include <iostream>
using namespace std;
class Phonebook {
private:
string Name;
public:
void menu();
void setName(string);
string getName(){return Name;}
void addContact(Phonebook *);
void printContacts(Phonebook);
};
void Phonebook::menu(){
cout << "Phonebook Menu" << endl;
cout << "(1) Add Contact" << endl;
cout << "Choose an option: ";
}
void Phonebook::setName(string s){
Name = s;
}
void Phonebook::addContact(Phonebook * A){
string s;
cout << "\n\n[Add Contact]" << endl;
cout << "Name: ";
cin >> s;
A->setName(s);
}
void Phonebook::printContacts(Phonebook A){
cout << "\n\n[Contact List]" << endl;
cout << A.getName() << endl;
}
int main(){
Phonebook A;
int option = 0;
A.menu();
cin >> option;
switch(option){
case 1:
A.addContact(&A);
A.printContacts(A);
break;
}
return 0;
}
-
this is enough for one string
This is enough for ten strings
Code:
string arrayNames[10];
-
When I try to convert the
to
I get the following error
Line: 11
Code:
error: could not convert '(std::string*)(&((Phonebook*)this)->Phonebook::Name)' from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'
-
This will work (general example)
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string Name[10];
Name[0] = "Samaras";
cout<<Name[0]<<endl;
return 0;
}
Where do you get the error?
I am guessing at a line of code that you treat Name as it was before (just a string), like you do here
Code:
void Phonebook::setName(string s){
Name = s;
}
You should treat Name as an array, because this is what Name is. An array of strings .
-
Every time you make a call to an array you have to include a [] with the number of the member you want - 1, starting with 0. For example, if I wanted the first member of name[10] I would have to write name[0], if I want the second it is name[1], name[2] for the third, and so on up to name[9].
edit: ninja'd
-
You should not be passing in a Phonebook object (or pointer to one) in your member functions. Instead of:
Code:
switch(option){
case 1:
A.addContact(A);
A.printContacts(A);
break;
}
It should be:
Code:
switch(option){
case 1:
A.addContact();
A.printContacts();
break;
}
And the individual member functions should be changed to accommodate this revision.
From a design standpoint, a Phone Book contains data on individuals (name, phone number, address, etc...). This would suggest that you have a "person" object which stores the name, phone number... and then your Phonebook class stores an array (or more usefully a std::vector) of such person objects:
Code:
class Person
{
std::string firstname;
std::string lastname;
std::string phone_number;
...
};
class Phonebook
{
std::vector<Person> entries;
...
};