nizbit 0 Junior Poster in Training

I wanted to say thanks for all the replies. Decrementing j instead, got my code to work. I like dusktreader's alternative. Originally I wanted to set the dupes to null but I did not want to overwrite any values.

Yes this was for homework. I desperately need to learn the STL. My school for a lot of reasons frowns on the STL. In fact they barely teach it. I understand some of their concerns, but at the same time the STL doesn't have cooties :D.

nizbit 0 Junior Poster in Training

I'm trying to find duplicates in an array and removing them by shifting the array index using the same array. I can't figure out where in my code the problem is but I'm still getting a duplicate. Could someone please help pinpoint the problem? I've tried debugging it but I'm not seeing it.

#include <iostream>
using namespace std;

int removeDuplicates(int A[], int n){
	int last = n-1;
	int found = 0;
	//bool flag = false;

	for(int i=0; i < last+1; i++)
	{
		for(int j=i+1; j < last+1; j++)
		{
			if(A[i] == A[j])
			{
				found++;
				for(int k = j;k < last+1;k++)
				{
					A[k]=A[k+1];
					//if(A[k-1] == A[i])
					//	flag = true;
				}
				last--;
				j=j+1;
			}

		}
}
	//found= last-found;
	return last;
}

int main() {

	// Test data
	int n = 24;
	int A[] = {1,1,2,4, 1,1,1,2, 3,5,8,0, 2,-2,-4,1, 9,10,-4,11, 11,8,5,-4};

	// Call
	int last = removeDuplicates(A,n);

	// Print Results
	for(int i = 0; i <= last; i++)
		cout << A[i] << " ";
	cout << endl;

	return 0;
}
nizbit 0 Junior Poster in Training

I know how to send email in PHP. I want to send email using a different email account.

nizbit 0 Junior Poster in Training

My PHP account is hosted by the university. I want to send email to my gmail account and not the account tied to my PHP account. Is this possible?

nizbit 0 Junior Poster in Training

Please anyone?

nizbit 0 Junior Poster in Training

I am currently using DDD. I get that error when I step into makeEmpty().

nizbit 0 Junior Poster in Training

If I understand correctly I changed the doe to:

void makeEmpty()
{
	if(a != NULL)
	{
		delete [] a;
		a = NULL;
	}
	a = new ItemType[1];
	pos = 1;
}

I still get the same error.

nizbit 0 Junior Poster in Training

I came to the same realization that I should just us pos as my length. I fixed my code and seems to be working:

void buildMaxHeap(int key)
{
	if (pos == 1)
	{
		a[0] = key;
		pos++;
	}
	else
	{	
		ItemType *temp = new ItemType[pos];
		for(int i=0; i<pos; i++)
		{
			temp[i] = a[i];
		}
	
		delete[] a;
		a = NULL; 
		temp[pos-1] = key;
		a = new ItemType[pos];
		for(int i=0; i<pos; i++)
		{
			a[i] = temp[i];
		}
		pos++;
		int len = pos - 1;
		int first = 0;
		while(first < len)
		{
		
			heapifyMax(a, 0, first);
			first++;
		}
	}
}

Now I have a new problem. When I'm done with the first set of values, I want to clear a to start reading in the new set of values, so I did:

void makeEmpty()
{
	delete [] a;
	a = NULL;
	a = new ItemType[1];
	pos = 1;
}

Which I get another error about a double free. When I looked that up, basically I'm trying to delete something that has already been unallocated.

nizbit 0 Junior Poster in Training

I've cleaned up my code a bit. It seems to fail when adding the 4th element.

void buildMinHeap(int key) 
{
	cout << "init before:"<< pos <<endl;	
	if (pos == 1)
	{
		a[0] = key;
		cout << "pos=1" << endl;
		pos++;
	}
	else
	{
		//len = pos+1;		
		ItemType *temp = new ItemType[pos];
		for(int i=0; i<pos; i++)
		{
			temp[i] = a[i];
		}
	
		delete[] a;
		a = NULL; 
		temp[pos-1] = key;
		cout << "after insert" << pos <<endl;
		a = new ItemType[pos];
		for(int i=0; i<pos; i++)
		{
			a[i] = temp[i];
			cout << a[i] << endl;
		}
		pos++;
		int first = 0;
		while(first < pos)
		{
		
			heapifyMax(a, 0, first);
			first++;
		}
	}
}
nizbit 0 Junior Poster in Training

