Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2008, 10:12 PM   #11
misho
Newbie
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0 misho is on a distinguished road
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.
misho is offline   Reply With Quote
Old Apr 19th, 2008, 10:19 PM   #12
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
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.
White-Hat` is offline   Reply With Quote
Old Apr 19th, 2008, 11:31 PM   #13
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

Alright, I have a question.

When using the...
copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
algorithm suggested to me, how can I apply this to my own class and member functions?

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"));
(I know that wouldn't work, just wanted to help pass a little more clarification along if something wasn't being understood.

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
White-Hat` is offline   Reply With Quote
Old Apr 20th, 2008, 4:50 AM   #14
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8 Ooble is on a distinguished road
Re: Vectors

Check this out.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 20th, 2008, 1:12 PM   #15
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
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.
White-Hat` is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC