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."
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;
}
}
}
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.