I need some dire help. Whenever I execute my program I get this message "./main: free(): invalid next size (fast)". Which leads me to believe something with my dynamic allocation of an array. I've used a debugger and pinpointed the error to the method where I'm trying to increase the size of the array. I've been pouring over the code for hours and cant seem to find whats wrong. I was hoping someone could take a look and tell me whats wrong.

void buildMinHeap(int key) 
{
	
	if (len == 1) //array is already allocated one int
	{
		a[0] = key;
		pos++;
	}
	else
	{
		int len = pos+1; //pos is the length
		ItemType *temp = new ItemType[len+1]; //ItemType is int
		for(int i=0; i<len; i++)
		{
			temp[i] = a[i];
		}
	
		delete[] a;
		a = NULL; 
		len = length(temp);
		temp[len-1] = key;
		cout << len <<endl;
		a = new ItemType[len];
		for(int i=0; i<len-1; i++)
		{
			a[i] = temp[i];
		}
		pos++;
		int first = 0;
		while(first < pos)
		{
		
			heapifyMax(a, 0, first);
			first++;
		}
	}
}
nizbit 0 Junior Poster in Training
self.movement = {"run" : {"run1" : (45, 55, 110, 120)
                           "run2" : (45, 55, 110, 130)
                           "run3" : (45, 55, 110, 140)}}

I would like load the dictionary run from a file so I could just change the values in the file. These values will not change during the runtime of the program.

nizbit 0 Junior Poster in Training

I have a dictionary that I would like to load from a file into a class variable. The dictionary holds configuration data. I would have different files for different configurations. I was wondering if there is a way to do this? I found pickle but that saves an existing instance as a copy so it can be loaded later.

nizbit 0 Junior Poster in Training

Thanks very much for the help. Now I'm going to study your example.

nizbit 0 Junior Poster in Training

I need to be able to print at an offset where the second element of the second list matches up with the first element of the first list. For example have a's line up with the b's:

list = [[a, b, c, d],[a, X, X, b, c, d],[a, X, X, b, c, d],[a, b, c, d]]

output:
a b c d
  a X X b c d
        a X X b c d 
              a b c d

I'm not looking for the answer. I am looking for people's thoughts/suggestions on how to go about this. Any help would be awesome.

nizbit 0 Junior Poster in Training

Thats awesome!! That helps a lot. Thanks!!!!

nizbit 0 Junior Poster in Training

I'm trying to print horizontally not having any luck. I need to print
I#x where x one through the length of a list i.e.-I#1 I#2 I#3 I#4 etc. This is what I have which is syntactically wrong:

for x in range(1, len(depend)):
    print "%05s" ('I#'+str(x))

Could someone please point me in the right direction.

nizbit 0 Junior Poster in Training

I have to correct my above statement. The slicing in afterBNEZ does take into account if there is code after BNEZ. If not, The slicing will return an empty list.

nizbit 0 Junior Poster in Training

I should be more clear when I mention parsing. I'm not using any parser generators or writing a parser. I'm parsing in the most basic sense, i.e-pulling what I want based on conditionals.

This is extremely hacky code. This is what I have for the cases that do work with the 2 examples above only. I makes no assumption that there is code before the loop and no code after the loop. I stopped writing that check because I realized I need to check for nested loops and was not sure how to extract the nested loops individually.

op = [['Loop:', 'LD', 'R2', '0(R1)'], ['DADD', 'R4', 'R2', 'R3'], ['SD', '0(R1)', 'R4'], ['DADDI', 'R1', 'R1', '#-8'], ['BNEZ', 'R1', 'Loop'], ['DADD', 'R2', 'R2', 'R4']]

for item in op:
    if item[0] == 'BNEZ':
        place = op.index(item)
        afterBNEZ = op[place+1:]
        beforeBNEZ = op[0:place+1]
nizbit 0 Junior Poster in Training

I need to parse an assembly program. I have no problems parsing a group of instructions or a group of instructions with one loop. For example:

LD    R2,     0(R1)
          DADD  R4,     R2,     R3
          SD    0(R1),  R4
          DADDI R1,     R1,     #-8
          DADD  R2,     R2,     R4
