2017-01-25 3 views
1

水、鳥類などの情報を入力するときは、水鳥に関する情報を入力すると、家禽についての情報を提供します。しかし、2番目の情報は最初の情報よりも優先されます。どのような方法は、賢明な賢明なデータを格納するには?コードで問題を抱えている鳥調査のプロジェット。データベースシステムのようにデータを保存する方法

/* 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='\0'; 
      char colour='\0'; 
      char nature='\0'; 
      char location='\0'; 
      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<<"\n\nWhat do you want to do\n1.Input bird's information" 
       <<"\n2.Display\n3.Search\n4.Exit." 
       <<"\n\nChoose appropriate number: "; 
      cin>>n; 

      switch(n){ 
       case 1://bird information 
       cout<<"Please Select Birds Category"<<endl; 
       cout<<"------------------"<<endl; 
       cout<<"1)Water\n2)Domestic\n3)prey\n4)treebased\n5)flightless\n6)migratory\n"<<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); 
    } 
+0

あなたは* *配列はゼロベースであることを知っていますか?したがって、100個の要素(グローバル変数 'obj'など)の配列には、0から99までのインデックスが含まれます。 –

答えて

1

あなたは鳥データとデータのコンテナの間、あなたの概念を分割する必要があります。

リレーショナルデータベースでは、テーブルがあります。テーブルの列を構造体のデータメンバーで表現させる。あなたがstd::vectorを使用することができ、コンテナまたはテーブルの場合

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


​​

多くのリレーショナルデータベースもインデックステーブルを含めるテーブルのレコード(行)は、レコード構造のインスタンスになりますを使用して検索を高速化します。インデックステーブルには、ペア、キー(または列の値)、およびインデックスがstd::vectorに含まれます。

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

stringパラメータはキーまたは列のタイプを表し:C++言語はstd::mapと呼ばれる便利なコンテナを持っています。
unsigned intパラメータは、データベースへのインデックス(外部キー)を表します。

あなたが最初のインデックステーブルにアクセスする名前で鳥のレコードを取得するには、その後、ベクトル:

unsigned int database_index = name_index["crow"]; 
    Bird crow = database[index]; 
関連する問題