如何为商店数据库(OOP/C++)添加名称字段

How to add a name field for a shop database (OOP/C++)

本文关键字:C++ 添加 字段 OOP 数据库      更新时间:2023-10-16

好的,所以我制作了一个数据库类型的程序,并创建了一个商品编号和价格字段。我还试图通过使用"itemnumber"字段和"price"的数组的索引值来坚持主键。但我想知道如何在添加"项目名称"字段的同时使其工作。我试着想出了很多不同的方法来添加char类型的数组,但这并没有真正奏效。

#include<iostream>
using namespace std;
class shop
{
    private:
        int i,n, item[20];
        float price[20];
    public:
        void input();
        void output();
};
void shop::input()
{
    cout<<"Enter the number of items: ";
    cin>>n;
    for (i = 1; i <= n; i++)
    {
        cout << "Enter the item number of the " << i << " item: ";
        cin >> item[i];
        cout << "Enter the price of the item: ";
        cin >> price[i];
    }
}
void shop::output()
{
    for (i = 1; i <= n; i++)
    {
        cout << "Item Number: " << item[i] << endl;
        cout << "Price: " << price[i] << endl << endl;
    }
}
void main()
{
    class shop s;
    s.input();
    s.output();
}

创建一个类或结构来保存数据:

struct Item
{
    int id;
    float price;     // float may lead to rounding errors.
                     // I would use int and store cents
    //string name;
    // If you really want to use c-style string
    char name[20];
    // or
    // char *name;   // You would need to use new with this
};

然后保留一个数组:

class shop
{
    private:
        Item items[20];
        // As mentioned, better than a static array would be a vector
        //std::vector<Item> items;
    public:
        void input();
        void output();
};

字符串向量怎么样?

#include<iostream>
#include <string>
#include <vector>
using namespace std;
class shop
{
    private:
        int i,n, item[20];
        float price[20];
        vector<string> names;
    public:
        void input();
        void output();
};
void shop::input()
{
    cout<<"Enter the number of items: ";
    cin>>n;
    names.resize(n);
    for (i = 1; i <= n; i++)
    {
        cout << "Enter the item number of the " << i << " item: ";
        cin >> item[i];
        cout << "Enter the price of the item: ";
        cin >> price[i];
        cout << "Enter the name of the item: ";
        cin >> names[i];
    }
}
void shop::output()
{
    for (i = 1; i <= n; i++)
    {
        cout << "Item Number: " << item[i] << endl;
        cout << "Price: " << price[i] << endl << endl;
        cout << "Name: " << names[i] << endl << endl;
    }
}

最好有一个析构函数来清理向量占用的内存。

我会使用std::string

#include <iostream>
#include <string>
using namespace std;
class shop
{
    private:
        int i,n, item[20];
        float price[20];
        string name[20];
    public:
        void input();
        void output();
};
void shop::input()
{
    cout<<"Enter the number of items: ";
    cin>>n;
    for (i = 1; i <= n; i++)
    {
        cout << "Enter the item number of the " << i << " item: ";
        cin >> item[i];
        cout << "Enter the name of the item: ";
        cin >> name[i];
        cout << "Enter the price of the item: ";
        cin >> price[i];
    }
}
void shop::output()
{
    for (i = 1; i <= n; i++)
    {
        cout << "Item Number: " << item[i] << endl;
        cout << "Item Name: " << name[i] << endl;
        cout << "Price: " << price[i] << endl << endl;
    }
}
int main()
{
    class shop s;
    s.input();
    s.output();
}