or
          Loop: LD    R2,     0(R1)
                DADD  R4,     R2,     R3
	        SD    0(R1),  R4
	        DADDI R1,     R1,     #-8
	        BNEZ  R1,     Loop
	        DADD  R2,     R2,     R4

My problem is when there are nested loops. For example:

Loop: LD    R2,     0(R1)
      Loop: LD    R2,     0(R1)
            DADD  R4,     R2,     R3
	    SD    0(R1),  R4
	    DADDI R1,     R1,     #-8
	    BNEZ  R1,     Loop
	    DADD  R2,     R2,     R4
     DADD  R4,     R2,     R3
     SD    0(R1),  R4
     DADDI R1,     R1,     #-8
     BNEZ  R1,     Loop
     DADD  R2,     R2,     R4

I was hoping that someone could provide some insight on how to parse the nested loop example. I would like to split the nested loops into 2 separate lists, innerLoop and outerLoop. The only way I see to do this is to count the whitespace or indentation to break it up. If anyone has a better idea, please let me know. Thanks!

nizbit 0 Junior Poster in Training

This is a very basic question, but I'm very new to python. I want to read data in a file. The data is between two delimiters. I'm not exactly sure how I can extract the data and skip the delimiters. Any help would be great.

nizbit 0 Junior Poster in Training

.

nizbit 0 Junior Poster in Training

No, get will read in a newline character as a newline character. Not sure what you mean by "stops". If you use get to read in a char, it "stops" after every character.

ifstream ins;
char c;
ins.get (c);

I was a little confused on what get() could do. I thought it would stop reading characters at a newline.

Also, are you trying to save the spaces and newlines or you just want to ignore them? If you need to save them, you're right, don't use >>. If you want to ignore spaces and newlines, which it looks to me like you want to, >> is perfect.

You can read in a character at a time, rather than read in an entire string. The spaces are no longer an issue if you read from the file into a character rather than a string. Here you have defined the variable you are reading into a character array of length 1:

char input[1];
// code
infile.get(input, 1);

Just make input a char.

char input;
// code
infile.get(input);

I want to ignore spaces, but I don't know if the file will contain spaces. I cannot make any assumptions on how the input file is formatted.


But frankly I think you are making it more complicated than it needs to be. You mentioned that you want to store 1 - 9 as characters, as well as store the asterisk as a character, but you have board defined as an integer array, …

nizbit 0 Junior Poster in Training

This is what I came up with, but the output is all zeros

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
	//Testing for correct number of arguments at command line
	if(argc != 3)
	{
		cout << "ERROR--Not enough arguements!!" << endl;
		cout << "The correct format is: \"./name of program\" \"input file\" \"output file\"" << endl;
		//exit(1);  change at submission
	}
	
	//opening data file from user at run time
	ifstream infile;
    infile.open(argv[1]);
	
	if(infile.fail())
	{
		cout << "Failed to open input file." << endl;
		exit(1);
	}

	ofstream outfile;
    outfile.open(argv[2]);
	
	if(outfile.fail())
	{
		cout << "Failed to open output file." << endl;
		exit(1);
	}

	const int SIZE = 81;

	int board[SIZE];
	char input[1];
	
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			if (infile.peek()=='\n') 
			    infile.ignore(1,'\n');
			else if (infile.peek()==' ') 
			    infile.ignore(1,' ');
			infile.get(input, 1);
			board[i*9+j] = atoi(input);
		}
	}
	
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			cout << board[i*9+j];
		}
		cout << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}

input file

* 1 * 4 * * * * 2
* * 4 * 7 2 * * *
* 8 3 * * 5 * 7 *
* 3 8 2 * 7 * * *
* 5 2 1 * 4 8 3 *
* * * 5 * 3 7 2 *
* 2 * 3 * * 9 4 *
* * * 7 2 * 6 * *
9 * * * * 6 * 1 *
nizbit 0 Junior Poster in Training

I am having a little bit of a dilemma. I have an input file that can have a number 1 through 9 or a asterisk. I am treating 1-9 as a character. I don't know if there is white space or new lines. I can't use >> because I don't know if there is white space. I can't use get() because it will stop reading in characters if get encounters a new line. Could someone give me some suggestions?

nizbit 0 Junior Poster in Training

