![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
Re: Vectors
You probably know this already, but vector and string are classes, so you are using classes, technically. You could make an address book program. Make a class that stores names, phone numbers, addresses, whatever, and has a few methods (maybe checking if a phone number is valid or outputting the information to a string). Then you can have a vector of these classes. Although, it may be better to have a vector of pointers to such classes.
|
|
|
|
|
|
#12 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Re: Vectors
Oh I know. I guess specifically a reason to create my own and use an OOP design within the program versus what I'm doing with it currently. Nonetheless, that's a great idea; just the thing to keep me busy for a while.
|
|
|
|
|
|
#13 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Re: Vectors
Alright, I have a question.
When using the... copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n")); I have a class called CAddressBook and I am using a member function named print(), which prints names, addresses, etc. My first thoughts were to write a for loop incrementing an integer until all the objects were printed, but I want to break this habit. EXAMPLE: copy(names.begin(), names.end(), ostream_iterator<vector/*or CAddressBook?*/>(print(), "\n")); Any help is appreciated. Edit: This is what I have so far.. going to sleep for the night. If anyone has any addition comments, they're welcome. #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class CAddressBook {
private:
string name;
string address;
string pn; //phone number
string on; //other notes
public:
CAddressBook(string, string, string, string);
~CAddressBook();
void CABoutput(void);
};
CAddressBook(string a, string b, string c, string d) {
name = a;
address = b;
pn = c;
on = d;
}
~CAddressBook() {
cout << "Object destroyed";
}
void CABoutput(void) {
cout << "Name: " << name << "\nAddress: " << address << "\nPhone Number: " << pn << "\nOther Notes: " << on;
cout << endl << endl;
}
int main() {
size_t size = 100;
vector<CAddressBook*> a_b(size);
string end;
int choose;
int i = 0;
cout << "Welcome to your address book!\n";
while(end != ("quit")) {
string name = "Jon Doe";
string address = "None entered";
string pn = "None entered";
string on = "None entered";
cout << "Would you like to...>\n1.) Enter a new entry\n2.) List current entries\n3.) Quit\n" <<
"Please enter your choise by number.\n";
cin >> choose;
if (choose == 1) {
cout << "Enter name: ";
cin >> name;
cout << "\nEnter address: ";
cin >> address;
cout << "\nEnter phone number: ";
cin >> pn;
cout << "\nEnter other notes: ";
cin >> on;
cout << endl;
a_b[i] = new CAddressBook(name, address, pn, on);
i++;
}
else if (choose == 2) { //this is where I want to include the algorithm |
|
|
|
|
|
#14 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8
![]() |
Re: Vectors
|
|
|
|
|
|
#15 |
|
Programmer
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0
![]() |
Re: Vectors
That seems to be just what I need. Although I've never used queue's, so trying to view it in terms of how I would use it for vectors is giving me some difficulty. I'll update when I get it figured out.
Edit: Alright, first attempt. Getting several errors, some syntax but generally it's the iterator errors I'm concerned about. Any help or pointers if I'm making obvious mistakes would be helpful. #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class CAddressBook {
private:
string name;
string address;
string pn; //phone number
string on; //other notes
public:
CAddressBook();
CAddressBook(string, string, string, string);
~CAddressBook();
void CABoutput(void);
};
CAddressBook::CAddressBook() {
name = " ";
address = " ";
pn = " ";
on = " ";
}
CAddressBook::CAddressBook(string a, string b, string c, string d) {
name = a;
address = b;
pn = c;
on = d;
}
CAddressBook::~CAddressBook() {
cout << "Object destroyed";
}
void CAddressBook::CABoutput(void) {
cout << "Name: " << name << "\nAddress: " << address << "\nPhone Number: " << pn << "\nOther Notes: " << on;
cout << endl << endl;
}
int main() {
size_t size = 100;
vector<CAddressBook*> a_b(size);
string end;
int choose;
int i = 0;
cout << "Welcome to your address book!\n";
while(end != ("quit")) {
string name = "Jon Doe";
string address = "None entered";
string pn = "None entered";
string on = "None entered";
cout << "Would you like to...>\n1.) Enter a new entry\n2.) List current entries\n3.) Quit\n" <<
"Please enter your choise by number.\n";
cin >> choose;
if (choose == 1) {
cout << "Enter name: ";
cin >> name;
cout << "\nEnter address: ";
cin >> address;
cout << "\nEnter phone number: ";
cin >> pn;
cout << "\nEnter other notes: ";
cin >> on;
cout << endl;
a_b[i] = new CAddressBook(name, address, pn, on);
i++;
}
else if (choose == 2) {
for (vector<string>::iterator it = a_b.begin(); it != a_b.end(); it++) {
a_b(it)->print();
cout << endl;
}
}
else (choose == 3) {
end = ("quit");
}
}
return 0;
}Last edited by White-Hat`; Apr 20th, 2008 at 1:40 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| reading from file, and vectors question | jasonfrost | C++ | 3 | Oct 3rd, 2007 9:02 AM |
| Arrays or Vectors? | can342man | C++ | 2 | Apr 20th, 2006 3:57 PM |
| Vectors and Classes | Writlaus | C++ | 11 | Apr 13th, 2006 5:25 AM |
| Passing vectors as arguments | Soulstorm | C++ | 6 | Mar 18th, 2006 4:49 PM |
| Drawing using vectors | HeX | Java | 12 | May 7th, 2005 10:25 AM |