its part of a dvd program. i want all the objects created and manipulated by the program to be stored in a dynamic array that might grow during the execution of the program. I want to program to store the info in a dynamic array that i can call anywhere to list the info. im having trouble with using dynamic memory and classes together, ...please help, thanks

class Names 
{ 
private: 
string title;
int i;
int * p; 

public: 
void enter_new();  // sets names of actors to the DVD 
}film; 


void Names::enter_new()
{char ans;
cout << "How many movies would you like to type? ";
cin >> film.i;
int ii= film.i;
(film.p)= new int[ii];

  if ((film.p) == 0)
    cout << "Error: memory could not be allocated";
  else
  {
   for (int a=1; a<(ii); a++)
    {cout<<"\n Please enter the name: "<<endl; 
      cin >> (film.p)[a];
   }
    delete[] p;
  }

You're using strings, why not use vectors too? Much easier.

i guess, easier for programmers, im just a beginner, elobrate a bit please?ill try with pointers tho, but im still confused abt my original question

im having trouble with using dynamic memory and classes together

,

>i guess, easier for programmers, im just a beginner
Easier for beginners too because you don't have to worry about managing the memory.

>im having trouble with using dynamic memory and classes together
Compare and contrast:

void Names::enter_new()
{
  cout<<"How many movies would you like to type? ";
  cin>> i;

  p = new int[i];
 
  for ( int a = 0; a < i; a++ ) {
    cout<<"\n Please enter the name: "; 
    cin>> p[a];
  }
}

You're not just working with the film object in a member function, you're working with any possible object of the Names class. Also, you want the memory to persist until the object is destroyed, which means not deleting it until the destructor is called. Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.

ya i tried that wrote before, it had some wierd looping problem...workin on it.

Easier for beginners too because you don't have to worry about managing the memory.

ya, i have to manage it, its a requirment.

Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.

ya i knw, im using nothrow

>ya, i have to manage it, its a requirment.
I'm glad you mentioned that beforehand. :icon_rolleyes:

>ya i knw, im using nothrow
I'm glad you posted the actual code you're using. :icon_rolleyes:

On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.

I'm glad you mentioned that beforehand. :icon_rolleyes:

That wasnt my problem, read the post again smrt..

I'm glad you posted the actual code you're using. :icon_rolleyes:

and my codes is very long...and i added nothrow after i posted
AND THAT STILL WASNT MY QUESTION!

On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.

think again! u hvnt answered any of my questions physc...

i asked for help not ur ''smart'' remarks

anyone please help. how do i use dynamic memory and classes together in my code?

>That wasnt my problem, read the post again smrt..
I guess I'm not smart enough. Please point out where you said a dynamic array was required. The closest you got was saying "I want", which clearly says that you have a choice. Therefore, I suggested a better choice.

>and my codes is very long
Then make it shorter for posting. There's a sticky at the top of this forum that tells you how to post like an intelligent human being.

>and i added nothrow after i posted
Then you should say "thanks" and leave it because my comment was accurate when you posted. As I said, I'm not psychic, so I don't know what changes you made after you posted.

>think again! u hvnt answered any of my questions physc...
BS. Maybe you should take some lessons in reading for comprehension, because the fourth post in this thread answers your question fully. If it doesn't, you asked the wrong question.

Now, on a side note, I'm going to ask you to use proper spelling and grammar in your posts and proofread them before hitting the "Submit Reply" button. Your silly abbreviations are perilously close to breaking our Keep-It-Clean rule.

you are trying to access a private member with your object in the main function.

cin >> film.i;

is not supposed to work cos its film is trying to access a member( i ) that has been declared private in the class.

>you are trying to access a private member with your object in the main function.
No, he's not. He's trying to access a private member of an object from within a public member function of the same class. Note the function definition:

void Names::enter_new()

Unless the rules have recently changed without my knowing, that's not a correct definition of main. The access is legal.

Here's an example of what I mean, m3an, since words don't seem to have sufficient effect. I've corrected your errors and added enough to "use dynamic memory and classes together":

#include <iostream>
#include <string>
#include <new>

using namespace std;

class Names { 
private: 
  string title;
  string *p;
  int i;
public:
  Names ( const string& init ): title ( init ) {}
  ~Names() { delete[] p; }
  void enter_new();
  void display();
}; 

void Names::enter_new()
{
  cout<<"How many movies would you like to type? ";
  cin>> i;
  cin.ignore();

  p = new(nothrow) string[i];

  if ( p == 0 )
    cout << "Error: memory could not be allocated";
  else {
    for ( int a = 0; a < i; a++ ) {
      cout<<"Actor "<< a + 1 <<": "; 
      getline ( cin, p[a] );
    }
  }
}

void Names::display()
{
  cout<<"Title: "<< title <<"\nActors:\n";

  for ( int a = 0; a < i; a++ )
    cout<<'\t'<< p[a] <<'\n';
}

int main()
{
  Names film ( "My film" );

  film.enter_new();
  film.display();
}

Have I still not answered your question?

Were you referring to something else mate?

>I guess I'm not smart enough. Please point out where you said a dynamic array was required. The closest you got was saying "I want", which clearly says that you have a choice. Therefore, I suggested a better choice.
wel i did not blame you for that, i mearly pointed out the fact. since my original post was not clear enough for you.


>Then make it shorter for posting. There's a sticky at the top of this forum that tells you how to post like an intelligent human being.
i did make it short for posting. look at my post....


>Then you should say "thanks" and leave it because my comment was accurate when you posted. As I said, I'm not psychic, so I don't know what changes you made after you posted.
well i added nothrow bofore you suggested, so i was just letting u know. But thanks anyways.


>BS. Maybe you should take some lessons in reading for comprehension, because the fourth post in this thread answers your question fully. If it doesn't, you asked the wrong question.
thats funny. Read the first line in the 5th post and then see who should take lessons.

>Read the first line in the 5th post and then see who should take lessons.
I still think it's you. You see, you had some weird looping problem, but my code worked perfectly. So, since I answered the question you asked, and your broken attempt at my answer didn't work, I didn't answer your question? That's some good logic, and it explains why you can't write working code. Please don't take your incompetence out on me.

>Have I still not answered your question?

You have answered my question. Thanks
I was not looking for a handout if that was the impression.
I want to understand what i am doing. Your code is helpful.
Programming can be frustrating sometimes, and your remarks didnt help at all.....but i do apologize.

>Read the first line in the 5th post and then see who should take lessons.
I still think it's you. You see, you had some weird looping problem, but my code worked perfectly. So, since I answered the question you asked, and your broken attempt at my answer didn't work, I didn't answer your question? That's some good logic, and it explains why you can't write working code. Please don't take your incompetence out on me.

I didnt say your code wasnt right....wheres the quote???.

ok thats nice...you really dont deserve an apology..bch

how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.

how did i b'come a part of this !!! gggggggeeeeeeeeesssssssshhhhhhh.

that was not for u :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.