I don't believe there was any "dis-information" from "you'd ran it and it crashed" because before that statement I said my program compiled. Although, I should have been clearer about "multiple files". Once I figured I should have added my files to a project to run the debugger, the debugger was not working properly. It would state that the source code had been updated and needed to re-build. Then it would compile without errors. I would run the debugger and it would complain again that the source code was updated when I did nothing to the code at all. I just need to find a solution to my debugger issues to prevent this same situation on my next project.

nizbit 0 Junior Poster in Training
Compiler: Default compiler
Executing  g++.exe...
g++.exe "H:\mylab3\Olympics.cpp" -o "H:\mylab3\Olympics.exe"   -g3  -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" -g3 
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x1e1): In function `ZN8Olympics10FindEventRESsPNS_8ListNodeE':
H:/mylab3/Olympics.cpp:64: undefined reference to `Event::geteventname()'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x572): In function `ZN8Olympics13AddContestantESsP10Contestanti':
H:/mylab3/Olympics.cpp:101: undefined reference to `Event::AddContestant(Contestant*, int)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x875): In function `ZN8Olympics16DeleteContestantESsSsSs':
H:/mylab3/Olympics.cpp:125: undefined reference to `Event::DeleteContestant(std::string, std::string)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0xa15): In function `ZN8Olympics5PrintERSo':
H:/mylab3/Olympics.cpp:141: undefined reference to `Event::Print(std::ostream&)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0xcd2): In function `ZN8Olympics10PrintEventERSoSs':
H:/mylab3/Olympics.cpp:166: undefined reference to `Event::Print(std::ostream&)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0xfdd): In function `ZN8Olympics18PrintEventPositionERSoSsSsSs':
H:/mylab3/Olympics.cpp:192: undefined reference to `Event::PrintPosition(std::ostream&, std::string, std::string)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x1489): In function `ZN8Olympics15SwapContestantsESsSsSsSsSs':
H:/mylab3/Olympics.cpp:219: undefined reference to `Event::SwapContestants(std::string, std::string, std::string, std::string)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x1755): In function `ZN8Olympics15AddSortedEventRESsRPNS_8ListNodeE':
H:/mylab3/Olympics.cpp:234: undefined reference to `Event::Event()'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x17e9):H:/mylab3/Olympics.cpp:235: undefined reference to `Event::setEventName(std::string)'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x18c1):H:/mylab3/Olympics.cpp:242: undefined reference to `Event::geteventname()'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x1979):H:/mylab3/Olympics.cpp:244: undefined reference to `Event::Event()'
C:\DOCUME~1\admin\LOCALS~1\Temp/ccwXbaaa.o(.text+0x1a0d):H:/mylab3/Olympics.cpp:245: undefined reference to `Event::setEventName(std::string)'
C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

Execution terminated
nizbit 0 Junior Poster in Training

Tried it and got linker errors to methods the linker could not find.

nizbit 0 Junior Poster in Training

I thought I would get a linker error.

nizbit 0 Junior Poster in Training

I'll add cout statements. I know how to use a debugger, but for only a single file-not multiple files.

nizbit 0 Junior Poster in Training

I know its a lot but I don't know really where to start troubleshooting it. The seg fault does not give me any info where the error is.

nizbit 0 Junior Poster in Training

I need major help. My program will compile but when I run it I get a segmentation fault. I don't know how to run a debugger with multiple files. Could someone please find when this error occurs? My project is due in 2 hours.

nizbit 0 Junior Poster in Training

Well the Contestant class contains strings of first name, last name, age, and country. At run time the user passes a input file with various commands and the info that Contestant class is supposed to hold. So I have no idea how long these strings might be.

nizbit 0 Junior Poster in Training

Say I have a class named Contestant, could I declare an instance of the Contestant class as:

Contestant *c = new Contestant;
nizbit 0 Junior Poster in Training

Thanks, I got it working.

nizbit 0 Junior Poster in Training

It doesn't matter that I declared the Olympic class as a friend to the Event class?

nizbit 0 Junior Poster in Training

The program I am working on deals with nested linked lists. I have a Olympics class that uses a linked list for events. Events have their own class with a linked list of contestants of a contestant class for each event.

I don't understand why I'm getting these compile errors:

