double CAR::count_total(double price2,double total_price)
{total_price=total_price+price2;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}
void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}
//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}
int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average2,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);
for(i=1;i<=100;i++){
cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);
data1.display(price);
total=data1.count_total(price,total);}
average2=total/(i-1);
cout<<"\n Total car average : RM "<<average2;
cout<<endl;
return 0;
}
|25|error: 'pn' was not declared in this scope|
|26|error: 'p' was not declared in this scope|
||=== Build finished: 2 errors, 8 warnings ===|
This won't compile.
After correcting this function, program works fine.
HOWEVER, you doesn.t using member variables price,total_price,average (aside from assigning to it in constructor)
double CAR::count_total(double price,double total_price)
{total_price=total_price+price;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}
void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}
//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}
int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);
for(i=1;i<=100;i++){
cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);
data1.display(price);
total=data1.count_total(price,total);}
average=total/(i-1);
cout<<"\n Total car average : RM "<<average;
cout<<endl;
return 0;
}
-Compilation finished with errors:
source.cpp: In constructor 'CAR::CAR(char*, char*, double)':
source.cpp:21:13: error: 'strcpy' was not declared in this scope
(How to DECLARE?)
Oh, my mistake (cos i delete ".h" and forgot to put "c" instead. But why I got this?:
Compilation finished with errors:
source.cpp: In constructor 'CAR::CAR(char*, char*, double)':
source.cpp:19:13: error: too few arguments to function 'char* strcpy(char*, const char*)'
In file included from string.h:642:0,
from c++/4.7/cstring:44,
from source.cpp:2:
x86_64-linux-gnu/bits/string3.h:103:1: note: declared here
source.cpp:20:18: error: too few arguments to function 'char* strcpy(char*, const char*)'
In file included from string.h:642:0,
from c++/4.7/cstring:44,
from source.cpp:2:
x86_64-linux-gnu/bits/string3.h:103:1: note: declared here
double CAR::count_total(double price,double total_price)
{total_price=total_price+price;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}
void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}
//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}
int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);
for(i=1;i<=100;i++){
cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);
data1.display(price);
total=data1.count_total(price,total);}
average=total/(i-1);
cout<<"\n Total car average : RM "<<average;
cout<<endl;
return 0;
}
You didn't assigned name and plate_num anything. Local variables with the same name shadows those from class. To get access to class variables rename function arguments line name_,plate_num_ or you can access class members as this->name, this->plate_num