Re: [Dev-C++] newbie questions without answer
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Carlos d. M. <cg...@wo...> - 2002-08-11 18:57:43
|
The code post is only a part, talking about the files he gave (perhaps
you didn't receive the files?).
I wrote principally for him, but as you want see all, here is the code:
The code for the struct thing (newbiequestion.cpp) is:
#include <iostream>
#include <stdlib.h>
#include <string.h>
struct date{
int month;
int day;
int year;
};
struct employ{
char *first;
char *second;
date eyear; /*i want to use a reference to the year in the first
struct
but then i get an error message from the compiler since i
didn't
initialize eyear!! how am i to get rid of this
problem??*/
};
int main()
{
date d;
employ e;
d.day=3D30;
d.month=3D8;
d.year=3D1982;
e.eyear =3D d;
e.first=3Dnew char [strlen("senem")]; // a way of doing this
shortly??
e.second=3Dnew char [strlen("beken")];
e.first=3D"senem";
e.second=3D"beken";
cout<<e.first<<" "<<e.second<<endl;
cout<<"birthday:\n"
<<e.eyear.day<<"/"<<e.eyear.month<<"/"<<e.eyear.year<<endl;
system("PAUSE");
return 0;
}
Another is the stringclas.cpp file which source is:
#include <iostream>
#include <stdlib.h>
#include <cassert>
#include <string.h>
#include <iomanip.h>
class string{
friend ostream &operator<<(ostream &,const string&);
friend istream &operator>>(istream &,string&);
public:
string(const char * =3D"");
string(const string &);
~string();
const string &operator=3D(const string &);
const string &operator+=3D(const string &);
bool operator!()const;
bool operator=3D=3D(const string&)const;
bool operator<(const string&)const;
bool operator!=3D(const string& right)const
{return !(*this=3D=3Dright);}
bool operator>(const string& right)const
{return (right<*this);}
bool operator<=3D(const string& right)const
{return !(right<*this);}
bool operator>=3D(const string& right)const
{return !(*this<right);}
char &operator[](int);
const char &operator[](int)const;
string operator()(int,int);
int getlength()const;
private:
int length;
char * sptr;
void setstring(const char*);
};
string::string(const char *s):length(strlen(s)){
cout<<"conversion constructor :"<<s<<"\n";
setstring(s);
}
string::string(const string©):length(copy.length){
cout<<"copy constructor: "<<copy.sptr<<"\n";
setstring(copy.sptr);
}
string::~string(){
cout<<"destructor: "<<sptr<<"\n";
delete[]sptr;
}
const string &string::operator=3D(const string &right){ //burda nie ref
var??
cout<<"operator=3D called\n";
if(&right!=3Dthis){
delete[]sptr;
length=3Dright.length;
setstring(right.sptr);
}
else
cout<<"attempted assignment of a string to itself\n";
return *this;
}
const string &string::operator+=3D(const string &right){
char *string1=3Dnew char[length+right.length+1]; ///DON'T MODIFY THE
SIZE
strcpy(string1,sptr); /* i tried to change this part a little
so that the first string is not modified,but
then
the program started to terminate abnormally
and i have absolute no idea what i did wrong!!
here is the original:
char *tempptr=3Dsptr;
lenght+=3Dright.length;
sptr=3Dnew char [length+1];
assert(sptr!=3D0);
strcpy(sptr,tempptr);
strcat(sptr,right.ptr);
delete[]tempptr;
return *this;*/
strcat(string1,right.sptr);
}
bool string::operator!()const{return length=3D=3D0;}
bool string::operator=3D=3D(const string &right)const
{return strcmp(sptr,right.sptr)=3D=3D0;}
bool string::operator<(const string &right)const
{return strcmp(sptr,right.sptr)<0;}
char &string::operator[](int subscript){
assert(subscript>=3D0&&subscript<length);
return sptr[subscript];
}
const char &string::operator[](int subscript)const{
assert (subscript>=3D0&&subscript<length);
return sptr[subscript];
}
string string::operator()(int index,int sublength){
assert(index>=3D0&&index<length&&sublength>=3D0);
int len;
if((sublength=3D=3D0)||(index+sublength))
len=3Dlength-index;
else
len=3Dsublength;
char *tempptr=3Dnew char[len+1];
assert(tempptr!=3D0);
strncpy(tempptr,&sptr[index],len);
tempptr[len]=3D'\0';
string tempstring(tempptr);
delete[]tempptr;
return tempstring;
}
int string::getlength()const{return length;}
void string::setstring(const char *string2){
sptr=3Dnew char[length+1];
assert(sptr!=3D0);
strcpy(sptr,string2);
}
ostream &operator<<(ostream &output,const string &s){
output<<s.sptr;
return output;
}
istream &operator>>(istream &input,string &s){
char temp[100];
input>>setw(100)>>temp;
s=3Dtemp;
return input;
}
int main()
{
string s1("happy"),s2("birthday"),s3;
cout<<"s1 is\""<<s1<<"\";s2 is\""<<s2
<<"\";s3 is\""<<s3<<'\"'
<<"\n the results of comparing s2 and s1:"
<<"\ns2=3D=3Ds1 yields"
<<(s2=3D=3Ds1?"true":"false")
<<"\ns2!=3Ds1 yields "
<<(s2!=3Ds1 ?"true":"false")
<<"\ns2>s1 yields "
<<(s2>s1?"true":"false")
<<"\ns2<s1 yields "
<<(s2<s1 ?"true":"false")
<<"\ns2>=3Ds1 yields "
<<(s2>=3Ds1 ?"true":"false")
<<"\ns2<=3Ds1 yields "
<<(s2<=3Ds1?"true":"false");
cout<<"\n\n testing !s3:\n";
if(!s3){
cout<<"s3 is empty,assigning s1 to s3;\n";
s3=3Ds1;
cout<<"s3 is\""<<s3<<"\"";
}
cout<<"\n\n s1+=3Ds2 yields s1=3D";
s1+=3Ds2;
cout<<s1;
cout<<"\n\n s1+=3D\"to you\"yields\n";
s1+=3D"to you";
cout<<"s1 =3D "<<s1<<"\n\n";
cout<<"the substring od s1 starting at\n"
<<"location 0 for 14 characters,s1(0,14),is:\n"
<<s1(0,14)<<"\n\n";
cout<<"the substring of s1 starting at \n"
<<"loaction 15,s1(15,0),is:" //<- 15 , which is out of
bounds due to the +=3D modification
<<s1(15,0)<<"\n\n";
string *s4ptr=3Dnew string(s1);
cout<<"s4ptr =3D"<<*s4ptr<<"\n\n";
cout<<"assigning *s4ptr to *s4ptr\n";
*s4ptr=3D*s4ptr;
cout<<"*s4ptr=3D "<<*s4ptr<<"\n";
delete s4ptr;
s1[0]=3D'H';
s1[6]=3D'B';
cout<<"\n s1 after s1[0]=3D'H' and s1[6]=3D'B' is: "
<<s1<<"\n\n";
system("PAUSE");
return 0;
}
Ioannis Vranos escribi=F3:
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Carlos
> Garc=EDa del Monte
> Sent: Sunday, August 11, 2002 3:49 PM
> To: dev-cpp-users-list
> Subject: Re: [Dev-C++] newbie questions without answer
>
>
>
> newbiequestion.cpp
>
> struct employ{
> char *first;
> char *second;
> date eyear; /*i want to use a reference to the year in
> the first struct
>
> I do not see any reference here. Also what first struct?
>
>
> but then i get an error message from the
> compiler since i didn't
> initialize eyear!! how am i to get rid of
> this problem??*/
>
> Provide a default constructor or default arguments to the provided
> constructor.
>
>
> I have no problems compiling it.
> employvar.eyear.(day/month/year)=3D x;
>
> That's a bit strange.
>
> Or
> date *eyear
> employvar.eyear->day=3Dx.
>
> Or
> date *eyear; in employer struct.
> datevar.day =3D x;
> employvar.eyear =3D datevar;
> Or datevar.day=3Dx;
> datevar.eyear =3D &datevar;
>
> I hope you know what you are doing in the above.
>
> The other question about shorter way. I don't understand.
>
> In the stringclass what happens is that when you modify the
> +=3D operator, so the string of the left hand string object is
> no midified you make that neither it size is modified
> (evidently), but then, ine the code, at line 178 - 190, the
> code assumes that function has not been changed (you did not
> adapted it to your modifications), so as it wants to output
> the substrings from a subscript to another position, and the
> positions are not found through object's functions, they are
> put ad hoc, the code at this lines are intended so you had
> just added s2 to s1, so s1 is "happy birthday to you", which
> has 21 characters, and there the code is "substring from 0,
> 14 and after from 15, 0", which are ranges that would be in
> s1, but you modified it in a way such now s1 is "happy",
> five chars, so when you ask for range 15,0 you're giving a
> range which is out of the bounds of s1.
>
> The burda nie ref var, that's the way....
>
> I am not good at understanding technical problems from text. Please
> provide some example code with comments.
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.cjb.net* Alternative URL:
> http://run.to/noicys
>
|