Help Me With C++?

Hey guys, I have an assignment due tomorrow that I will have to do tonight because I have class all day tomorrow. If you saw my other blog, you know how stressed I am about this stuff. But I really want to get this stuff, so I don't just want someone to do it for me. If I even get half this thing working I'll be happy.

Here is the assignment: http://www.cs.uleth.ca/~bomhof/cs2620A/assn/assn1.txt

And all I have so far is a list of things I need to implement, no actual code.

What I think I need:

A function that creates the picture. I think I will need some vectors that accept strings in this function.

A Draw function that draws the picture.

a frameIt function that draws the frame. It depends on a function called isFramed that determines if there is a frame or not.

A unFrameIt function that draws no frame. It also depends on isFramed.

isFramed returns a boolean type and a frame character.

I think that is everything I need.

I don't want to dive in and do this all at once. I want to take baby steps. If I could get the vectors reading in strings and printing out strings, I'll be happy.

And here is what I think I would do to do that...

write a class called CharacterPicture, so..

class CharacterPicture {
public:
vector <string> iVec();
};

And then in my .cc file, have something like...

cout << "Enter some words: ";
cin << iVec();

Does any of that make sense? Am I at least thinking the right way? And I know the code is missing a lot, its just a snipet of what I know that I would have to write

Comments

Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class CharacterPicture
{
	private&#58;
		// Character used when printing the frame
		string frameCharacter;
		// Vector used to store all strings in the object
		vector <string> pictureStrings;
		
		// Returns the longest string contained in the object
		// *** Not required by spec, but makes life a lot easier during
		//	 framing and concatenation by determining how far to frame or pad
		int longestString&#40;&#41;;
	public&#58;
		// Create a new CharacterPicture object
		CharacterPicture&#40;&#41;;
		// Create a new CharacterPicture object as a copy of another
		CharacterPicture&#40;CharacterPicture& pic&#41;;
		
		// Returns the number of discrete strings in this object
		int getSize&#40;&#41;;
		// Returns a string from this object by index
		string getString&#40;int index&#41;;
		// Returns the character used for the frame
		string getFrame&#40;&#41;;
		
		// Change the frame character used in output, the default is '*'
		void setFrame&#40;string frame&#41;;
		// Add a new string to the object
		// *** Not required by spec, but simplifies adding with the other two
		//	 methods. Also has the side effect of making programmatic
		//	 additions a bit more elegant.
		void addString&#40;string str&#41;;
		// Add a new string to the object by console input
		void inputString&#40;&#41;;
		// Load strings from a file
		void loadFile&#40;string filename&#41;;
		
		// Concatenate another CharacterPicture object horizontally
		void concatHorizontal&#40;CharacterPicture pic&#41;;
		// Concatenate another CharacterPicture object vertically
		void concatVertical&#40;CharacterPicture pic&#41;;
		
		// Print the character picture to stdout, takes an optional boolean to
		// determine if the frame should be used
		void output&#40;bool frame = false&#41;;
		// Print the character picture to the filename specified, takes an
		// optional boolean to determine if the frame should be used
		void output&#40;string filename, bool frame = false&#41;;
};

int CharacterPicture&#58;&#58;longestString&#40;&#41;
{
	int maxlen = 0;
	for&#40;int i = 0; i < getSize&#40;&#41;; i++&#41;
	{
		if&#40;pictureStrings&#91;i&#93;.length&#40;&#41; > maxlen&#41;
		{
			maxlen = pictureStrings&#91;i&#93;.length&#40;&#41;;
		}
	}
	return maxlen;
}

CharacterPicture&#58;&#58;CharacterPicture&#40;&#41;
{
	setFrame&#40;&#34;*&#34;&#41;;
}

CharacterPicture&#58;&#58;CharacterPicture&#40;CharacterPicture& pic&#41;
{
	for&#40;int i = 0; i < pic.getSize&#40;&#41;; i++&#41;
	{
		addString&#40;pic.getString&#40;i&#41;&#41;;
	}
	setFrame&#40;pic.getFrame&#40;&#41;&#41;;
}

int CharacterPicture&#58;&#58;getSize&#40;&#41;
{
	return pictureStrings.size&#40;&#41;;
}

