2017-10-17 12 views
-8

私はプログラミングでは新機能、配列ではweakです。ここでコーディングと質問があります。間違いがあった場合は私に見せてください。あなたのstructC++配列 - 構造体の動的配列を作成するemployeeとdisplay

Question

#include<iostream> 
#include<string> 
using namespace std; 

struct Library 
{ 
    string Name[10]; 
    int ID[10],Unit[10]; 
    double Price[10]; 
}; 

struct Library L; 
int main() 
{ 
    int book; 
    cout<<"Enter the number of book : "; 
    cin>>book; 
    for(int i=0; i<book; i++) 
    { 
    cout<<"\nBook Name : "; 
    cin>>L.Name[i]; 
    cout<<"Book ID : "; 
    cin>>L.ID[i]; 
    cout<<"Unit : "; 
    cin>>L.Unit[i]; 
    cout<<"Price : "; 
    cin>>L.Price[i];  
    } 
    cout<<"You have entered these info : "; 
    cout<<"\nName \t ID \t Unit Price"; 
    for(int i=0; i<book; i++) 
    { 
    cout<<"\n"<<L.Name[i]<<endl; cout<<"\t"<<L.ID<<"\t"<<L.Unit<<"\t"<<L.Price<<endl; 
    } 

} 
+2

あなたは**それを試みたことがありますか?それは動作しましたか? – Steve

+0

名前を除くすべてエラーコード(0x4a7090)を表示 – Zeshon

+0

@ Zeshon私はそれが構造型のオブジェクトの動的に割り当てられた配列(割り当てられていない)はライブラリと名前を付けるべきであり、構造そのものはBookという名前にするべきだと思います。 –

答えて

0

小さなエラーを修正しました。構造体の配列に1行だけ動的にメモリを動的に割り当てました。 https://ideone.com/5KUKkV

は、あなたが本の数の入力を取る後は、あなたがこれをしなければならない:

は、ここでは試してみてください

struct Library *L = new Library[book]; 

そして、あなたはあなたの構造体の各メンバにアクセスする方法はL[i].ID, L[i].Nameなどです。 。

また、構造体メンバーが無効です。コードの正しさを参照してください。

完全なコードは:

struct Library 
{ 
    string Name; 
    int ID, Unit; 
    double Price; 
}; 


int main() 
{ 

    int book; 
    cout << "Enter the number of book : "; 
    cin >> book; 

    struct Library *L = new Library[book]; 

    for (int i = 0; i<book; i++) 
    { 
     cout << "\nBook Name : "; 
     cin>>L[i].Name; 
     cout << "Book ID : "; 
     cin >> L[i].ID; 
     cout << "Unit : "; 
     cin >> L[i].Unit; 
     cout << "Price : "; 
     cin >> L[i].Price; 
    } 
    cout << "You have entered these info : "; 
    cout << "\nName \t ID \t Unit Price"; 
    for (int i = 0; i<book; i++) 
    { 
     cout << "\n" << L[i].Name << endl; cout << "\t" << L[i].ID << "\t" << L[i].Unit << "\t" << L[i].Price << endl; 
    } 

} 
0

よりもむしろアレイを、私は(あなたの質問のタイトルにと割り当てのように)構造体の配列を示唆:

struct Library 
{ 
    string Name; 
    int ID; 
    int Unit; 
    double Price; 
}; 

あなたが使用する必要がある場合動的メモリ割り当てを使用すると、コレクションを次のように作成できます。

Book * Collection = new Library[10]; 
0

構造体のベクトルを使用:

#include<iostream> 
#include<string> 
using namespace std; 

struct Library 
{ 
    string Name; 
    int ID,Unit; 
    double Price; 
}; 

int main() 
{ 
    vector<Library> L; 
    Library tempL; 
    int book; 
    cout<<"Enter the number of book : "; 
    cin>>book; 

    for(int i=0; i<book; i++) 
    { 
    cout<<"\nBook Name : "; 
    cin>>tempL.Name; 
    cout<<"Book ID : "; 
    cin>>tempL.ID; 
    cout<<"Unit : "; 
    cin>>tempL.Unit; 
    cout<<"Price : "; 
    cin>>tempL.Price;  
    l.push_back(tempL); 
    } 
    cout<<"You have entered these info : "; 
    cout<<"\nName \t ID \t Unit Price"; 
    for(int i=0; i<L.size(); i++) 
    { 
    cout<<"\n"<<L[i].Name<<endl; 
    cout<<"\t"<<L[i].ID<<"\t"<<L.Unit<<"\t"<<L[i].Price<<endl; 
    } 
} 
+0

あなたの変更が割り当ての* "ダイナミックアレイ" *部分を満たしていないと悪いです。これは、 'new'と配列を使って動的メモリ割り当てに関する教訓になるかもしれません。 –

+0

また、人々の宿題をしないことをお勧めします。代わりに、彼らにヒントを与えたりガイドしたりしてください。あなたが宿題をするなら、彼らは学ばないでしょう。 –