2011-04-24 47 views
0
#include <iostream> 
#include <cstring> 

using namespace std; 

class CD 
{ 

public : 
    void getCDdetails(CD *); 
    void printCDD(CD *) const; 
    int putCount() const 
    { 
    return count; 
    } 

private : 
static int count; 
char Title[60]; 
char Artist[60]; 
char Type; 
int Tracks; 
}; 


int CD :: count=0; 

void CD :: getCDdetails(CD * cd) 
{ 
cd=new CD[putCount() + 1]; //THIS LINE IS NOT HELPING 
    for(int j=putCount(); j < 100; j++) 
    { 
    cout << "\nTitle :"; 
    cin >> cd[j].Title; 
    cout << "\nArtist :"; 
    cin >> cd[j].Artist; 
    cout << "\nType :"; 
    cin >> cd[j].Type; 
    cout << "\nTracks"; 
    cin >> cd[j].Tracks; 
    count++; 
    cout << "Details added, Press enter to continue"; 
    fflush(stdin); 
    getchar(); 
    break;; 
    } 
    printCDD(cd); 
} 


void CD :: printCDD(CD * cd) const 
{ 

cout << "\nThere are " << putCount() << " number of CD details in the database at present"; 
    for(int j=0; j < putCount(); j++) 
    { 
    cout << "\nTitle : " << cd[j].Title; 
    cout << "\nArtist : " << cd[j].Artist; 
    cout << "\nType : " << cd[j].Type; 
    cout << "\nTracks " << cd[j].Tracks << endl; 
    } 
    fflush(stdin); 
    cout << "\nPress enter to continue"; 
    getchar(); 
} 


void display_menu(); 

int main() 
{ 
display_menu(); 
return 0; 
} 

void display_menu() 
{ 
CD * st; 
int choice; 
int j; 
for(;;) 
{ 
system("clear"); 
cout << "\n\t\t\t\tCurrent number of CDs in the CD database is " << st[0].putCount(); 
cout << "\n1:Enter a CD detail" \ 
    << "\n2:Print all the CD details" \ 
    << "\n3:Quit" \ 
    << "\nEnter your choice : "; 
cin >> choice; 
    switch(choice) 
    { 
    case 1 : 
     st[0].getCDdetails(st); 
      break; 
    case 2 : 
      st[0].printCDD(st); 
      break; 
    case 3 : 
      exit(0); 
    default : 
      cout << "\nPlease enter valid choice"; 
      fflush(stdin); 
      getchar(); 
      break; 
    } 
    }   
} 

這行(cd = new CD [putCount()+ 1];)沒有幫助,因爲每次我將數據添加到對象數組時,可能會分配新的內存塊,並且先前添加的數據迷路要在對象數組(CD)中動態添加?

例如:當我加入到IST對象的確定

Current number of CDs in the CD database is 0 
    1:Enter a CD detail 
    2:Print all the CD details 
    3:Quit 
    Enter your choice : 1 
    Title :a 
    Artist :a 
    Type :a 
    Tracks1 
    Details added, Press enter to continue 

There are 1 number of CD details in the database at present 
Title : a 
Artist : a 
Type : a 
Tracks 1 

Press enter to continue 

1:Enter a CD detail 
2:Print all the CD details 
3:Quit 
Enter your choice : 1 
Title :a 
Artist :a 
Type :a 
Tracks1 
Details added, Press enter to continue 

There are 1 number of CD details in the database at present 
Title : a 
Artist : a 
Type : a 
Tracks 1 

        Press enter to continue 

        Current number of CDs in the CD database is 1 
1:Enter a CD detail 
2:Print all the CD details 
3:Quit 
Enter your choice : 1 
Title :b 
Artist :b 
Type :b 
Tracks2 
Details added, Press enter to continue 

There are 2 number of CD details in the database at present 
**Title : 
Artist : 
Type : 
Tracks 0** //ITS NOT DISPLAYING THAT WAS ADDED JUST BEFORE 

Title : b 
Artist : b 
Type : b 
Tracks 2 

按回車鍵繼續

如果它痛苦地看着它有人可以告訴我一些例子,如何動態地添加到一個對象數組(我是j首先嚐試使用new運算符),如果成功,我還沒有嘗試鏈表,我會繼續並使用鏈表。

回答

1

您可能會更容易使用像vector這樣的動態容器。這樣您就不必擔心爲陣列動態分配空間。此外,如果這是家庭作業,你應該把它標記爲這樣。

+0

那麼它不是我的作業我只是盯着我會嘗試 – mukesh 2011-04-24 14:55:37

+0

將矢量接受用戶定義的數據類型(CD類)作爲其數據類型 – mukesh 2011-04-24 15:28:36

+0

是的它是一個通用的容器。 – 2011-04-24 16:15:03