A problem with a ship tracking program.

Hi all,
this is another HW and I've got a problem and can't figure out what's wrong.

The goal is: "Starfleet would like you to write a program to keep track of the ships in the fleet. Specifically, Starfleet would like a program to keep track of the name, registry number, and captain’s name for each ship. Your program will read initial ship data from a text file. Your program will then use a menu driven loop to allow the user to add addition ships, list the ships stored in memory, select a ship by name, or quit."

Here's the cpp file.

#include <iostream>
#include <string>
#include <fstream>
#include "Header.h"

using namespace std;

const int fleetSize = 30;

int main()
{
int numOfShips = 0;
int menuChoice = 0;
string shipName[fleetSize];
int shipRegistrie[fleetSize];
string shipCaptain[fleetSize];
numOfShips = loadData(shipName, shipRegistrie, shipCaptain);
cout << "Welcome to the ship tracking program." << endl;
cout << "Choose one of the menus below." << endl;
while (menuChoice != 1 && menuChoice != 2 && menuChoice != 3 && menuChoice != 4)
{
cout << "1. Add the name of a new ship. (Max: 30)" << endl;
cout << "2. Display all the ships." << endl;
cout << "3. Search for a ship." << endl;
cout << "4. Quit the program." << endl;
cin >> menuChoice;
if (numOfShips >= 30)
{
cout << "Sorry, you can't put any more ships. " << endl;
menuChoice = 0;
}
else if (menuChoice == 1)
{
shipName[numOfShips] = shipNames();
shipRegistrie[numOfShips] = shipRegistries();
shipCaptain[numOfShips] = shipCaptains();
putData(numOfShips, shipName, shipRegistrie, shipCaptain);
numOfShips++;
}
else if (menuChoice == 2)
{
displayShips(numOfShips, shipName, shipRegistrie, shipCaptain);
}
else if (menuChoice == 3)
{
search(numOfShips, shipName, shipRegistrie, shipCaptain);
}
else if (menuChoice == 4)
{
cout << "Program terminated." << endl;
system("pause");
return 0;
}
menuChoice = 0;
}
system("pause");
}

and this is the header file

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

using namespace std;

int loadData(string shipNames[30], int shipRegistries[30], string shipCaptains[30])
{
int shipCount = 0;
ifstream shipData;
shipData.open("shipData.txt");
while (!shipData.eof())
{
shipData >> shipNames[shipCount];
shipData >> shipRegistries[shipCount];
shipData >> shipCaptains[shipCount];
shipCount++;
}
shipData.close();
return shipCount;
}

string shipNames()
{
string trackNames;
cout << "What's the name of the ship that you want to add to your database? " << endl;
cin >> trackNames;
return trackNames;
}

int shipRegistries()
{
int Regies;
cout << "What's the registry number for your new ship? " << endl;
cin >> Regies;
cout << Regies << " added to your new ship. " << endl;
return Regies;
}

string shipCaptains()
{
string captains;
cout << "What's the name of the captain? " << endl;
cin >> captains;
cout << captains << " will command your ship. " << endl;
return captains;
}

void displayShips(int numOfShips, string shipName[30], int registries[30],string captainName[30])
{
for (int i = 0; i < numOfShips; i++)
{
cout << shipName[i] << endl;
cout << registries[i] << endl << endl;
cout << captainName[i] << endl;
}
}

void search(int numOfShips, string shipName[30], int registries[30], string captainName[30])
{
string name;
cout << "Which ship are you looking for? " << endl;
cin >> name;
for (int i = 0; i < numOfShips; i++)
{
if (name == shipName[i])
{
cout << "Match found!" << endl;
cout << shipName[i] << endl;
cout << registries[i] << endl;
cout << captainName[i] << endl;
system("pause");
break;
}
else if (name != shipName[i] && i == numOfShips)
{
cout << "No match is found." << endl;
}
}
}

void putData(int numOfShips, string shipNames[30], int shipRegistries[30], string shipCaptains[30])
{
ofstream shipData;
shipData.open("shipData.txt");
for (int i = 0; i < numOfShips; i++)
{
shipData << shipNames[i] << endl;
shipData << shipRegistries[i] << endl;
shipData << shipCaptains[i] << endl;
}
shipData.close();
}

this is the inputfile.

Enterprise
17014
Kirk
Voyager
74656
Janeway
Liberty
66265
Bouchillon


the problem is, whenever I add a new ship and try to display the ship,
it outputs a weird number -898878 something like this.
Which code went wrong?

help plz!
Last edited on
loadData loops incorrectly on eof.
putData relies on numOfShips to be correct, but whenever you add a new ship, you don't increase that number until after putData is called.
Topic archived. No new replies allowed.