string CharacterPicture&#58;&#58;getString&#40;int index&#41;
{
	if&#40;index >= 0 && index < getSize&#40;&#41;&#41;
	{
		return pictureStrings&#91;index&#93;;
	}
	else
	{
		return string&#40;&#34;&#34;&#41;;
	}
}

string CharacterPicture&#58;&#58;getFrame&#40;&#41;
{
	return frameCharacter;
}

void CharacterPicture&#58;&#58;setFrame&#40;string frame&#41;
{
	frameCharacter = frame;
}

void CharacterPicture&#58;&#58;addString&#40;string str&#41;
{
	pictureStrings.push_back&#40;str&#41;;
}

void CharacterPicture&#58;&#58;inputString&#40;&#41;
{
	char str&#91;256&#93;;
	cin.getline&#40;str, 256&#41;;
	addString&#40;string&#40;str&#41;&#41;;
}

void CharacterPicture&#58;&#58;loadFile&#40;string filename&#41;
{
	string line;
	ifstream stringfile&#40;filename.c_str&#40;&#41;&#41;;
	if&#40;stringfile.is_open&#40;&#41;&#41;
	{
		 while&#40;!stringfile.eof&#40;&#41;&#41;
		 {
			   getline&#40;stringfile, line&#41;;
			   addString&#40;line&#41;;
		 }
		 stringfile.close&#40;&#41;;
	}
}

void CharacterPicture&#58;&#58;concatHorizontal&#40;CharacterPicture pic&#41;
{
	int length = longestString&#40;&#41; + 1;
	for&#40;int i = 0; i < pic.getSize&#40;&#41;; i++&#41;
	{
		if&#40;i < getSize&#40;&#41;&#41;
		{
			string str;
			str = pictureStrings&#91;i&#93;;
			str.resize&#40;length, ' '&#41;;
			str += pic.getString&#40;i&#41;;
			pictureStrings&#91;i&#93; = str;
		}
		else
		{
			string str;
			str.resize&#40;length, ' '&#41;;
			pictureStrings.push_back&#40;str + pic.getString&#40;i&#41;&#41;;
		}
	}
}

void CharacterPicture&#58;&#58;concatVertical&#40;CharacterPicture pic&#41;
{
	for&#40;int i = 0; i < pic.getSize&#40;&#41;; i++&#41;
	{
		pictureStrings.push_back&#40;pic.getString&#40;i&#41;&#41;;
	}
}

void CharacterPicture&#58;&#58;output&#40;bool frame&#41;
{
	if&#40;frame&#41;
	{
		for&#40;int i = 0; i < &#40;longestString&#40;&#41; + 4&#41;; i++&#41;
		{
			cout << frameCharacter;
		}
		cout << endl;
	}
	for&#40;int i = 0; i < getSize&#40;&#41;; i++&#41;
	{
		if&#40;frame&#41;
		{
			cout << frameCharacter << &#34; &#34;;
		}
		cout << pictureStrings&#91;i&#93;;
		if&#40;frame&#41;
		{
			for&#40;int j = 0; j < &#40;longestString&#40;&#41; - pictureStrings&#91;i&#93;.length&#40;&#41;&#41;; j++&#41;
			{
				cout << &#34; &#34;;
			}
			cout << &#34; &#34; << frameCharacter;
		}
		cout << endl;
	}
	if&#40;frame&#41;
	{
		for&#40;int i = 0; i < &#40;longestString&#40;&#41; + 4&#41;; i++&#41;
		{
			cout << frameCharacter;
		}
		cout << endl;
	}
}

void CharacterPicture&#58;&#58;output&#40;string filename, bool frame&#41;
{
	streambuf* sbuf = cout.rdbuf&#40;&#41;;
	ofstream file;
	file.open&#40;filename.c_str&#40;&#41;&#41;;
	// Redirect cout to a file
	cout.rdbuf&#40;file.rdbuf&#40;&#41;&#41;;
	output&#40;frame&#41;;
	// Restore cout to how it was before
	cout.rdbuf&#40;sbuf&#41;;
}

