读书人

求大神指导C++ 编译求通过,该怎么解决

发布时间: 2012-03-25 20:55:16 作者: rapoo

求大神指导C++ 编译求通过
#include <iostream>
using namespace std;
#include <string.h>

class InvoiceItem
{
private:
char *p;
int n;
float costprice;
float unitprice;
float all;
float tax;
float total;
public:
InvoiceItem(char *,float);
void cal();
void plus(int);//没写析构函数
int getn(){return n;}
float getc(){return costprice;}
float getu(){return unitprice;}
float gettotal(){return total;}
float getall(){return all;}
float gettax(){return tax;}
char *getname(){return *p;}*/
};
InvoiceItem::InvoiceItem(char *name,float cost)
{
p=new char[strlen(name+1)];
strcpy(p,name);
costprice=cost;
unitprice=cost*1.3;
}

void InvoiceItem::cal()
{
all=n*unitprice;
tax=all*0.06;
total=all+tax;
}

void InvoiceItem::plus(int num)
{
while(1)
{
if(num>=0)
{
n=num;
break;
}
else
{
cout<<"购买的商品数量不能为负数!"<<endl;
cin>>num;
}
}
}
void main()
{
InvoiceItem item[5]={
InvoiceItem("computer",4999),
InvoiceItem("book",40),
InvoiceItem("medicine",10),
InvoiceItem("food",5),
InvoiceItem("fruit",20)
};
int num,i,flag=1,n=0;
float allprice=0,tax=0;
char name[30],select;
cout<<"本店商品列表:"<<endl;
for(i=0;i<5;i++)
{
cout<<"商品名:"<<Item[i].getname()<<"\t成本价:"<<Item[i].getc()<<"\t单价:"<<Item[i].getu()<<endl;
}
while(1)
{
cout<<"请输入要购买的商品的名称:"<<endl;
while(1)
{
cin.getline(name,30);
cin.ignore();
for(i=0;i<5;i++)
{
if(strcmp(Item[i].getname(),name)==0)
{
flag=0;
index[n++]=i;
}
}
if(flag=0) break;
else
{cout<<"输入的商品不存在!请重新输入!"<<endl;}
}
cout<<"请输入购买的数量:"
cin>>num;
Item[index[n-1]].plus(num);
Item[index[n-1]].cal();
cout<<"您是否要继续购买?Y/N"
while(1)
{
cin>>select;
if(select=='Y'||select=='N') break;
else
cout<<"输入不正确 请重新输入"<<endl;
}
if(select=='N')
break;
}
//下面输出小计
cout<<"本次交易如下:"<<endl;
for(i=0;i<5;i++)
{
if(item[i].getn()!=0)
{
cout<<"商品名:"<<item[i].getname()<<"\t数量:"<<item[i].getn()<<"\t小计:"<<item[i].gettotal()<<endl;
allprice+=item[i].getall();
tax+=item[i].gettax();
}
}
cout<<"总价(不含税):"<<allprice<<endl;
cout<<"总价(含税)"<<(allprice+tax)<<endl;
cout<<"税款:"<<tax<<endl;*/
}

[解决办法]

C/C++ code
public:    InvoiceItem(char *,float);    void cal();    void plus(int);    int getn(){return n;}    float getc(){return costprice;}    float getu(){return unitprice;}    float gettotal(){return total;}    float getall(){return all;}    float gettax(){return tax;}    char *getname(){return p;}//这儿返回p,而不是*p};
[解决办法]
去掉所有的 */
Item改为item
剩下的 index 不知是什么东西,自己找去吧

读书人网 >C++

热点推荐