Click to See Complete Forum and Search --> : Completely Stuck !


Godhatesme
May 4th, 2003, 05:55 AM
Hi all - a few details first...

I have no background in coding of any sort.

I did a 1 week course in C++ some 5 weeks ago and have a course assignment to do.

I started said coursework 4 weeks ago but have had family illness and work issues to deal with since.

I have to submit the coursework next Monday (May 12).

I am NOT asking anyone to do it for me !!

I've started the programming bit and have basically become stuck at what I have to do next. The approach I have taken is to break the programme down into chunks (so I can follow it) rather than write the programme in one go and then try to debug it.

Problem is - as I haven't looked at it for 4 weeks, I can't remember how I got to this point and really just need a slight nudge to get started again.

I've attached a .cpp file to show how far I have reached - I need to now add Mystring s2(25) which defines an empty string of 25 chars. I need to provide an appropriate contructor and ensure the first character in the string is null ('\0' - is that right?). There must be space for 26 characters to include the terminating null charatcer. 'len' needs to be set to 25 and 'size' set to 0.

As I said, I'm NOT looking for anyone to do this for me - I have looked at this for the past 36 hours and seem to be going nowhere! I do have a number of books with me - Smiley's 'Learn to Program C++' seems quite good but doesn't really help me right now.

I'm sure if I can just get my head into it again, I'll be sort of OK and will make some progress.

Thank you if you can help.

Tony

hometown
May 4th, 2003, 06:59 AM
Okay, if you DONT want to be helped, why do you post your problem here ?:)

Why dont you use string class instead of such an array of characters ? String class works better... You can add string S2 to string S1 efficiently...

Regards,
Nina

Godhatesme
May 4th, 2003, 08:09 AM
Hi Nina

I DO require some help - someone else posted a similar message on here and got blasted for asking for others to do their coursework. I was after suggestions rather than "Here is what you do..."

I've actually got to add up to s6 for Mystring and the way you suggest sounds a better way of doing things.

Only problem is...what does String class look like?!

