I have a homework i need to solve, but i get this in build messages "||=== Build file: "no target" in "no project" (compiler: unknown) ===|"
I don't understand where i did my mistake, and i keep doing the same mistake everytime i do my homework, I need assistance in defining my errors and fixing them.
Thank you!
[/code]
#include <iostream>
#include<cassert>
using namespace std;
class setType
{
private:
int length;
double *p;
public:
void set(int);
void print() const;
setType(int length=1);
bool belongsTo(double);
};
void setType::set(int l)
{
length=l;
delete []p;
p=new double[length];
assert(p!=NULL);
cout<<"Enter the 5 elements of the set: ";
for(int i=0; i<length; i++)
{
cin>>p[i];
}
}
void setType::print() const
{
cout<<"The Element of the set are: {";
for (int i=0; i<length; i++)
{
cout<<p[i]<<" ";
}
cout<<"}";
}
You were pretty close. I removed the asserts for my convenience. You need to include a destructor which is where the delete [] goes, not where you had it. I deleted the set() method for my convenience too.
#include <iostream>
usingnamespace std;
class setType
{
private:
int length;
double *p;
public:
setType(int);
void print() const;
bool belongsTo(double);
};
setType::setType(int l)
{
length = l;
p = newdouble[length];
cout<<"Enter the 5 elements of the set: ";
for(int i=0; i<length; i++)
{
cin>>p[i];
}
}
void setType::print() const
{
cout<<"The Element of the set are: {";
for (int i=0; i<length; i++)
{
cout<<p[i]<<" ";
}
cout<<"}";
}
bool setType::belongsTo(double v )
{
for(int i=0; i<length; i++)
{
if(v==p[i])
returntrue;
}
returnfalse;
}
int main()
{
double value;
setType one(5);
one.print();
cout << "\nEnter a value to check if it is an element of the set or not: ";
cin >> value;
if(one.belongsTo(value))
cout << value << " is an element of the set." << endl;
else
cout << value << " is not an element of the set." << endl;
return 0;
}