#include<iostream>

using namespace std;

class product 
{
private:
    int code;
    char name[20];
    double price;
int instock;
double soldperyear[12];
public:
    product(int,char[],double,int);
    void setproduct(int c , char n[],double p,int i)
    {
    setcode(c);
    setname(n);
    setprice(p);
    setinstock(i);
    }
    void peryear(int i)
    {
    for ( i=1;i<=12;i++)
        soldperyear[i]=0;}
void setcode(int c)
{
    if(c>=2500&&c<=2750)
        code=c;
    else 
        code=2500;
}
void setname(char n[])

{
strncpy(name, n,19);
        name[19]='\0';}
void setprice(double p)
{
price = p> 0.0?p:0.0;
}
void setinstock(int i)
{i=0;
    instock=i;
}
int getcode()
{ return code;}
char * getname()
{
return name;
}
double getprice()
{
return price;
}
int getstock()
{
    return instock;
}
void increment(int inc)
{
    instock=instock +inc ;
}
void decrement(int ins,int month)
{if(instock>=ins){
    instock-=ins;
    soldperyear[month]+=ins;}
else cout<<" not enough products in stock "<<endl;
}
double t (){
double total =0;
for (int i=1;i<=12;i++)
total +=soldperyear[i];
return total ;

}
void print() {

    cout<<" Product: "<<endl;
    cout<<"code :"<<getcode()<<endl;
    cout<<"Name : "<<getname()<<endl;
    cout<<"price: "<<getprice()<<endl;
    cout<<"sold per year : " <<t()<<endl;
    cout<<" In stock : " <<getstock()<<endl;



}

};
product::product(int c, char n[], double p, int i){
  setproduct( c ,  n, p, i);

}
void main()
{int code;
char name[20];
double price;
int instock;
int x;
int z;

 product car,&ref=car,*ptr=&car,p,&refp=p,*ptrp=&p;

cout<<"Car: "<<endl
<<"Enter car code: "<<endl;
cin>>code;
car.setcode(code);

cout<<"Enter Name: "<<endl;
cin>>name;
ref.setname(name);

cout<<"Enter price :"<<endl;
cin>>price;
ptr->setprice(price);

cout<<"Available in stock:"<<endl;
cin>>instock;
car.setinstock(instock);
for(int i=0;i<12;i++)
{
int month;
cout<<"quantity sold per month"<<i+1<<endl;
cin>>month;
car.decrement(month,i);

}
cout<<car.t();

cout<<"if you want to increment your stock press 1 esle 0 to exit "<<endl;
cin>>x;
if(x==1){int z;
    cout<<"enter qauntity you want to increment:"<<endl;
    cin>>z;
    car.increment(z);}

car.print();
cout<<endl<<endl<<endl;

cout<<"P: "<<endl
<<"Enter P code: "<<endl;
cin>>code;
p.setcode(code);

cout<<"Enter Name: "<<endl;
cin>>name;
refp.setname(name);

cout<<"Enter price :"<<endl;
cin>>price;
ptrp->setprice(price);

cout<<"Available in stock:"<<endl;
cin>>instock;
p.setinstock(instock);
for(int i=0;i<12;i++)
{
int month;
cout<<"quantity sold per month"<<i+1<<endl;
cin>>month;
p.decrement(month,i);

}
cout<<p.t();

cout<<"if you want to increment your stock press 1 esle 0 to exit "<<endl;
cin>>x;
if(x==1){int z;
    cout<<"enter qauntity you want to increment:"<<endl;
    cin>>z;
    p.increment(z);}

p.print();
cout<<endl<<endl<<endl;
}

Here's the first error:

36:23: error: ‘strncpy’ was not declared in this scope

Now I've helped you find it, you can fix it.

Let's start by getting this code into some semblance of consistent indentation:

#include<iostream>

using namespace std;

class product 
{
private:
    int code;
    char name[20];
    double price;
    int instock;
    double soldperyear[12];
public:
    product(int,char[],double,int);

    void setproduct(int c , char n[],double p,int i)
    {
        setcode(c);
        setname(n);
        setprice(p);
        setinstock(i);
    }

    void peryear(int i)
    {
        for ( i=1;i<=12;i++)
            soldperyear[i]=0;
    }

    void setcode(int c)
    {
        if(c>=2500&&c<=2750)
            code=c;
        else 
            code=2500;
    }

    void setname(char n[])
    {
        strncpy(name, n,19);
        name[19]='\0';
    }

    void setprice(double p)
    {
        price = p> 0.0?p:0.0;
    }

    void setinstock(int i)
    {
        i=0;
        instock=i;
    }

    int getcode()
    {  
        return code;
    }

    char * getname()
    {
        return name;
    }

    double getprice()
    {
        return price;
    }

    int getstock()
    {
        return instock;
    }

    void increment(int inc)
    {
        instock=instock +inc ;
    }

    void decrement(int ins,int month)
    {
        if(instock>=ins)
        {
            instock-=ins;
            soldperyear[month]+=ins;
        }
        else 
            cout<<" not enough products in stock "<<endl;
    }

    double t (){
        double total =0;
        for (int i=1;i<=12;i++)
            total +=soldperyear[i];
        return total ;

    }

    void print() {
        cout<<" Product: "<<endl;
        cout<<"code :"<<getcode()<<endl;
        cout<<"Name : "<<getname()<<endl;
        cout<<"price: "<<getprice()<<endl;
        cout<<"sold per year : " <<t()<<endl;
        cout<<" In stock : " <<getstock()<<endl;
    }

};