The chap who 'taught' the week I did assumed we had prior knowledge of C (which I wasn't told until Day 1 at 9am !) - I can just about write HTML, but C passes me by !

I think I just about follow now what I have written so far - when I copied the text between Mystring::Mystring and the start of the destructor and changed the numbers, all I get is error messages.

I'm working on MS Visual 6.0, SP5 compiler by the way.

Thanks

Tony

Godhatesme
May 4th, 2003, 09:47 AM
My confidence is slowly draining away...

I have attached the full program that the assignment is based on but to run this and then try to debug it will take forever and won't garner the results I need to submit.

If this fails, I have little option left but to withdraw from the course and fail it.

Thanks anyway.

Tony

mwilliamson
May 4th, 2003, 10:54 AM
because pointers can't be copied directly, you need to override the copy contructor and the operator=.

I'll do one to show you:
MyString& operator=(const MyString& r)
{
len = r.len;
size = r.size;
ptr = new char[len+1];
strncpy( ptr, r.ptr, len+1 );
}

[COLOR=#008800]// what is size for?

mdmd
May 4th, 2003, 11:01 AM
Well, in your last attachment you didn't include your Mystring
update so I can't see what you added. Hopefully though
because you are doing things like this that you've added a
copy constructor and operator=; have you ?
Mystring a;
Mystring b;
a = b;
You also probably have a contructor that takes "a string" ?

Can you update your attachment ?

I think this is what you need for the CTOR that takes an int.
Mystring::Mystring( int inSize )
{
len = 0;
size = inSize + 1 ;
ptr = new char[inSize];
memset( ptr, 0, inSize );
}
Why do you keep both the length and size ? I suspect size means
the maximum size of the buffer.
len can be easily computed with strlen(); Why do you store it, is
that part of the assignment ?

Also, why do you default to 80 chars in the default ctor, is that
part of the assignment ? I would've done one.

Godhatesme
May 4th, 2003, 11:15 AM
A-ha - might more sense if you could see what I'm actually working from!

I did try to attach this as a .txt file but word seems to want to make it into a .doc file!!

You are required to develop a class Mystring to provide the following functionality which may best be illustrated by the following main() program which utilises this class.

#include <iostream.h>
#include <iomanip.h>
void main(void)
{
Mystring s1; // Note 1
Mystring s2(25); // Note 2
Mystring s3("This is a string"); // Note 3
Mystring s4 = s3; // Note 4
Mystring s5(s3); // Note 5
Mystring s6("Another string");

s1 = s3; // Note 6

// Here show all the information associated with s1, viz 'len',
// 'size' (see later) and the actual string.

s1 = s3 = s6;
// Here show all the info associated with s1 and s3 (i.e. show that s6 has been assigned correctly

if (s1 == s3) // Note 7

cout « "Strings s1 and s3 are the same\n" ;
else
cout « "Strings s1 and s3 are different\n" ;

if (s1 != s4) // Note 8

cout « "Strings s1 and s3 are different\n";
else
cout « "Strings s1 and s3 are the same\n";

if (s1 == "Another string") // Note 9

cout « "Strings are the same\n" ;
else
cout « "Strings are different\n" ;

if (s1 != "a string") // Note 10

cout « "Strings are different\n" ;
else
cout « "Strings are the same\n";

s1 = s5 + s3; // Note 11

// Here show all info associated with s1

cout « s1; // Note 14

s2 = s3 + s6; // Note 11a

// Here show all info associated with s2

s2 = "My string";

// Here show all info associated with s2

s2 = s2 + s2;
cout « s2 « endl;

s1 += s3; // Note 12

// Here show all info associated with s1

s3 += s6; // Note 12

s4 = "abc";

// Here show all info associated with s4

s6 = "def";

// Here show all info associated with s6

s1 = s4 += s6;

// Here show all info associated with s6

cout « s1 « endl « s4 « endl;

if (s1.length() > 80) // Note 13

cout « "String s1 is longer than 80 chars\n";
else
cout « "String s1 is not longer than 80 chars\n";

cout « "String s1 is " « s1 " and is "
« s1.length() « " characters long.\n";

cout « "Please input a string" « endl;

// Provide an input string of 20 chars.
// You should get an exception.

cin » s3; // Note 15
cout « s3;
cout « "Please input a string" « endl;

// Provide an input string of 10 chars.
// You should not get an exception.

cin » s3; // Note 15
cout « s3;

if (s5.substring("a string")) // Note 16

{
cout « "The string 'a string' is contained within "
« "s5 starting at position "
« s5.subposition("a string"); // Note 17
}

else
cout « "The string 'a string' is not contained within s5\n";
if (s3.substring("a string"))

{
cout « "The string 'a string' is contained within "
« "s3 starting at position "
« s3.subposition("a string");
}
else
cout « "The string 'a string' is not contained within s3\n";
cout « "The character at position 4 of s3 is "
« s3.getcharat(4) « endl; // Note 18
cout « s4;
s4.setcharat(5,'@'); // Note 19
cout « s4 « endl;
}

Note 1: This defines an empty string with a default length of 80 chars. Provide an appropriate constructor. You should ensure that the first character in this string is the null ('\0') character. Here there must be space for 81 characters to include the terminating null character, ten' (see later) should be set to 80 and 'size' to 0.

Note 2: This defines an empty string with a length of 25 chars.
Provide an appropriate constructor. You should ensure that the
first character in this string is the null ('\0') character. Here there
must be space for 26 characters to include the terminating null
character, 'len' (see later) should be set to 25 and 'size' to 0.

Note 3: This defines a string of the correct size to hold the given string. Provide an appropriate constructor. Here there is space for 17 characters including the null terminating characters. Both 'len' and 'size' should be set to 16.

Note 4: This defines a string initialized with another string. A copy constructor is required here.

Note 5: The copy constructor of Note 4 will suffice here.

Note 6. Overload the = operator.

Note 7: Overload the == operator.

Note 8: Overload the != operator.

Note 9. Overload the == operator.

Note 10. Overload the != operator.

Note 11. Overload the + operator. An appropriate exception should be thrown if there is insufficient space (in this case in s1) to store the concatenated strings.

Note 11 a. An appropriate exception should be thrown if there is insufficient Space (in this case in s2) to store the concatenated strings.

Note 12: Overload the += operator. An appropriate exception should be thrown if there is insufficient space to store the concatenated strings.

Note 13: int length () is a member function which returns the length of the string contained, NOT the size of the array containing the string.

Note 14: Overload the « operator for output.

Note 15: Overload the » operator for input. Throw an exception if the size of the input string is too large.

Note 16: bool substring (char *) is a member function which returns true if its parameter is contained within the string, false otherwise.

Note 17: int subposition (char *) is a member function which returns the index of the starting position of the parameter contained within the string (the first position has an index of 0). -1 is returned if the parameter is not contained within the string.

Note 18: char getcharat (int) is a member function which returns the character at the specified position of the string. An exception is thrown if the index is outside the bounds of the string.

Note 19: void setcharat (int, char) is a member function which sets the appropriate position of the string with the supplied character. An exception is thrown if the index is outside the bounds of the string.

The coding might be a bit iffy - the original code has already been logged here - but the notes are in the correct place.

So far as I can deduce, I have managed to get as far as Note 2. This is where I am stuck.

Completely Confused

Tony

mdmd
May 4th, 2003, 11:35 AM
Ok, you have

Note 1.

I'm just a little curious still why he ( instructor ) is requiring size
and len; Also I think he is swapping the meaning of size and len.
I consider len to be the length of the actual string.
I consider size to be the maximum size available to the string.
So size is always at least 1 > len. His meanings for len and size
confuses the issues I think.

You also have
Note 2:

from the previous post. You just need to configure
size and len as he wants ( I'm protesting his meanings so I wont
do it :D )

And from mWilliamson you have
Note 4:
Note 5:

So now you should be unstuck for #2 and can move on to #3
knowing that #4 and #5 are done also, which brings you to #6

So, you can continue ? Need help ? I would suggest trying to
continue. We can do it in 5 minutes, but you wouldn't learn
anything :eek: But we'll still help when wanted :D

Godhatesme
May 4th, 2003, 11:55 AM
I will give it go - Sincere THANK YOU !!

The Old Goat who 'taught' us didn't have much of a sense of humour and there were a couple of other peeps in the class who corrected some more of his code in various places !!

I'll crack on and get back here if / when I get stuck again...

Thanks once again !

A happier Tony
:)

mdmd
May 4th, 2003, 12:14 PM
Oops, wait a minute ! That was operator=() and you need to add
a couple things. I think he meant to write the copy constructor,
so lets make the swap. Here is info on operator=()

Mystring& Mystring::operator=(const Mystring& r)
{
1) if( * this == r )
return *this;

2) delete [] ptr;

3) len = r.len;
size = r.size;
ptr = new char[size];
strncpy( ptr, r.ptr, len+1 );

4) return *this;
}

1) Make sure you don't assign to yourself
2) Delete the old data ( just need to make sure that ALL CTORS
allocate with [] even if there is only 1 character.
3) Now copy and assign the new values.
4) return *this ( left hand side of operation ! )

Your copy CTOR will be the same as this but you won't need to
check for self assignment and you won't be returning *this.
So the original code for operator=() posted far above should
actually be for the copy constructor.

Of course this code requires you to have an operator==() but that
is coming up soon in your requirements. You can comment it out
till then so you can compile. Or take the address of ptr and
compare that, if ptr has the same starting address, you can be
relatively sure they point to the same thing.
ptr == r.ptr
Just DONT compare the objects addresses like this
this == &r
Because r can actually have more than one address; not in this
case, but soon enough.

mwilliamson
May 4th, 2003, 04:02 PM
good catch mdmd, I don't know what i was thinking when I wrote that ;) I missed 3 things!