mysql POS机的简单vs2013代码 及所遇问题解析

这是一个适合初学者的MySQL POS机程序示例,包括用户类(Domestic, Manage, Root)和商品操作。文章指出在代码执行过程中遇到的问题,如历史输入残留、strcat_s函数中断、mysql_fetch_row理解难度、内存分配和析构函数的疑惑,以及头文件重复包含等。此外,还讨论了结构体与类的选择、构造函数和析构函数的编写规范等编程实践。" 124533879,11258636,C语言深入解析:数组完全指南,"['C语言', '数组操作', '编程基础']
摘要由CSDN通过智能技术生成

源码如下:


//user.h头文件

#include "windows.h"
#include "iostream"
#include "string.h"
#include "mysql.h"
using namespace std;


//货物的属性
struct Goods{//用结构体  还是用类啊??
// Goods();
// ~Goods();
char* G_ID;
char* G_name;
char* G_amount;
char* G_selling_price;
char* manufacture;
char* discount;
};


/*普通用户的类*/
class Domestic{
public:
Domestic();
// ~Domestic();
char* putin(MYSQL*);//输入“ID”或“名字”,来返回一个字符串,
char* find(MYSQL*,int);//根据putin()的返回值 来找商品并显示
void inumber(MYSQL*);//输入数量,并打印总额
};


/*管理用户的类*/
class Manage :public Domestic{
public:
void add_Domestic(MYSQL*);
void del_Domestic(MYSQL*);
void add_Goods(MYSQL *);
void del_Goods(MYSQL *);
void modify_Goods(MYSQL *);
// void inquiry_G_buying_price(char *);//进价就不弄了
// void modify_discount(float);
protected:
// float G_buying_price;
};


class Root :public Manage{
public:
void add_Manage(MYSQL*);
void del_Manage(MYSQL*);
void inquiry_total(MYSQL*);
void inquiry_user(MYSQL*);
};
bool is_number(string str);


//fuction.h头文件

#include "user.h"


void ROOT(MYSQL*);
void MANAGE(MYSQL*);
void DOMESTIC(MYSQL*);


//user.cpp代码

#include "user.h"


Goods  *goods;
float sum = 0;
/********************************普通用户**********************************/
/*初始化*/
Domestic::Domestic()//到底需要不??
{
goods = (Goods *)malloc(sizeof(Goods));
goods->G_ID = (char *)malloc(20);
goods->G_name = (char *)malloc(100);
goods->G_amount = (char *)malloc(20);
goods->discount = (char *)malloc(20);
goods->G_selling_price = (char *)malloc(20);
goods->manufacture = (char *)malloc(100);


memset(goods->G_ID, 0, 20);
memset(goods->G_name, 0, 100);
memset(goods->G_amount, 0, 20);
memset(goods->discount, 0, 20);
memset(goods->G_selling_price, 0, 20);
memset(goods->manufacture, 0, 100);





// goods->discount = "1";
}


/*析构函数*//*析构函数这块会有中断,有错误
Domestic::~Domestic()
{
free(goods->manufacture);
free(goods->G_selling_price);
free(goods->discount);
free(goods->G_amount);
free(goods->G_name);
free(goods->G_ID);
free(goods);
}*/


/*输入查找的ID或商品名*/
char * Domestic::putin(MYSQL* mysql)
{
char* str;
str = (char *)malloc(5);
memset(str, 0, 5);
int j = 0;
int result = 0;
cout << "输入商品信息:\n";
cin >> str;
if (is_number(str))
{
goods->G_ID = str;
result = 1;
// return 1;//1 表示输入数据为 ID
}
else
{
goods->G_name = str;
// return 0;//0表示输入为 名字
}
return find(mysql, result);
}


/*查找对应的商品并打印其信息*/
char * Domestic::find(MYSQL* mysql,int f)//f表示查找输入的是ID 还是名字
{
MYSQL_RES *res;


MYSQL_ROW row;
row = (MYSQL_ROW)malloc(sizeof(MYSQL_ROW));
memset(row, 0, sizeof(MYSQL_ROW));


char *buff;
buff = (char *)malloc(500);
memset(buff, 0, 500);


strcat_s(buff, sizeof("select G_ID,G_name,G_amount,G_selling_price,manufacture,discount from Goods where "), "select ID,Name,Amount,Selling_price,Manufacture,Discount from Goods where ");

if (f == 1)
{
strcat_s(buff, 100 + sizeof(buff) + sizeof("G_ID = "), "ID = ");//中间其实是合并后的大小
strcat_s(buff, 100 + sizeof(buff)+sizeof(goods->G_ID), goods->G_ID); 
mysql_query(mysql, buff);


res = mysql_store_result(mysql);
row = mysql_fetch_row(res);


if (row == 0)
cout << "没有此商品!" << endl;
else
cout << "货物编号:" << *row << endl << "货物名称:" << *(row + 1) << endl << "货物存货:" << *(row + 2) << endl << "货物单价:" << *(row + 3) << endl << "货物厂家:" << *(row + 4) << endl << "货物折扣:" << *(row + 5) << endl;
}
if (f == 0)
{
strcat_s(buff, sizeof("G_name = Goods->G_name"), "G_name = Goods->G_name");
mysql_query(mysql, buff);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值