在用vc++2010运行程序出现错误无法明白,请高手们指教一二?(空间限制,预编译没有写)

[复制链接]
查看11 | 回复1 | 2011-2-18 16:40:41 | 显示全部楼层 |阅读模式
//cashier.cpp 类的实现
#include
#include
#include "cashier.h"
using namespace std;
Cashier::Cashier(char *pname,float pri,int pc)
{ name=new char[strlen(pname)+1];
strcpy(name,pname);
price=pri;
count=pc;
}
Cashier::Cashier(const Cashier & init)
//拷贝构造函数
{ name=new char[strlen(init.name)+1];
strcpy(name,init.name);
price=init.price;
count=init.count;
}

Cashier & Cashier:: operator=(const Cashier & q) //重载赋值运算符
{ delete [] name;
name=new char[strlen(q.name)+1];
strcpy(name,q.name);
price=q.price;
count=q.count;
return *this;
}

Cashier::~Cashier()
//析构函数
{delete [] name;}
void Cashier::input()
//输入成员数据
{ cout>name;
cout>price;
cout>count;
}
float Cashier::total()
//计算总和

{return price*count;
}
void Cashier::print()
//打印输出
{cout.width(10);
cout.fill(' ');
cout.setf(ios::left);
cout
#define n 20
#include "cashier.h"
using namespace std;
void main()
{ Cashier a[n];
//创建Cashier的类对象数组a[n]
int i=0;
int flag=1,tag=1; //设置状态变量
float sum=0.0,pay;
char c;
while(tag&&flag)
{

while((c=getchar())!='\n'||i>pay;
cout<<"找零:"<<pay-sum;
while(tag)
//状态变量tag为真时,执行循环体
{cout<<"按enter键重新开始输入或者按esc键退出";

if((c=getchar())=='\n')
//按enter键重新开始输入,设置状态变量tag=0退出本循环,flag=1则执行外循环,重新输入

{tag=0;flag=1;}

else if((c=getchar())==027)
//按esc键,ASCII码是027.设置状态变量tag=0,flag=0.退出外循环

{tag=0;flag=0;}

else tag=1;


}
}
}

回复

使用道具 举报

千问 | 2011-2-18 16:40:41 | 显示全部楼层
具体调试自己弄吧……下面是我觉得的问题:1、在实现operator=()时没有考虑自我赋值的情况(不严密,但不影响此次实验结果)2、input()函数中不能保证name被正确赋值3、while((c=getchar())!='\n'||i<n)//输入空格键或超过最大商品数则停止输入。这句话矛盾,安根据我的理解,将或运算改为与运算4、 for(int j=0;j<=i;j++) //遍历对象数组,输出商品信息。循环结束的条件错误,比如i==n成立时,则循环内a[j]会超出范围5、while(tag)循环内里面的语句:其一、将c=getchar()放到第一个 if 前面,因为c这个变量后面还需要用到,或者删除第二个if 中的c
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行