簡體   English   中英

如何為商店數據庫(OOP / C ++)添加名稱字段

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

好的,所以我制作了一個數據庫類型程序,並創建了商品編號和價格字段。 我還嘗試通過使用“項目編號”字段和“價格”兩者的數組的索引值來堅持主鍵。 但是想知道如何在其中添加“項目名稱”字段以使其正常工作。 我嘗試過考慮添加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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM