My program is working perfectly but when I want to initialize the default constructor which is all zero's
it prints -9.25596e+061 .
Hier is the code :

class Sales
{
private: 
static const int QUARTERS = 4;


public:
double sales[QUARTERS];
double average;
double max;
double min;
Sales();
Sales( double ar[]); 
void setSales(Sales & s, const double ar[]);
void setSales(Sales & s);
void showSales(const Sales & s);
};


Sales::Sales() 

{ 

double sales[]=
{
0.0,0.0,0.0,0.0
           };


}

What's the problem?

Thank you in advance!    

Thank you in advance!

Recommended Answers

All 9 Replies

Just initialize it in a loop. In the constructor you are declaring a new local variable for the constructor named sales. It will also be a good idea of having all your variables for the class being private instead of public.

for(int i =0 ; i < QUARTERS; i++)
{
    sales[i]=0.0;
}

thanks now it works but it doesn't make sense it's the same thing but other way of writing

It is not the same thing

Sales::Sales()
{
    double sales[] = {0.0, 0.0, 0.0, 0.0};
}

The above code creates an double array called sales in the constructor which hides the sales array that is a member of the class. Once the constructor is done this local array that has all zeros is deleted.

Sales::Sales()
{
    for(int i =0 ; i < QUARTERS; i++)
    {
        sales[i]=0.0;
    }
}

This code uses a loop and sets each element of the sales array that is a member of Sales to 0.

sorry for asking again but it turned out that this is not the only problem it is the same problem with the second constructor i assign again zeros and it prints -9.25596e+061

Sales::Sales(double ar[4]) 
{

    for (int i=0 ; i<4 ; i++)
        ar[i]=sales[i];
    double n=0;

    for(int i=0;i<4;i++)
    {
        if(ar[i]>n)
        n=ar[i];
     }
    max=n;
    double j=max;
    for(int i=0;i<4;i++)
    {
        if(ar[i]<j)
            {
                j=ar[i];
                min=j;
            }

    }

    double sum=0;

for(int i=0;i<4;i++)
    {
    sum += ar[i];
    }
     average=sum/4;
    }

Line 5 should be sales[i] = ar[i]. All subsequent uses of ar[i] should be replaced with sales[i].

doesn't make any difference same result as the previous

what is in ar[] when you pass it to your constructor? Can you post the code that is not working? You need to be a little more detialed with your question/problem when posting. Having to play 20 questions is not something I want to do. You need to have a clear explanation as to what is not working and all relevent code. Don't post all of the code just what we need to see how it works.

I tried to write a second constructor that should pass an array "ar" to be used. And the constructor should store information in the members average, max and min.

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.