Hi all.
can anyone help me correct errors in my code which is not running anymore and i dont really know why. If anyone can notice anything which shouldnt be in the code let me know please. I would really appreciate any sort of help.
Before writing your code you will need to think carefully about the following (HINT: these are hints!).
1 Decide on the classes you need. You should also be careful to include a set of classes that
implement the functionality of the code in such a way that they could be used by other
developers.2. To establish suitable member functions, take into account the responsibilities of each class; for
example, which class should perform the filtering operation?3. Encapsulation dictates that, as far as possible, an object should be responsible for its own data.
4. Use polymorphism only as appropriate. The general rule is that if an inbuilt operator matches
the purpose of a member function then it should be overloaded.5. Use inheritance only where necessary. Only if you can reasonable say that object A is a type of
object B should A inherit from B. For example, a car is a type of vehicle, but a table isn’t a type
of elephant (even if both have four legs).The following also forms part of the specification (for both the part B and part C modules).
1. The program must use an object-oriented approach; it must contain only member functions
(other than main() which should do no more than create the first object).2. All members must be private.
3. Document properly the code you add. Don’t document every line of code - just the major
blocks of code (see the marking scheme).4. The structure of your code should be neat and easy to follow.
#include <iostream>
using namespace std;
// ------CLASS DEFINITIONS------
class TheData
{
public:
//public member functions
TheData(double* = 0, unsigned long = 0, bool = false);
~TheData(); //destructor function
void SetValue(double*, TheData&);
void SetValue(unsigned long, TheData&);
void SetValue( bool, TheData&);
double* ShowValue_Values();
unsigned long ShowValue_Length();
bool ShowValue_Valid();
void EnterData(TheData& OriginalData);
void DisplayData(TheData OriginalData, TheData FilteredData) const;
private:
//private data
double* Values;
unsigned long Length;
bool Valid;
};
class TheFilter
{
public:
TheFilter(double* = 0, unsigned long = 0, bool = false);
~TheFilter();
void EnterFilter(TheFilter& Filter);
void SetValue(double*, TheFilter&);
void SetValue(unsigned long, TheFilter&);
void SetValue(bool, TheFilter&);
double* ShowValue_Values();
unsigned long ShowValue_Length();
bool ShowValue_Valid();
int ApplyFilter(TheData OriginalData, TheFilter Filter, TheData& FilteredData);
void DisplayData(TheFilter Filter) const;
private:
double* Values;
unsigned long Length;
bool Valid;
};
/*class PrivateData : public TheData, public TheFilter
{
public:
double* Values;
unsigned long Length;
bool Valid;
};*/
enum {OK,FILTER_TOO_LONG}; // Function return values
// ------------MAIN------------
int main()
{
// main() to create the first object only!
//create an object of type TheData
TheData OriginalData; TheData FilteredData;
TheFilter Filter;
//call the EnterData() and OriginalData accesor functions
OriginalData.EnterData; OriginalData.DisplayData;
char UserInput;
// loop until the user wishes to exit
while (1) {
// show the menu of options
cout << endl;
cout << "Filter Menu" << endl;
cout << "-----------" << endl;
cout << "1. Enter data for filtering" << endl;
cout << "2. Enter filter values" << endl;
cout << "3. Apply filter" << endl;
cout << "4. Display filtered data" << endl;
cout << "5. Exit from the program" << endl << endl;
// get the user's choice
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
// act on the user's input
switch(UserInput) {
case '1':
OriginalData.EnterData(OriginalData);
FilteredData.SetValue(false, OriginalData);
break;
case '2':
Filter.EnterFilter(Filter);
FilteredData.SetValue(false, FilteredData);
break;
case '3':
if (Filter.ShowValue_Valid() == true && OriginalData.ShowValue_Valid() == true
&& FilteredData.ShowValue_Valid() == false)
{
if (Filter.ApplyFilter(OriginalData, Filter, FilteredData) ==
FILTER_TOO_LONG)
{
cout << "The filter must not be longer than the data." <<
endl;
}
else
{
FilteredData.SetValue(true, FilteredData);
cout << "Filter applied." << endl;
}
}
break;
case '4':
if (Filter.ShowValue_Valid() == true && OriginalData.ShowValue_Valid() == true
&& FilteredData.ShowValue_Valid() == true)
{
OriginalData.DisplayData(OriginalData, FilteredData);
Filter.DisplayData(Filter);
}
else
{
cout << "Data have not yet been filtered" << endl;
}
break;
case '5':
/*delete [] Filter.Values;
delete [] OriginalData.Values; UNSURE WHAT TO DO HERE!
delete [] FilteredData.Values;*/ return
0;
break;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
}
// --THEDATA MEMBER FUNCTIONS--
TheData::TheData(double*, unsigned long, bool)
{
Values = 0;
Length = 0;
Valid = false;
}
TheData::~TheData()
// TheData destructor function
{
/*delete[] OriginalData.Values;
delete[] OriginalData.Length;
delete[] OriginalData.Valid;
// Free memory
delete[] FilteredData.Values;
delete[] FilteredData.Length;
delete[] FilteredData.Valid;*/
}
void TheData::EnterData(TheData& OriginalData)
{
// initialize the data structure that holds the data to be filtered, including getting
// the number of data values from the user
delete[] OriginalData.Values;
cout << "How many data values do you wish to enter: ";
cin >> OriginalData.Length;
OriginalData.Valid = true;
// allocate memory to the data
OriginalData.Values = new double[OriginalData.Length];
if (OriginalData.Values == 0)
{
cout << "Unable to allocate sufficient memory." << endl;
exit(1);
}
// obtain all of the data values
cout << endl;
cout << "Enter the data values" << endl;
cout << "---------------------" << endl;
for (unsigned long CountData = 0; CountData < OriginalData.Length; CountData++)
{
cout << "Enter value " << CountData + 1 << ": ";
cin >> OriginalData.Values[CountData];
}
}
void TheData::DisplayData(TheData OriginalData, TheData FilteredData) const
{
// display all of the input data values
cout << endl;
cout << "The input data values" << endl;
cout << "---------------------" << endl;
cout << "[ ";
for (unsigned long CountData = 0; CountData < *OriginalData.Values; CountData++)
{
//cout << OriginalData.SetValue(OriginalData.Values[CountData], OriginalData) << " ";
}
cout << "]" << endl;
// display all of the data output values
cout << endl;
cout << "The data output values" << endl;
cout << "----------------------" << endl;
cout << "[ ";
for (unsigned long CountData = 0; CountData < FilteredData.Length; CountData++)
{
//cout << FilteredData.SetValue(FilteredData.Values[CountData], FilteredData) << " ";
}
cout << "]" << endl;
}
void TheData::SetValue(double* SetValue, TheData& Original_or_Filtered_Data)
{
Original_or_Filtered_Data.Values = SetValue;
}
void TheData::SetValue(unsigned long SetValue, TheData& Original_or_Filtered_Data)
{
Original_or_Filtered_Data.Length = SetValue;
}
void TheData::SetValue(bool SetValue, TheData& Original_or_Filtered_Data)
{
Original_or_Filtered_Data.Valid = SetValue;
}
double* TheData::ShowValue_Values()
{
return 0;
}
unsigned long TheData::ShowValue_Length()
{
return 0;
}
bool TheData::ShowValue_Valid()
{
return false;
}
// -THEFILTER MEMBER FUNCTIONS-
TheFilter::TheFilter(double*, unsigned long, bool)
{
Values = 0;
Length = 0;
Valid = false;
}
TheFilter::~TheFilter()
{
// delete[] "something"?
// Free memory
}
void TheFilter::EnterFilter(TheFilter& Filter)
{
// initialize the data structure that holds the filter, including getting the number of
// filter values from the user
delete [] Filter.Values;
cout << "How many data values do you wish to enter: ";
cin >> Filter.Length;
Filter.Valid = true;
// allocate memory to the filter values
Filter.Values = new double[Filter.Length];
if (Filter.Values == 0)
{
cout << "Unable to allocate sufficient memory" << endl;
exit(1);
}
// obtain all of the filter values
cout << endl;
cout << "Enter the filter values" << endl;
cout << "-----------------------" << endl;
for (unsigned long CountData = 0; CountData < Filter.Length; CountData++)
{
cout << "Enter value " << CountData + 1 << ": ";
cin >> Filter.Values[CountData];
}
}
int TheFilter::ApplyFilter(TheData OriginalData, TheFilter Filter, TheData& FilteredData)
{
// return an error if the filter is longer than the data
if (Filter.Length > OriginalData.Length) return FILTER_TOO_LONG;
// initialize the data structure that holds the filtered data
delete[] FilteredData.Values;
FilteredData.Length = OriginalData.Length - Filter.Length + 1;
// get memory for the filtered data
FilteredData.Values = new double[FilteredData.Length];
if (FilteredData.Values == 0)
{
cout << "Unable to allocate sufficient memory" << endl;
exit(1);
}
// apply the filter to the data
for (unsigned long CountData = 0; CountData < FilteredData.Length; CountData++)
{
FilteredData.Values[CountData] = 0.0;
for (unsigned long CountFilter = 0; CountFilter < Filter.Length; CountFilter++)
{
FilteredData.Values[CountData] += OriginalData.Values[CountData + CountFilter]
* Filter.Values[CountFilter];
}
}
return OK;
}
void TheFilter::DisplayData(TheFilter Filter) const
{
// display all of the filter values
cout << endl;
cout << "The filter values" << endl;
cout << "-----------------" << endl;
cout << "[ ";
for (unsigned long CountData = 0; CountData < Filter.Length; CountData++)
{
//cout << Filter.SetValue(Filter.Values[CountData], Filter) << " ";
}
cout << "]" << endl;
}
void TheFilter::SetValue(double* SetValue, TheFilter& Filter)
{
Filter.Values = SetValue;
//cout << "Testing Values: " << Filter.Values << " = " << SetValue;
}
void TheFilter::SetValue(unsigned long SetValue, TheFilter& Filter)
{
Filter.Length = SetValue;
//cout << "Testing Length: " << Filter.Length << " = " << SetValue;
}
void TheFilter::SetValue(bool SetValue, TheFilter& Filter)
{
Filter.Valid = SetValue;
//cout << "Testing Valid: " << Filter.Valid << " = " << SetValue;
}
double* TheFilter::ShowValue_Values()
{
double* ShowValue;
ShowValue = 0; //Filter.Values;
return ShowValue;
}
unsigned long TheFilter::ShowValue_Length()
{
unsigned long ShowValue;
ShowValue = 0; //Filter.Length;
return ShowValue;
}
bool TheFilter::ShowValue_Valid()
{
bool ShowValue;
ShowValue = false; //Filter.Valid;
return ShowValue;
}