2011-04-24 5 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)에 동적으로 추가 하시겠습니까?

예를 분실 : 나는 그것의 확인을

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 

보도

를 계속 입력 오브젝트 IST에 추가 할 때 만약 그 고통스러운 사람이 어떻게 동적으로 추가하는 나에게 몇 가지 예제를 표시 할 수 있습니다 그것을보고하기 객체 배열 (나는 j 먼저 새로운 연산자를 써야 함) 나는 성공적으로 링크 된리스트를 사용하려고 시도하지 않았다.

답변

1

vector과 같은 동적 컨테이너를 사용하는 것이 더 쉬울 것입니다. 이렇게하면 배열에 동적으로 공간을 할당하는 것에 대해 걱정할 필요가 없습니다. 또한 이것이 숙제 인 경우에는 그것을 숙제로 표시해야합니다.

+0

글쎄 그게 내 숙제가 아니야. 내가 그걸 시도 할 것 같아. – mukesh

+0

은 데이터 형식으로 사용자 정의 데이터 형식 (클래스 CD)을 받아 들일 것이다. – mukesh

+0

네, 그것은 일반적인 컨테이너입니다. –

관련 문제