I am having trouble with this part of my program assignment. I have created and compiled the three separate implementation files that design the three classes, they each have a default constructor, overloaded constructor, and a display function.

//************************************************************************************************************
"When your program is working correctly, define an array of pointers to Ship objects to store pointers of up to 15 Ship, CruiseShip or FreightShip objects. Then read in the data from the file pgm8.txt. For each set of ship data it contains a code to indicate whether the data that follows is for a "S"hip, "C"ruise ship or "F"reight ship.
- If the code is "S" then the code is followed by the ship's name and year.
- If the code is "C" then the date of departure, maximum number of passengers and number of passengers signed up for the cruise are also included in the data file.
- If the code is "F" then the code is followed by the ship's name, year and freight capacity.

Use the new operator to create either a Ship, CruiseShip or FreightShip object, storing the pointer to the object in the next element of the array."

I have iincluded my main.cpp file as well as the external file that is to be read in.
//********************************************************************************************************

#include <iostream>
#include <string>
#include <fstream>

#include "pgm8-ship.h"
#include "pgm8-CruiseShip.h"
#include "pgm8-FreightShip.h"

using namespace std;

int main ()
{
   /*
   Ship ship1("Dauntless", 2003);
   CruiseShip cruise1("Titanic", 1912, "4/10/12", 2500, 1324);
   FreightShip freight1("Black Pearl", 1800, 50000);

   ship1.displayShip();
   cout << endl;
   cruise1.displayShip();
   cout << endl;
   freight1.displayShip();
   */

   string shipName, departure;
   int yearBuilt, maxPass, signPass, capacity;

   const int NUM_SIZE = 15;
   string code;

   Ship *shipObjs[NUM_SIZE];

   ifstream infile;
   infile.open("pgm8.txt");

   while(!infile.eof())
   {
   }



   return 0;
 }

External file information:

S
Beagle
1820
C
Carnival Conquest
2002
5/7/2012
2974
1500
F
Aristote
2007
18860
C
Jewel of Seas
2004
5/16/2012
2500
2200
S
Bounty
1789
F
Herodote
2001
25000
C
Crown Princess
2006
5/22/2012
3080
3000
F
Zim Ontario
2009
63300
C
Oasis of Seas
2009
4/5/2012
5400
4800

I would appreciate any help, hints, tips, or suggestions. Thank you!

Recommended Answers

All 4 Replies

Well English is not my first language but still I think I could not understand what your problem is?

I think this is where you are facing difficulty. I dont see the use of new keyword here which is fairly easy.

Use the new operator to create either a Ship, CruiseShip or FreightShip object, storing the pointer to the object in the next element of the array."

I am having trouble with this part of my program assignment.

It would appear you have created and tested each of the classes to make sure they work correctly. This is evident by the comment block for the testing of each ADT class that you made. I would assume that you had made each class so as they would create an instance of each class and display that instance to the screen. This is all assumed that you've gotten to this part of the programming.

What you need to do next is to work on the while loop. By the look of it, you want to continually read from the file until you have reached to the end of the file. The pseudocode that I would suggest includes the following:

while(!end of file)    //read in the next character
        infile >> getNextChar;

        if(getNextChar equals Cruise){
            get firstDateInteger;
            get secondDateString;
            get firstNumberInteger;
            get secondNumberInteger;
            create new CruiseInstance(firstDateInteger, secondDateInteger, firstNumberInteger, secondNumberInteger);
            add CruiseInstance to dynamic array
        } else if(getNextChar equals Ship){
            get NameString;
            get firstInteger
            create new ShipInstance(NameString, firstInteger);
            add ShipInstance to dynamic array
        }else if(getNextChar equals Freight){
            get firstNameString;
            get yearInteger
            get secondInteger
            create new FreightInstance(firstNameString, yearInteger, secondInteger);
            add FreightInstance to dynamic array
        } else {
            output to screen "Invalid input character. The program will now exit"
            Exit program
        }
}  // end while loop

... the above pseudocode is basically what you are looking to do. You want to read in the first character, what type of instance you need to make, grab all the information for that particular instance, -then- create a new instance and add all the required information to that object as it is being created. Once the object is created, you then want to add it to the dynamic array so that you do not lose that new instance once you attempt to reloop through the while loop.

I understand what to do, I just don't understand the proper syntax for the warray of pointers and how to read in the code to each array element. Thank you for the help though.

Use the new operator to create either a Ship, CruiseShip or FreightShip object, storing the pointer to the object in the next element of the array."

When making a new object, collect the data that you need and make a new object. Like this.

Soda *newSoda = new Soda("Coca Cola", 20, 1.49);

This states that a type Soda, named newSoda, will be newly created (on the heap) with the parameters of "Coca Cola", 20, 1.49. When adding this to a dynamic array, just reference the address with the * symbol.

As such

Soda *ArrayOfSoda;                  // declares a pointer of type Soda
ArrayOfSoda = new Soda[15];        // initialized the pointer to a dynamic array of type Soda

int NumOfSodaInArray = 0;           // Current size of the array  (not the capacity of the array)

// add a Soda to the array
ArrayOfSoda[NumOfSodaInArray++] = *newSoda;  

// This grabs the address of the newSoda that was previous made
// Adds the address to the pointer of type Soda
// Increments the NumOfSodaInArray after the pointer was added to the array.
// NumOfSodaInArray is now equal to 1
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.