1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include "lab4Header.h"
#include <iostream>
#include <fstream>
#include <iomanip>
//initialize pointer
starFleet::starFleet()
{
for (int i = 0; i < FLEET_SIZE; i++)
{
Ships[i] = NULL; // RM, changed from ships to Ships, as per your class.
}
shipCount = 0;
}
starFleet::~starFleet()
{
for (int i = 0; i < shipCount; i++)
{
delete[] Ships[i]->name;
delete[] Ships[i]->type;
delete[] Ships[i]->position;
delete[] Ships[i]->condition;
delete[] Ships[i]->captain;
delete[] Ships[i];
}
}
void starFleet::loadData()
{
shipCount = 0; //set the count = 0
ifstream inputFile;
cout << "Loading Data file......." << endl;
inputFile.open("starships.txt");
char tempString[S_SIZE]; // RM, temporary holder for input info.
while (!inputFile.eof())
{
Ships[shipCount] = new shipData; // RM, Set aside data for a new ship.
inputFile.get(tempString, S_SIZE, ';');
Ships[shipCount]->name = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
inputFile.get(tempString, S_SIZE, ';');
strcpy(Ships[shipCount]->name, tempString); // RM, Copy the name info into the name array.
inputFile >> Ships[shipCount]->regNum;
inputFile.get(tempString, S_SIZE, ';');
Ships[shipCount]->type = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(Ships[shipCount]->type, tempString);
inputFile.get(tempString, S_SIZE, ';');
Ships[shipCount]->position = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(Ships[shipCount]->position, tempString);
inputFile.get(tempString, S_SIZE, ';');
Ships[shipCount]->condition = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(Ships[shipCount]->condition, tempString);
inputFile.get(tempString, S_SIZE, ';');
Ships[shipCount]->captain = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(Ships[shipCount]->captain, tempString);
/* RM, modify all of the code for type, position, etc. so it matches the code for name. You can reuse tempString for this.
You need to use tempString first, set aside the memory with new, and then copy the data over using strcpy.*/
shipCount++;
}
if (shipCount > 0)
shipCount--;
inputFile.close();
std::cout << endl << "Successfully loaded " << shipCount << " Ships." << endl << endl; // return the total count
return;
}
void starFleet::addstarship()
{
Ships[shipCount] = new shipData(); // RM, your struct is called shipData, not Ship.
fstream file("starships.txt", fstream::app);
char tempName[S_SIZE];
cout << " What is the name of the ship ? : ";
cin.get(tempName, S_SIZE);
Ships[shipCount]->name = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->name, tempName);
cout << " What is the registry number of the ship ? : ";
cin >> Ships[shipCount]->regNum;
cout << " What is the type of the ship ? : ";
cin.get(tempName, S_SIZE);
Ships[shipCount]->type = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->type, tempName);
cout << " What is the position of the ship ? : ";
cin.get(tempName, S_SIZE);
Ships[shipCount]->position = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->position, tempName);
cout << " What is the condition of the ship ? : ";
cin.get(tempName, S_SIZE);
Ships[shipCount]->condition = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->condition, tempName);
cout << " What is the captain of the ship ? : ";
cin.get(tempName, S_SIZE);
Ships[shipCount]->captain = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(Ships[shipCount]->captain, tempName);
// RM, can't use dots with pointers. You have to dereference with (->).
file << '\n' << Ships[shipCount]->name << ';' << Ships[shipCount]->regNum << ';' <<
Ships[shipCount]->type << ';' << Ships[shipCount]->position << ';' <<
Ships[shipCount]->condition << ';' << Ships[shipCount]->captain;
shipCount++;
return;
}
void starFleet::displayFleet()
{
// int count = 0; RM, not necessary.
if (shipCount > 0)
{
cout << "List all of the fleet!" << endl;
for (int i = NULL; i < shipCount; i++)
{
// RM, can't use dots with pointers. Have to dereference with (->).
cout << "Name of the ship: " << Ships[i]->name << endl;
cout << "Registry number: " << Ships[i]->regNum << endl;
cout << "Type of ship" << Ships[i]->type << endl;
cout << "Position of ship: " << Ships[i]->position << endl;
cout << "Condition of the ship: " << Ships[i]->condition << endl;
cout << "Captain of the ship: " << Ships[i]->captain << endl;
}
}
}
bool starFleet::searchFleet()
{
int regNum1;
cout << "Please enter the registry number of the fleet you would like to search for: ";
cin >> regNum1;
for (int d = NULL; d < shipCount; d++)
{
// RM, can't use dots with pointers. Use -> to dereference.
if (Ships[d]->regNum == regNum1){
cout << setprecision(2) << setw(5) << left << "Name: " << Ships[d]->name << endl;
cout << setprecision(2) << setw(5) << left << "registry number: " << Ships[d]->regNum << endl;
cout << setprecision(2) << setw(5) << left << "type: " << Ships[d]->type << endl;
cout << setprecision(2) << setw(5) << left << "Position: " << Ships[d]->position << endl;
cout << setprecision(2) << setw(5) << left << "Condition of ship: " << Ships[d]->condition << endl;
cout << setprecision(2) << setw(5) << left << "Captain of ship: " << Ships[d]->captain << endl;
return true;
}
}
cout << "That fleet does not exsist" << endl;
return false;
}
// RM, getCount implementation.
int starFleet::getCount()
{
return shipCount;
}
|