First off, indent your code: it becomes a whole load easier to see what is going on. Compare your version with this:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int N = 20;
const char Filename[] = "Shiplist.dat";
struct Ship
{
unsigned int number;
float capacity;
char name[50];
};
fstream sp;
int menu()
{
int choice;
cout << "\n \t MENU";
cout << "\n 1. Add individual ships";
cout << "\n 2. Add a list of ships";
cout << "\n 3. Print the list of all ships on screen";
cout << "\n 4. Show the ships with the biggest load capacity ";
cout << "\n 5. Search ship by name";
cout << "\n 6. Add shipments";
cout << "\n 7. Cancel shipments";
cout << "\n 8. Save ship information";
cout << "\n 9. Add new ship to the list";
cout << "\n 10. Exit";
do {
cout << "\n Your choice:";
cin >> choice;
} while (choice < 1 || choice > 10);
return choice;
}
Ship input()
{
Ship S = { 0 };
cin.ignore();
cout << "\n Enter ship number:";
cin >> S.number;
cin.ignore();
cout << "\n Enter the name of the ship:";
cin.getline(S.name, 50);
cout << "\n Enter the load capacity of the ship:";
cin >> S.capacity;
return (S);
}
int enter(Ship Lib[], int n)
{
int i, m;
do {
cout << "\n Enter information for how many ships(max 5 in one go):";
cin >> m;
} while (m < 0 || m > 5);
if (n + m < 20)
{
for (i = n; i < n + m; i++)
{
cout << "\n Ship " << i + 1;
Lib[i] = input();
}
return (n + m);
}
else cout << "\n You can't add any more ships!";
}
void Save_File(Ship Lib[], int n)
{
sp.open(Filename, ios::binary | ios::out);
if (!sp)
{
cout << "\n Error in file! \n";
exit(1);
}
sp.write((char*) Lib, sizeof(Ship) *n);
sp.close();
}
void append()
{
Ship b = { 0 };
sp.open(Filename, ios::binary | ios::app);
if (!sp)
{
cout << "\n Error in file! \n";
exit(1);
}
cout << "\n Add a new ship to the list \n";
b = input();
sp.write((char*) &b, sizeof(Ship));
sp.close();
}
void output(Ship Lib[], int n)
{
int i, k = 0;
cout << "\n \t List of all ships \n";
for (i = 0; i < n; i++)
{
cout << "\n" << i + 1 << "\t" << Lib[i].number << " " << Lib[i].name << " " << Lib[i].capacity;
k++;
if (k % 5 == 0) cout << "\n\n\n\n\n\n";
}
}
int Load_File(Ship Lib[])
{
long pos;
int n = 0, i;
Ship b;
sp.open(Filename, ios::binary | ios:: in);
if (!sp)
{
cout << "\n File doesn't exist!! \n";
return n;
}
sp.seekg(0 L, ios::end);
pos = sp.tellg();
sp.close();
n = pos / (sizeof(Ship));
sp.open(Filename, ios::binary | ios:: in);
if (!sp)
{
cout << "\n Error in file! \n";
exit(1);
}
for (i = 0; i < n; i++)
{
sp.read((char*) &b, sizeof(Ship));
Lib[i] = b;
}
sp.close();
return n;
}
void Ship_by_Name(Ship Lib[], int n)
{
char Sname[50];
cin.ignore();
cout << "\n Ship name: ";
cin.getline(Sname, 50);
for (int i = 0; i < n; i++)
if (!strcmp(Sname, Lib[i].name))
{
cout << "\n Ship found!";
cout << "\n Number: " << Lib[i].number << "\t Name: " << Lib[i].name << "\t Load capacity: " <<
Lib[i].capacity;
break;
}
else cout << "\n This ship isn't recorded!";
}
int main()
{
int choice, n = 0;
Ship Lib[N];
char answ = 'y';
n = Load_File(Lib);
do {
choice = menu();
switch (choice)
{
case 1:
input();
break;
case 2:
n = enter(Lib, n);
Save_File(Lib, n);
break;
case 3:
n = Load_File(Lib);
output(Lib, n);
break;
case 5:
n = Load_File(Lib);
Ship_by_Name(Lib, n);
break;
case 8:
Save_File(Lib, n);
break;
case 9:
do {
append();
n++;
cout << "\n One more[y/n]? " << "\n Answer: ";
cin >> answ;
Save_File(Lib, n);
} while (!(answ == 'N' || answ == 'n'));
break;
}
} while (choice != 9);
}
Now you can see easily where functions, loops, and conditionals start and end.
As for the stuff you are "missing" like "show ships with largest loading capacity" think and it: how would you do that manually?
Simple: you would go through the information on each ship in turn, making a note of its loading capacity and keeping a "running index" to the largest so far, updating that if you found a bigger one. Don't believe me? What's the biggest of these numbers:
1
2
3
77
62
142
4
999
-3
That's obvious: you looked at each until you got to the end and remembered what the last biggest one you met was. So do that in a computer: write a function that loops through all the ships, and check their load capacity. Return the largest at the end.
If you are having problems getting started at all, then this may help:
How to Write Code to Solve a Problem, A Beginner's Guide[
^]