level 6
如下定义了两个类,一个基类,一个派生类,然后定义了一个拍成类对象的链表,但是当使用getline(cin,astring)函数输入string成员时无法读取(getline第三个参数为默认),但当把getline()函数第三个参数设置为某一个字符(如:getline(cin,astring,'#')时,可以读入,为什么?
class box{
protected:
double height;
double length;
double wideth;
public:
box(double heightvalue,double lengthvalue,double widethvalue);
box();
box(const box& abox);
box& operator=(const box& abox);
virtual double volume();
bool operator<(box& abox);
friend double getwideth(box& abox);
static double getlength(box& abox);
double showvolume();
double getheight()const{return height;};
double getlength()const{return length;};
double getwideth()const{return wideth;};
};
class carton:public box{
public:
string* pmentral;
carton* next;
double getheight();
carton(string pstr="paple");
carton(double heightvalue,double lengthvalue,double widethvalue,string pstr="paple",carton* next=NULL);
carton(const carton& acarton);
void getmentral(const carton& acarton);
double volume();
double showvolume();
carton* createlist();
};
carton* carton::createlist()
{
carton* head=NULL;
carton* end=head;
carton* ps;
char ch;
cout<<"Please input the value of the carton!"<<endl;
string astring;
do{
double heightvalue=0;
double lengthvalue=0;
double widethvalue=0;
cin>>heightvalue;
cin>>lengthvalue;
cin>>widethvalue;
getline(cin,astring); //此处为何读取不了输入的字符串???
cout<<astring;
ps=new carton(heightvalue,lengthvalue,widethvalue,astring);
if(head==NULL)
{
head=ps;
}
else
{
end->next=ps;
}
end=ps;
cout<<"Do you want to input again?"<<endl;
cin.ignore(1024,'\n');
cin>>ch;
cout<<endl;
}while(ch=='y'||ch=='Y');
end->next=NULL;
// ps->next=NULL;
// delete ps;
return head;
};
2011年04月17日 15点04分
1
level 6
怎么没人啊,下面是全部代码:
#include <iostream>
#include <string>
using namespace std;
class box{
protected:
double height;
double length;
double wideth;
public:
box(double heightvalue,double lengthvalue,double widethvalue);
box();
box(const box& abox);
box& operator=(const box& abox);
virtual double volume();
bool operator<(box& abox);
friend double getwideth(box& abox);
static double getlength(box& abox);
double showvolume();
double getheight()const{return height;};
double getlength()const{return length;};
double getwideth()const{return wideth;};
};
box::box(double heightvalue,double lengthvalue,double widethvalue):
height(heightvalue),length(lengthvalue),wideth(widethvalue){};
box::box()
{
height=length=wideth=1.0;
};
box::box(const box& abox)
{
height=abox.height;
length=abox.length;
wideth=abox.wideth;
// return *this;
};
box& box::operator=(const box& abox)
{
height=abox.height;
length=abox.length;
wideth=abox.wideth;
return *this;
};
double box::volume()
{
cout<<"The box volume() do!"<<endl;
return height*length*wideth;
};
bool box::operator<(box& abox)
{
return volume()<abox.volume();
};
double box::showvolume()
{
return volume();
};
class carton:public box{
//private:
public:
string* pmentral;
carton* next;
// double volume();
double getheight();
carton(string pstr="paple");
carton(double heightvalue,double lengthvalue,double widethvalue,string pstr="paple",carton* next=NULL);
carton(const carton& acarton);
void getmentral(const carton& acarton);
double volume();
double showvolume();
carton* createlist();
// void showlist(carton* acarton)const;
};
carton::carton(string pstr)
{
pmentral=new string(pstr);
2011年04月17日 15点04分
2
level 6
};
carton::carton(double heightvalue,double lengthvalue,double widethvalue,string pstr,carton* next):
box(heightvalue,lengthvalue,widethvalue){
pmentral=new string(pstr);
};
void carton::getmentral(const carton& acarton)
{
cout<<*(acarton.pmentral)<<endl;
};
/*
double carton::volume()
{
cout<<"The carton volume() do!"<<endl;
return height*length*wideth;
}
*/
double carton::getheight()
{
return height;
};
double getwideth(box& abox)
{
return abox.wideth;
};
double box::getlength(box& abox)
{
return abox.length;
};
double carton::volume()
{
return 10*height*length*wideth;
};
double carton::showvolume()
{
return volume();
};
class pallent:public carton{
private:
double number;
public:
pallent(double numbervalue);
double getheight(){return height;};
double getlength(){return length;};
double getwideth(){return wideth;};
};
pallent::pallent(double numbervalue)
{
number=numbervalue;
};
carton::carton(const carton& acarton):box(acarton){
pmentral=new string(*acarton.pmentral);
// strcpy(pmentral,acarton.pmentral);
cout<<"The copy construction!"<<endl;
};
void showvolume(box& abox)
{
cout<<"The main volume is :"<<abox.showvolume()<<endl;
};
carton* carton::createlist()
{
carton* head=NULL;
carton* end=head;
carton* ps;
char ch;
cout<<"Please input the value of the carton!"<<endl;
string astring;
do{
double heightvalue=0;
double lengthvalue=0;
double widethvalue=0;
cin>>heightvalue;
cin>>lengthvalue;
cin>>widethvalue;
getline(cin,astring);
cout<<astring;
ps=new carton(heightvalue,lengthvalue,widethvalue,astring);
2011年04月17日 15点04分
3
level 6
// acarton.pmentral=&astring;
if(head==NULL)
{
head=ps;
}
else
{
end->next=ps;
}
end=ps;
cout<<"Do you want to input again?"<<endl;
cin.ignore(1024,'\n');
cin>>ch;
cout<<endl;
}while(ch=='y'||ch=='Y');
end->next=NULL;
// ps->next=NULL;
// delete ps;
return head;
};
void showlist(carton* acarton)
{
if(acarton!=NULL)
{
while(acarton!=NULL)
{
cout<<"The carton is :"<<acarton->getheight()<<" by "<<acarton->getlength()<<" by "<<acarton->getwideth()<<" by "<<*(acarton->pmentral)<<endl;
acarton=acarton->next;
}
}
cout<<"The list has over!"<<endl;
};
int main()
{
box abox(20,30,40);
carton acarton(30,40,50,"suliao");
showvolume(abox);
showvolume(acarton);
box* pabox=&abox;
// carton* pacarton=&acarton;
cout<<"The volume of the abox is :"<<abox.volume()<<endl;
cout<<"The volume of the acarton is :"<<acarton.volume()<<endl;
cout<<"The volume of the pabox is :"<<pabox->volume()<<endl;
pabox=&acarton;
cout<<"The volume of the pacarton is :"<<pabox->volume()<<endl;
/*
cout<<"The volume of the abox is :"<<abox.volume()<<endl;
cout<<"The volume of the acarton is :"<<acarton.volume()<<endl;
cout<<"The wideth of the abox is :"<<getwideth(abox)<<endl;
cout<<"The length of the abox is :"<<abox.getlength(abox)<<endl;
*/
pallent apallent(50);
// cout<<"The height of the pallent is :"<<apallent.height<<endl;
carton tcarton=acarton;
cout<<"The mentral of the acarton is :"<<endl;
acarton.getmentral(tcarton);
carton* ahead=acarton.createlist();
showlist(ahead);
return 0;
}
2011年04月17日 15点04分
4
level 6
回复:5楼
输入一个字符串,getline()第三个参数是终止字符啊,将它定义为换行‘\n’
2011年04月17日 15点04分
6
level 7
回复:6楼
getline第三个默认参数是'\n'
你是不是输的
(1)(空格)(2)(空格)(3)(空格)(换行)
那么,astring得到的是空格,
正确的
输入可能如下:
输入(1)(空格)(2)(空格)(3)(C)(P)(P)(换行)
astring得到的是CPP
2011年04月17日 16点04分
7