product::product(int c, char n[], double p, int i)
{
    setproduct( c ,  n, p, i);
}

void main()
{
    int code;
    char name[20];
    double price;
    int instock;
    int x;
    int z;

    product car,&ref=car,*ptr=&car,p,&refp=p,*ptrp=&p;

    cout<<"Car: "<<endl
        <<"Enter car code: "<<endl;
    cin>>code;
    car.setcode(code);

    cout<<"Enter Name: "<<endl;
    cin>>name;
    ref.setname(name);

    cout<<"Enter price :"<<endl;
    cin>>price;
    ptr->setprice(price);

    cout<<"Available in stock:"<<endl;
    cin>>instock;
    car.setinstock(instock);
    for(int i=0;i<12;i++)
    {
        int month;
        cout<<"quantity sold per month"<<i+1<<endl;
        cin>>month;
        car.decrement(month,i);
    }
    cout<<car.t();

    cout<<"if you want to increment your stock press 1 esle 0 to exit "<<endl;
    cin>>x;
    if(x==1)
    { 
        int z;
        cout<<"enter qauntity you want to increment:"<<endl;
        cin>>z;
        car.increment(z);
    }

    car.print();
    cout<<endl<<endl<<endl;

    cout<<"P: "<<endl
        <<"Enter P code: "<<endl;
    cin>>code;
    p.setcode(code);

    cout<<"Enter Name: "<<endl;
    cin>>name;
    refp.setname(name);

    cout<<"Enter price :"<<endl;
    cin>>price;
    ptrp->setprice(price);

    cout<<"Available in stock:"<<endl;
    cin>>instock;
    p.setinstock(instock);
    for(int i=0;i<12;i++)
    {
        int month;
        cout<<"quantity sold per month"<<i+1<<endl;
        cin>>month;
        p.decrement(month,i);

    }
    cout<<p.t();

    cout<<"if you want to increment your stock press 1 esle 0 to exit "<<endl;
    cin>>x;
    if(x==1)
    {
        int z;
        cout<<"enter qauntity you want to increment:"<<endl;
        cin>>z;
        p.increment(z);
    }

    p.print();
    cout<<endl<<endl<<endl;
}

Now that we can read the code without a lot of confusion, could you please tell us what error you are getting, and post the error messages (if any) so we can go over them?

I do see several potential issues, though I don't know if any of them are related to the error you are getting. These are the error messages g++ 4.9.2 gives for your code:

stock.cpp: In member function ‘void product::setname(char*)’:
stock.cpp:40:20: error: ‘strncpy’ was not declared in this scope
  strncpy(name, n,19);
                    ^
stock.cpp: At global scope:
stock.cpp:115:11: error: ‘::main’ must return ‘int’
 void main()
           ^
stock.cpp: In function ‘int main()’:
stock.cpp:124:13: error: no matching function for call to ‘product::product()’
     product car,&ref=car,*ptr=&car,p,&refp=p,*ptrp=&p;
             ^
stock.cpp:124:13: note: candidates are:
stock.cpp:110:1: note: product::product(int, char*, double, int)
 product::product(int c, char n[], double p, int i)
 ^
stock.cpp:110:1: note:   candidate expects 4 arguments, 0 provided
stock.cpp:5:7: note: product::product(const product&)
 class product 
       ^
stock.cpp:5:7: note:   candidate expects 1 argument, 0 provided
stock.cpp:124:36: error: no matching function for call to ‘product::product()’
     product car,&ref=car,*ptr=&car,p,&refp=p,*ptrp=&p;
                                ^
stock.cpp:124:36: note: candidates are:
stock.cpp:110:1: note: product::product(int, char*, double, int)
 product::product(int c, char n[], double p, int i)
 ^
stock.cpp:110:1: note:   candidate expects 4 arguments, 0 provided
stock.cpp:5:7: note: product::product(const product&)
 class product 
       ^
stock.cpp:5:7: note:   candidate expects 1 argument, 0 provided
stock.cpp:122:9: warning: unused variable ‘z’ [-Wunused-variable]
     int z;
  • Once again, you are declaring void main(). I won't go over the reasons this wrong again, but a better compiler would at least give a warning.
  • You are not #includeing the <cstdlib> header, which is where the declarations for several of the functions you are using (e.g., strncpy()) are found. This should be a showstopper right there, though whether or not it is depends on the implementation of the headers (some implementations include headers themselves willy-nilly, making it seem like the headers aren't needed when they actually are).
  • You do not define a default constructor for product, but on this line

    product car,&ref=car,ptr=&car,p,&refp=p,ptrp=&p;

you declare several items of that class without the necessary parameter list to initialize them. While only the variable car actually needs to have the initializer arguments (all the others end up referring or pointing to car), the fact that car isn't getting constructed correctly causes the rest of them to fail in a cascade error.

Again, these may not be the same problems you've encountered, though if it is not that last one at the root of it I would be surprised. Could you please post the errors you are getting, so we can see what you are trying to fix?

yup thanks guys but the problem is not with strncpy theres another problem with error " no appropriate default constructor available" and it point to product inside main ()

In your code there is one way to create a product object; by calling the constructor product(int,char[],double,int). This is because this is the only constructor you wrote.

So when you try to do this: product car on line 124, you're trying to use the default constructor, which as a function looks like this: product().

No such constructor exists. You get a constructor like that made automatically for you if you don't create any constructors yourself, or if you explicitly create a constructor like that yourself.

So either write the default constructor product(), or make sure that you only create product objects using the constructor that you did write.

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.