鸟类调查中的项目在代码中遇到问题.如何像数据库系统一样存储数据

Projet in birds survey having trouble in code. how to store data like database system

本文关键字:数据库系统 一样 数据 存储 何像 问题 调查 项目 遇到 代码      更新时间:2023-10-16

如何将我的输入存储在以下类别中,即水,家庭等。当我提供有关水鸟的信息时,我提供有关家禽的信息,但第二个信息覆盖了第一个信息。有没有办法存储类别方面的数据?

/* declaring header files */
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
class Bird{
    private:
        char name[50],colour[50],nature[50],location[50];
        float living_duration;
    public:
        int code;
        int set_info(){
            char name='';
            char colour='';
            char nature='';
            char location='';
            float living_duration=0.0;
        }
        int get_info(){
            cout<<"nEnter bird's name: ";
            cin>>name;
            cout<<"Colour: ";
            cin>>colour;
            cout<<"Nature: ";
            cin>>nature;
            cout<<"Location: ";
            cin>>location;
            cout<<"Living Duration: ";
            cin>>living_duration;
            cout<<"Bird's code: ";
            cin>>code;
            }
        int display_info(){
            cout<<"nBird's name: "<<name;
            cout<<"nColour     : "<<colour;
            cout<<"nNature     : "<<nature;
            cout<<"nlocation     : "<<location;
            cout<<"nLiving Duration  : "<<living_duration<<" year";
            cout<<"nCode       : "<<code;
        }
    }obj[100];
int main(){
    int i,j,k,n,m;
    do{
           cout<<"nnWhat do you want to don1.Input bird's information"
               <<"n2.Displayn3.Searchn4.Exit."
               <<"nnChoose appropriate number: ";
           cin>>n;
           switch(n){
              case 1://bird information
                 cout<<"Please Select Birds Category"<<endl;
                 cout<<"------------------"<<endl;
                 cout<<"1)Watern2)Domesticn3)preyn4)treebasedn5)flightlessn6)migratoryn"<<endl;
                 cin>>m;
                    switch(m){
                    case 1:
                        cout<<"Enter the number of bird how many to input: ";
                        cin>>j;
                        for(i=1;i<=j;i++){
                        cout<<"nInformation of Bird "<<i<<".n";
                        obj[i].get_info();
                        }
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    default:
                        cout<<"Wrong choice!!nPlease enter correct number.";
                        break;
                    }

               case 2://display
                  for(i=1;i<=j;i++)
                 {
                  cout<<"nBird no "<<i<<".n";
                  obj[i].display_info();
                 cout<<"n";
                 }
                 break;
               case 3://search
                 cout<<"nEnter the bird code: ";
                 cin>>k;
                 for(i=1;i<=j;i++)
                  {
                 if(k==obj[i].code)
                    {
                     cout<<"nBird no "<<i<<".n";
                     obj[i].display_info();
                     break;
                    }
                   }
                 if(k!=obj[i].code)
                   cout<<"Wrong code input...n";
                  break;
               case 4://exit
                  break;
               default:
                  cout<<"Wrong choice!!nPlease enter correct number.";
                  break;
             }
         }while(n!=4);
  }
您需要在鸟类

数据和数据容器之间拆分概念。

关系数据库中,您将拥有。 让表的列由结构的数据成员表示。 表的记录(行)将是记录结构的实例:

class Bird
{
public:
  std::string name;
  std::string colour;
  std::string nature;
  std::string location;
  float       living_duration;
};

对于容器或表,您可以使用std::vector
std::vector<Bird> bird_table;

许多关系数据库还包括索引表以加快搜索速度。 索引表将包含对、键(或列值)和索引到std::vector。 C++语言有一个方便的容器,称为std::map

std::map<string, unsigned int> name_index;

string 参数表示键或列类型。
unsigned int 参数表示数据库的索引(也称为外键)。

要按名称检索 Bird 记录,请先访问索引表,然后访问向量:

   unsigned int database_index = name_index["crow"];
   Bird  crow = database[index];