Hello i got an assignment to do on classes, basically i got 90% of the program working except for some minor issues which i cant seem to figure out.
Here is the code:

#include <iostream.h>
#include <assert.h>
#include <cstring>
class weather
{
private:
	int day;
	int month;
	int year;
	int temp;
	int humid;
	char *condition;
	void leapyear(int);
public:
	weather(int =1,int =99,int =54,int =100,int =33,char*= "Elie"); //Problem
	~weather();
	void setall(int,int,int,int,int,char*);
	int get_day();
	int get_month();
	int get_year();
	int get_temp(int);
	int get_humid();
	void print();
	void get_condition();
};




weather::weather(int x,int y,int z,int t,int h,char *k)
{
	setall(x,y,z,t,h,k)
};
weather::~weather()
{ delete condition;}

void weather::setall(int a,int b,int c,int d,int e,char *f)
{
	

		{if (a<1 ||a>31)//day
			day=1;
		else 
			day=a;}

		{if (b<1 || b>12)//month
			month=1;
		else
			month=b;}
		{if(c<1900||c>2005)//year
			year=2000;
		else 
			year=c;}

		{if (d<60||d>60)//temp
			temp=20;
		else 
			temp=d;}
		
		{if (e<0||e>100)//humid
			humid=55;
		else
			humid=e;}

		condition=new char[strlen(f)+1];
		assert (condition!=0);
		strcpy(condition,f);
}

int weather::get_day()
{return day;}

int weather::get_month()
{return month;}

int weather::get_year()
{return year;}

int weather::get_temp(int t)
{	t=0;
t=((9/5)*temp)+32;
	return t;
	}

int weather::get_humid()
{return humid;}


void weather::leapyear(int a)
{
	a=year;

	if (a%4==0 && a%100!=0)
		cout<<"This is a leap year"<<endl;
	else
		cout<<"This is not a leap year"<<endl;
}


void weather::print()
{
	cout<<"On the: "<<day<<"/"<<month<<"/"<<year<<endl;
	cout<<"-------------------------------------------------------"<<endl;
	cout<<"Temperature is: "<<temp<<endl;
	cout<<"Humidity (in %) is: "<<humid<<endl;
	cout<<"Condition is "<<condition<<endl;
}





void weather::get_condition()
{ cout<<condition;}

void main()

{
	weather a;

	a.print();

}

Can anyone please compile it and tell me where my mistakes are.
I would like to thank anyone that helps .

Recommended Answers

All 3 Replies

Hello i got an assignment to do on classes, basically i got 90% of the program working except for some minor issues which i cant seem to figure out.
Here is the code:
code
Can anyone please compile it and tell me where my mistakes are.
I would like to thank anyone that helps .

small changes compiled it but just have a look at assert() which i didnt get

#include <iostream>
//#include <assert>
#include <cstring>

using namespace std; 
class weather
{
private:
int day;
int month;
int year;
int temp;
int humid;
char *condition;
void leapyear(int);
public:
//weather(1,99,54,100,33,"Elie"); //Problem
weather(int x,int y,int z,int t,int h,char *k)
{
setall(x,y,z,t,h,k);
}
~weather();
void setall(int,int,int,int,int,char*);
int get_day();
int get_month();
int get_year();
int get_temp(int);
int get_humid();
void print();
void get_condition();
};



/*weather(int x,int y,int z,int t,int h,char *k)
{
setall(x,y,z,t,h,k)
};*/
weather::~weather()
{ delete condition;}

void weather::setall(int a,int b,int c,int d,int e,char *f)
{


{if (a<1 ||a>31)//day
day=1;
else 
day=a;}

{if (b<1 || b>12)//month
month=1;
else
month=b;}
{if(c<1900||c>2005)//year
year=2000;
else 
year=c;}

{if (d<60||d>60)//temp
temp=20;
else 
temp=d;}

{if (e<0||e>100)//humid
humid=55;
else
humid=e;}

condition=new char[strlen(f)+1];
//assert (condition!=0);
strcpy(condition,f);
}