int main&#40;int argc, char *argv&#91;&#93;&#41;
{
	// This object is initialized by hard-coded strings
	// It uses the default of no frame
	CharacterPicture Pic1;
	Pic1.addString&#40;&#34;Twinkle twinkle&#34;&#41;;
	Pic1.addString&#40;&#34;Little star&#34;&#41;;
	Pic1.addString&#40;&#34;How I wonder&#34;&#41;;
	Pic1.addString&#40;&#34;What you are&#34;&#41;;

	// This object gets its content from 5 user-provided strings
	// It uses a frame, but does not change it from the default of '*'
	CharacterPicture Pic2;
	for&#40;int i = 0; i < 5; i++&#41;
	{
		cout << &#34;Input string &#34; << i + 1 << &#34; of 5&#34; << endl;
		Pic2.inputString&#40;&#41;;
	}
	
	// This object loads from 'pic3.txt'
	// It uses a frame of '@'
	CharacterPicture Pic3;
	Pic3.loadFile&#40;&#34;pic3.txt&#34;&#41;;
	Pic3.setFrame&#40;&#34;@&#34;&#41;;

	// This object is a duplicate of Pic1, then horizontally concatenated
	// with Pic2 and displayed with no frame
	CharacterPicture Pic4&#40;Pic1&#41;;
	Pic4.concatHorizontal&#40;Pic2&#41;;
	
	// This object is a vertical concatenation of Pic2 and Pic1
	// It uses a frame of '-', and writes it to the file 'pic5.txt'
	CharacterPicture Pic5&#40;Pic2&#41;;
	Pic5.concatVertical&#40;Pic1&#41;;
	Pic5.setFrame&#40;&#34;-&#34;&#41;;

	cout << endl;
	Pic1.output&#40;&#41;;
	cout << endl;
	Pic2.output&#40;true&#41;;
	cout << endl;
	Pic3.output&#40;true&#41;;
	cout << endl;
	Pic4.output&#40;&#41;;
	cout << endl << &#34;Writing pic5.txt&#34; << endl;
	Pic5.output&#40;&#34;pic5.txt&#34;, true&#41;;
	cout << endl;
	
	system&#40;&#34;PAUSE&#34;&#41;;
	return EXIT_SUCCESS;
}

Oh and the pic3.txt I used was
Code:
What is love
Baby don't hurt me
Don't hurt me
No more

Edit: Noticed random unused variable at the top of main()
Edit again: Forgot to change back a bool when I was testing stuff
 
:O nice work chuckstudios! Now can science understand all of that? id recommend just debugging through all of it
 
What the hell Chuck, he said he wanted someone to help him understand the concepts and the gist of it, not write the whole thing and rub it in his face :angry:
 
[quote name='Psyfira' post='1768031' date='Feb 11 2009, 02:55 PM']What the hell Chuck, he said he wanted someone to help him understand the concepts and the gist of it, not write the whole thing and rub it in his face :angry:[/quote]

I suck at explaining programming things.
 
[quote name='Psyfira' post='1768031' date='Feb 11 2009, 12:55 PM']What the hell Chuck, he said he wanted someone to help him understand the concepts and the gist of it, not write the whole thing and rub it in his face :angry:[/quote]

Yeah thats what I wanted, but I do want to learn this stuff, so I'm not just going to hand in his code. I'm going to use it to ask my own questions like "How would I do this?" and then check the answers. Kinda like a study guide. It should help a bit. Thanks a lot chuck
 
Heh, some people just can't resist solving every small programming problem they come across. :P I've saved the assignment text to my computer for the next time I really want to program but have nothing to program (which happens quite often.)

science: If you have any more C++ questions, I'll be happy to help. :) Not sure if you still need help with this.
 

Blog entry information

Author
science
Views
259
Comments
14
Last update

More entries in Personal Blogs

More entries from science

  • More Poems
    I've been feeling inspired... I'm not sure what it is. But I've been...
  • Warm Pizza
    Tonight, I was in Kensington picking up pizza for a poker game at my...
  • A poem I wrote
    I don't write poetry, ever. But this just came to me, and I felt like I...
  • I bought a PS3
    So I bought my friend's PStriple off of him. It has a broken blu-ray...
  • I am too old for this, but...
    Yeargh random rant about girls thread. Long, long story short, my long...
General chit-chat
Help Users
  • AncientBoi
  • BakerMan
    I rather enjoy a life of taking it easy. I haven't reached that life yet though.
    AncientBoi @ AncientBoi: y