379: error: `ListNode' has not been declared
380: error: ISO C++ forbids declaration of `node' with no type
: In function `Event* FindEventR(std::string, int*)':
386: error: `item' has not been declared
386: error: request for member of non-aggregate type before '->' token
388: error: `item' has not been declared
388: error: request for member of non-aggregate type before ';' token
391: error: `next' has not been declared
391: error: request for member of non-aggregate type before ')' token
: In function `Event* FindEvent(std::string)':
396: error: `head' undeclared (first use this function)
396: error: (Each undeclared identifier is reported only once for each function it appears in.)
: At global scope:
399: error: `ListNode' has not been declared
400: error: ISO C++ forbids declaration of `node' with no type
: In function `Event* AddSortedEventR(std::string, int*&)':
401: error: `item' has not been declared
401: error: request for member of non-aggregate type before ')' token
403: error: `ListNode' undeclared (first use this function)
403: error: `newPtr' undeclared (first use this function)
405: error: `head' undeclared (first use this function)
409: error: `next' has not been declared
409: error: request for member of non-aggregate type before ')' token

Everything is declared. I …

nizbit 0 Junior Poster in Training

I have a class named Contestant which holds all the info on a person playing a game. Then I have a game class for all the different games a contestant could be playing. My linked list in the game class is defined by the contestant class. I need to swap two players positions recursively in my linked list. I am given the member prototype as:

SwapContestantsR(string first_name_one, string last_name_one, string first_name_two, string last_name_two, ListNode* &node, ListNode** contestant_one, ListNode** contestant_two)

I understand I have two base cases. One, if I find both contestants, then swap. Two, if node=NULL then I have reached the end of my linked list and no contestant by either name exists. Otherwise I recurse by moving to the next node. I complete lost with the implementation and any help would be great.

nizbit 0 Junior Poster in Training

Great, it worked! Thanks to everyone for the help!

nizbit 0 Junior Poster in Training

Ok, I cleaned up my code a bit from the previous post:

void game::DeleteContestantR(std::string first_name, std::string last_name, ListNode* &node)
//throw (ContestantException)
{
	/* See if we are at end of list. */
	if (node == NULL);
	//throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: insert index out of range");

	else if ((node->item->firstName == first_name) && (node->item->lastName == last_name))
	{
		ListNode *tempNode = node;
		node = node->next;
		delete tempNode;
	}
	else DeleteContestantR(first_name, last_name, node->next);
}

Now I get a compile error that firstName and lastName is private. Which they are private in my contestant class. I thought I am able to point to them because when I defined the ListItemType for my linked list, I used a pointer to the Contestant class:

typedef Contestant* ListItemType;
nizbit 0 Junior Poster in Training

Couldn't I compare by changing the statement to:

node->item->data member of the other class

nizbit 0 Junior Poster in Training

No character arrays. item is defined by typedef as an instance to another class. first_name and last_name are C++ strings.

nizbit 0 Junior Poster in Training

I have a recursive method that traverses a linked list for a first name or last name then deletes the node from the list. This is what I have:

void game::DeleteR(string first_name, string last_name, ListNode* &node)
//throw (ContestantException)
{
	/* See if we are at end of list. */
	if (node == NULL)
	//throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: insert index    out of range");  need to change this message

	else if (node->item == first_name) || (node->item == last_name))
	{


		/*
		* Check to see if current node is one
		* to be deleted.
		*/
		if ((node->item == first_name) || (node->item == last_name))
		{
			ListNode *tempNextP;

			/* Save the next pointer in the node. */
			tempNextP = node->next;

			/* Deallocate the node. */
			free(node);

			/*
			 * Return the NEW pointer to where we
			 * were called from.  I.e., the pointer
			 * the previous call will use to "skip
			 * over" the removed node.
			 */
			node == tempNextP;
		}
	}
	
	else DeleteR(first_name, last_name, node->next);
}

At line 16 I het a compile error " no match for 'operator==' in 'node->game::ListNode::item == first_name'". Can I not use a comparison operator?

nizbit 0 Junior Poster in Training

Great. Thanks for the help.

nizbit 0 Junior Poster in Training

updated #5

nizbit 0 Junior Poster in Training

Well class should not be in there. I thought I would have to make an instance so I could add a race car to the linked list.