int weather::get_day()
{return day;}

int weather::get_month()
{return month;}

int weather::get_year()
{return year;}

int weather::get_temp(int t)
{ t=0;
t=((9/5)*temp)+32;
return t;
}

int weather::get_humid()
{return humid;}


void weather::leapyear(int a)
{
a=year;

if (a%4==0 && a%100!=0)
cout<<"This is a leap year"<<endl;
else
cout<<"This is not a leap year"<<endl;
}


void weather::print()
{
cout<<"On the: "<<day<<"/"<<month<<"/"<<year<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"Temperature is: "<<temp<<endl;
cout<<"Humidity (in %) is: "<<humid<<endl;
cout<<"Condition is "<<condition<<endl;
}





void weather::get_condition()
{ cout<<condition;}

int main()

{
weather a(1,99,54,100,33,"Elie");;

a.print();
getchar();
}
#include <iostream>
#include <string>

using namespace std;

class weather {
      private:
              int day;
              int month;
              int year;
              int temp;
              int humid;
              string condition;
              void leapyear(int);
      public:
             weather(int a=1,int b=99,int c=54,int d=100,int e=33,string= "Elie"); //Problem
             //~weather();
             void setall(int,int,int,int,int);
             int get_day();
             int get_month();
             int get_year();
             int get_temp(int);
             int get_humid();
             void print();
             void get_condition();
};




weather::weather(int x,int y,int z,int t,int h,string k)
        :condition(k)
{
    setall(x,y,z,t,h);
};
//weather::~weather() { delete condition;};

void weather::setall(int a,int b,int c,int d,int e)
{

     if (a<1 ||a>31)//day
         day=1;
     else 
         day=a;

     if (b<1 || b>12)//month
         month=1;
     else
         month=b;
         
     if(c<1900||c>2005)//year
         year=2000;
     else 
         year=c;

     if (d<60||d>60)//temp
        temp=20;
     else 
        temp=d;

     if (e<0||e>100)//humid
        humid=55;
     else
        humid=e;

     //condition=new char[strlen(f)+1];
     //assert (condition!=0);

}

int weather::get_day() {return day;};

int weather::get_month() {return month;};

int weather::get_year() {return year;};

int weather::get_temp(int t)
{ t=0;
  t=((9/5)*temp)+32;
  return t;
}

int weather::get_humid() {return humid;};


void weather::leapyear(int a)
{
 a=year;

 if (a%4==0 && a%100!=0)
    cout<<"This is a leap year"<<endl;
 else
    cout<<"This is not a leap year"<<endl;
}


void weather::print()
{
cout<<"On the: "<<day<<"/"<<month<<"/"<<year<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"Temperature is: "<<temp<<endl;
cout<<"Humidity (in %) is: "<<humid<<endl;
cout<<"Condition is "<<condition<<endl;
}





void weather::get_condition() { cout<<condition;};

int main()
{
 weather a;

 a.print();

 cin.get();
}

The errors:

1. When you assign values by default you have to either provide variable names or default initializers (see above).
2. The setall() function is unnecessary, all of it's body belongs to the class initializer function.
3. Since you assign a string value for condition and you're using C++ you can omit encountering C-style strings. Furthermore, since you assing value by default you don't have to assert() it. There are better functions to provide a check for the length of a string (size(), length(), for example).
4. Your destructor was a bit strange since it destroys a member of the class and not the class itself. I commented it out. If you don't provide a destructor in a class, a default destructor is called, so - in this case you don't have to worry.
5. The other errors were minor and mostly semicolons here and there but my advice is to try to correct the errors you get first yourself, instead of directly posting code after the compiler returns errors. This is the best way to learn :)

Thank you guys for your quick replies , and for next time i'll try to figure them myself . The problem here is that i am still a beginner with it so it will take me sometime to get used to it :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.