How to create an array of objects of another class?

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

47 Replies - 974 Views - Last Post: 08 July 2013 - 01:34 PM Rate Topic: -----

#46 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 210
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 08 July 2013 - 10:00 AM

View PostDavid W, on 08 July 2013 - 09:30 AM, said:

View Postjordannn15, on 08 July 2013 - 10:52 AM, said:

...

Okay what he showed me in the lecture is to copy the index of whatever movie the user wants to remove to used i.e. the next available spot in the array then do used--;

As for doing stuff like std::cin.sync() and std::flush I haven't learned that yet and I would put that in my code but then I think my professor would be suspicious.



> Okay what he showed me in the lecture is to copy the index of whatever movie the user wants to remove to used i.e. the next available spot in the array then do used--

Do you think that 'obscures' the one you wish to delete?

What I think he was telling you, that perhaps you misunderstood was to

0. find the index of the record you wish to remove (if it exists)

1. COPY the record you wish to remove
to the next free index spot at the end of the array
(provided that there was still room in the array ...
i.e for index 'size', where record is being moved to, and size < MAX_SIZE)

2. Copy down and over each record above the one you wish to remove
suppose j is index of record to remove and that j < size < MAX_SIZE
then:
   movies[size] = movies[j]; // copy record at index j to first spot open at end
   for( int i = j; i < size-1; ++ i)
   {
      movies[i] = movies[i+1]; // copy all above down one (and over)
   }
   -- size;



3. --size (ii.e. --used) to reflect the new top position in the array

But note:

"movies[size] = movies[j]; // copy record at index j to first spot open at end"

this above line is NOT really needed,
since, if you next were to add a record,
that info moved (above) the top index position is over-written and lost.
So, just moving records (above the one record to delete) down one index, is sufficient


Answer: Well it's an online class so the lectures are videos and he typed out and showed us how to do the remove function a little bit. He just wrote out the two lines movies[i] = movies[used - 1]; and used --;

> As for doing stuff like std::cin.sync() and std::flush I haven't learned that yet and I would put that in my code but then I think my professor would be suspicious.

Can't you look up these in a reference and quote that ref. (after you understand the info there?)
(As a student, the sooner you learn to use a ref to get relevant info of library functions, the sooner you will enjoy coding.)

One of the big (and unnecessarily big) problems of student coding in C++/C ...
is learning about the input stream (buffer)
and how to keep it ready for the the next input
so that
that input doesn't grab something
that was left over in the in stream buffer
by a previous read or read error.

The sooner a student learns how to deal with invalid (numeric) input
and how to keep the input stream buffer in the desired (most often a flushed) state ...
the sooner that student's coding ease will be ...
for many student problems.


I guess I could but as I said up there it's an online class so I have no where to quote it.

I'm almost done and all I really need now is help with writing the movies to an output file name "movies.dat" so when I run the program another time they will load again and still be there for the user to see again. I have LoadFromFile and SaveToFile functions but for some reason its not writing to the file and I can't tell if the LoadFromFile is working because it has nothing to read.
Was This Post Helpful? 0
  • +
  • -

#47 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to create an array of objects of another class?

Posted 08 July 2013 - 10:28 AM

Quote

I guess I could but as I said up there it's an online class so I have no where to quote it.


// your comments in your program are an excellent place to document stuff ...
// like ref's, etc, to help recall the info the next time you see the code
// and to give credit to a source, which is usually appreciated ...
// and sometimes necessary to keep your code 'legal'
Was This Post Helpful? 0
  • +
  • -

#48 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,401
  • Joined: 19-February 09

Re: How to create an array of objects of another class?

Posted 08 July 2013 - 01:34 PM

View Postjordannn15, on 08 July 2013 - 06:00 PM, said:

I'm almost done and all I really need now is help with writing the movies to an output file name "movies.dat" so when I run the program another time they will load again and still be there for the user to see again. I have LoadFromFile and SaveToFile functions but for some reason its not writing to the file and I can't tell if the LoadFromFile is working because it has nothing to read.


Your dvd output function only outputs (cout) to the console.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4