nizbit 0 Junior Poster in Training

Sorry, I fixed the typo. I am working on an assignment and templates can't be used. Also I would like to add that the example is not my assignment.

nizbit 0 Junior Poster in Training

First I just wanted to say thanks for all the help everyone has provided for me before. I'm sure you wanted to reach through the internet and strangle me :) .

I having some difficulty with understanding/implementing a linked list class with another class. Just something as simple as interacting with each class has me puzzled. Say I have a class called race car. This class contains the make, number, team and driver of the race car. Then say there is a race track class. This class will define a linked list for the race car class. The member functions might do a bunch of things like add, swap, delete and print. I'm not concerned with the member functions. Would something like this work:

class racecar {

      private:
          string make, team, driver;
          int number;
.
.
.
.
};
class raceTrack {
typedef racecar* ListItemType;

      private:
          racecar make, team, number;
          struct ListNode
	   {
	      ListItemType item;
	      ListNode    *next;
	   }; 
.
.
.
};

This is just an example I thought of. Could someone help me by using this example or show a different example? Thanks.

nizbit 0 Junior Poster in Training

I'm being dense today, because I'm still not getting it.

nizbit 0 Junior Poster in Training

I've been banging my head on this errors and can't seem to find where I went wrong. For any of the "set_" errors, set_ (as well as print) refers to a member function of another class . Please, any help would be great.

test.cpp: In member function `void CDCollection::get(int, std::ofstream&)':
error: `print' is not a type
error: request for member of non-aggregate type before '(' token

test.cpp: In member function `void CDCollection::add(std::istream&)':
error: `set_artist' has not been declared

error: request for member of non-aggregate type before '(' token
error: `set_title' has not been declared
error: request for member of non-aggregate type before '(' token

error: `set_year' has not been declared
error: request for member of non-aggregate type before '(' token
error: `set_descrip' has not been declared
error: request for member of non-aggregate type before '(' token

Execution terminated

class:

class CDCollection
{
	public:
        CDCollection();
        void print(ofstream &output);
		void get(int num_cds, ofstream &out);
		void add(istream& in);
	
	
	
	private:
		int num_cds, cdTotal, max;
		char cdArray[100];
};

CDCollection::CDCollection()
{
	cdTotal = 0;
}

void CDCollection::get(int num_cds, ofstream &out)
{
	cdArray[num_cds].print(out);
}
void CDCollection::print(ofstream &out)
{
	out << "CD COLLECTION:" << endl;
	for (int num_cds = 0; num_cds < cdTotal; num_cds++)
	{	
		get(num_cds, out);
	}
}
void CDCollection::add(istream& in)
{
	max = 100;
	if (cdTotal < max)
	{
		SString artist;
		SString title;
		SString descrip;
		SString test;
		int year;
		char plus[10];
		
		in >> artist;
		in >> title;
		in >> year;
		in.getline(plus,10);
		in >> test;
		
		while(1)
		{
			
			if (test.str_compare("END\n") == 0)	
			{	
				break;
			}
			
			SString …
nizbit 0 Junior Poster in Training

I have a class named SString, which is a user defined c style string class. The constructor for SString dynamically allocates a char array. Then I have a CD class which contains data about a specific CD, like artist, title, year, etc. They are defined as SString instances. I want to allocate an array using SString's constructor for the different CD data. Can I make a call to SString's constructor? Or since it is an instance of the SString class, the default constructor was called? Or should I be implementing this in my get methods? I'm not exactly sure how to do this.


Here is the code:

SString::SString(char *array, int size)
    {
        int i;
	    _string = new char[size + 1]; // +1 for the string's null terminator character
	    for(i=0; i < size; i++)
	    {
            _string[i] = array[i];
	    }
        _string[i] = 0; // add string's null terminating character
    }
class CD
{
	private:
		SString artist, title, descrip;
		int year;
		
	public:
		CD();
		CD(SString artist, SString title, SString descrip, int year);
		SString get_artist();
		SString get_title();
		SString get_descrip();
		int get_year();
        void set_artist(char &newArtist);
        void set_title(char &newTitle);
        void set_descrip(char &descrip);
        void set_year(int newYear);
        void print(SString artist, SString title, SString descrip, int year);
		friend std::istream& operator >>(std::istream &is, CD &CD);
};