Struct pointer initialization
Hello everyone,
I wrote a piece of code here, which actually works, but I was told to initialize the data structure that holds the data to be filtered and I dont really understand how to do this.
As you can see
Quote:
double* Values; // holds the data to be filtered
The pointer Values will be used to create a dynamic array, but first I was told to initialize it . So, how do I initialize it?
I have a function
Quote:
void EnterData(TheData& DataIn)
{
// initialize the data structure that holds the data to be filtered, including getting
// the number of data values from the user
cout << "Please enter the number of data values to be filtered:" << endl;
cin >> DataIn.Length;
cout << endl;
So, I was trying to write something like DataIn.Values = NULL to initialize the structure where data will be held, but it doesnt really work.
Thank you for any help.
Here is a full code.
Code:
#include <iostream>
using namespace std;
struct TheData
{
double* Values; // holds the data to be filtered
unsigned long Length; // number of data values
bool Valid; // true if the data values have been Valid by the user
};
void EnterData(TheData&);
int main()
{
int a;
TheData OriginalData = {0,0,false};
EnterData(OriginalData);
cout << endl;
//cout << OriginalData.Length;
cin >> a;
delete [] OriginalData.Values;
}
// Allow the user to enter the data to be filtered
// Arguments:
// (1) the structure containing the input data
// Returns: nothing
//
void EnterData(TheData& DataIn)
{
// initialize the data structure that holds the data to be filtered, including getting
// the number of data values from the user
cout << "Please enter the number of data values to be filtered:" << endl;
cin >> DataIn.Length;
cout << endl;
// allocate memory to the data
DataIn.Values = new(double[DataIn.Length]);
// obtain all of the data values
cout << "Please enter the data values to be filtered:" << endl;
for (unsigned int i=0; i<DataIn.Length; i++)
{
cout << "Please enter value #" << i+1 << ": ";
cin >> DataIn.Values[i];
}
DataIn.Valid = true;
cout << "All the values were saved successfully.";
}