我这个是商品销售管理系统,我把输入的信息全部存进文件里,读取有问题
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
class Date
{
public:
Date() {}
Date(int y, int m, int d) :year(y), month(m), day(d) {}
Date(Date &a) {
year = a.year; month = a.month; day = a.day;
}
void show() {
cout << year << "年" << month << "月" << day << "日" << endl;
}
protected:
int year, month, day;
};
class Goods :public Date
{
private:
int id;
int price;
int count;
int number;
string name;
string type;
int size = 0;
//char buf[500];
//Date pubdate;
public:
Goods();
Goods(int i, string n, string t, int p, int c, int y, int m, int d) ;
Goods(Goods &b);
void menu();
void buy();
void search();
void display();
};
Goods buf[500];
Goods::Goods() {}
Goods::Goods(int i, string n, string t, int p, int c, int y, int m, int d) :Date(y, m, d) {
id = i; name = n; type = t; price = p; count = c;
}
Goods::Goods(Goods &b)
{
id = b.id; name = b.name; type = b.type; price = b.price; count = b.count;
}
void Goods::menu() {
cout << " 商品管理系统 " << endl << endl;
cout << " ******1.进购商品*******" << endl << endl;
cout << " ******2.查询库存*******" << endl << endl;
cout << " ******3.购买商品*******" << endl << endl;
cout << " ******4.查询销售记录***" << endl << endl;
cout << " ******0.退出***" << endl << endl;
cout << "请选择服务项目:";
int choose;
cin >> choose;
switch (choose)
{
case 1:buy(); break;
case 2:search(); break;
//case 3:sell(); break;
//case 4:search(); break;
case 0: {
cout << "**********谢谢使用**********" << endl;
return;
}
break;
}
}
void Goods::buy() {
char ch;
cout << "进购种类:";
cin >> number;
ofstream fout("G:\\abc\\商品销售管理.txt", ios_base::app);
//fout << "商品编号 " << "商品名称 " << "商品型号 " << "商品数量 " << "商品价格 " << "进购日期" << endl;
for (int i = 1; i <= number; i++) {
cout << "商品编号(如:1001):";
cin >> buf[size].id;
cout << "商品名称:";
cin >> buf[size].name;
cout << "商品型号:";
cin >> buf[size].type;
cout << "商品数量:";
cin >> buf[size].count;
cout << "商品价格:";
cin >> buf[size].price;
cout << "进购日期:";
cin >> year >> month >> day;
cout << endl;
size++;
fout << id << setw(14) << name << setw(14) << type << setw(12) << count << setw(13) << price << setw(12) << year << "年" << month << "月" << day << "日" << endl;
}
cout << "是否继续进购?(是Y/否N): ";
cin >> ch;
if (ch == 'Y' || ch == 'y') {
buy();
}
if (ch == 'N' || ch == 'n') {
menu();
}
}
void Goods::search() {
int n, i;
ifstream ifs("G:\\abc\\商品销售管理.txt");
while (!ifs.eof()) {
ifs.getline(buf, 500, '\n');
//cout << buf << endl;
cout << "请输入您要查询商品编号:";
cin >> n;
for (i = 0; i <= size; i++) {
if (buf[i].id == n) {
buf[i].display();
}
}
cout << sizeof(buf) << endl;
}
ifs.close();
int main()
{
Goods good;
good.menu();
system("pause");
return 0;
}