Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2008, 1:14 PM   #1
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Vectors

Hi y'all.

I learn by example and writing my own code, and I've been working on vectors recently. I'm going to post my code here, and if anyone could point out a more efficient way to do it, and more commands I could add so I may educate myself further, please do so. Thanks

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void main() {
	string end = ("no");
	string inser;
	string lst;
	int x = 0;
	vector<string> names;
	do {
		cout << "Enter as many names as you'd like." << endl;
		cin >> inser;
		names.push_back(inser);
		x++;
		cout << "Would you like to end?" << endl;
		cin >> end;
		cout << "Would you list to list the names?" << endl;
		cin >> lst;
		if (lst == ("yes")) {
			for(int i=0;i<x;i++) {
				cout << names[i] << endl;
			}
		}
	}
		while (end != ("yes"));
}

Much appreciated.
White-Hat` is offline   Reply With Quote
Old Apr 17th, 2008, 1:17 PM   #2
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Vectors

well you dont need to keep track of the size of the vector in your x variable. you can just use names.size().
mbd is offline   Reply With Quote
Old Apr 17th, 2008, 1:22 PM   #3
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

Ahh. So something like this? Seems to work fine.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void main() {
	string end = ("no");
	string inser;
	string lst;
	vector<string> names;
	do {
		cout << "Enter as many names as you'd like." << endl;
		cin >> inser;
		names.push_back(inser);
		cout << "Would you like to end?" << endl;
		cin >> end;
		cout << "Would you list to list the names?" << endl;
		cin >> lst;
		if (lst == ("yes")) {
			for(int i=0;i<names.size();i++) {
				cout << names[i] << endl;
			}
		}
	}
		while (end != ("yes"));
}
White-Hat` is offline   Reply With Quote
Old Apr 17th, 2008, 2:03 PM   #4
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 93
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Vectors

main is type int, you're a heretic if you do otherwise, etc.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Apr 17th, 2008, 2:26 PM   #5
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

Lol, my instructor always used void in her examples she gives us. Guess I picked up the bad habit.
White-Hat` is offline   Reply With Quote
Old Apr 17th, 2008, 2:27 PM   #6
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Vectors

you could also use an algorithm and an iterator to print the vector instead of the for loop. then your code would not depend on an random access container.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

void main() {
	string end = ("no");
	string inser;
	string lst;
	vector<string> names;
	do {
		cout << "Enter as many names as you'd like." << endl;
		cin >> inser;
		names.push_back(inser);
		cout << "Would you like to end?" << endl;
		cin >> end;
		cout << "Would you list to list the names?" << endl;
		cin >> lst;
		if (lst == ("yes")) {
			copy(names.begin(), names.end(), ostream_iterator< string >(cout, "\n");
		}
	}
		while (end != ("yes"));
}
mbd is offline   Reply With Quote
Old Apr 17th, 2008, 2:55 PM   #7
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

Okay, this is what I have now:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
	string end = ("no");
	string inser;
	string lst;
	vector<string> names;
	do {
		cout << "Enter as many names as you'd like; Enter stop when you're finished." << endl;
		cin >> inser;
		names.push_back(inser);
		if(inser == ("stop")) {
                names.pop_back();
		cout << "Would you like to list the names?" << endl;
		cin >> lst;
		cout << "Would you like to end?" << endl;
		cin >> end;
		}
		if (lst == ("yes")) {
			copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
		}
	}
		while (end != ("yes"));
}

The only thing I don't understand is where the iterator comes into play here.. If you could explain that for me it would be great. (edit: found a flaw in the stop loop, adding it as a name. included the pop_back command)
White-Hat` is offline   Reply With Quote
Old Apr 17th, 2008, 3:52 PM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Vectors

View a file as a container of strings, where the first item in the container is the first thing written to the file and the last item in the container is the last thing written. osteam_iterator is simply an adapter that, as far as the copy algorithm is concerned, makes an ostream look like a container.

Incidentally, for entry of a long list, a user will find it rather inconvenient to have to enter each element and then, for every element, having to give two responses. Users are inconvenient at times, but it is usually a good idea for a programmer not to inconvenience them.
grumpy is offline   Reply With Quote
Old Apr 17th, 2008, 4:24 PM   #9
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

@grumpy

Taking that into consideration, I rewrote it to be more hassle-free for the user.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
	string end = ("no");
	string inser;
	string x;
	vector<string> names;
	cout << "Enter as many names as you'd like; Enter stop when you're finished." << endl;
	do {
		cin >> inser;
		names.push_back(inser);
		if(inser == ("stop")) {
		names.pop_back();
		cout << "If you would like to list the names you have written, enter \"list\". Otherwise,  enter \"quit\" to end. To enter more names, enter \"more\"." << endl;
		cin >> x;
		}
		if (x == ("list")) {
			copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
		}
			else if (x == ("more")) {
				cout << "Okay, we'll enter more!";
				cout << endl;
			}
			else {
				if (x == ("quit")) {
					end = ("quit");
				}
			}
	}
		while (end != ("quit"));
		return 0;
}
White-Hat` is offline   Reply With Quote
Old Apr 18th, 2008, 3:19 PM   #10
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

I was entertaining the thought of re-writing the program and using classes. How could I add to the program without exiting my realm (being semi-new to the c++ game), while giving classes within the program actual meaning (i.e., a reason to use an OOP design versus a procedural syntax {I guess a larger program would be in order})?
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 3:33 AM.

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