Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So bisicaly i've been trying to make a C++ programme that is about information systems. These systems are made to handle an array of functions that deal with Ships(think of it like we are working in a dock and we need to enter in information about the ships that are there). To make this happen i created sctuct Ship that has different information in it(i could only figure out how to declare the number, name and the load capacity; i need to also declare date of leaving the dock and the list of shipments it has to carry- my first question is: How do i go about declaring it (do i declare a new struct because the max load a ship can carry is 10 * 1 tonne))). With the code as is i have problems with int enter () because it doesnt always for well, the same goes for void outout() . I havent been able to create a function to go with 4. from the menu that being "Show the ships with the biggest load capacity " the same goes for 6. "Add shipments" and 7. "Cancel shipments". In 7. there needs to be a function that first deletes the shipment from its previous ship and there need to be choices concerning what i should do with the shipment( for exapmle arrange to a different ship; if there are no free ships a new ship can be suggested to be inputed). For convenience id like to have an option to clear the info in the save .dat file.

All of the things i couldnt do i have no idea how to do as im really new to coding. I would be really greateful to any help i could get. Thank you for you time!

What I have tried:

C++
#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(0L, 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);

}
Posted
Updated 4-Jan-22 2:13am
v2

First off, indent your code: it becomes a whole load easier to see what is going on. Compare your version with this:
C++
#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[^]
   
Comments
panda08 3-Jan-22 11:35am
   
Ok i will use that. Thank you! Do you have any idea if i could create a function that clears the .dat file on demand, if yes how would i go about doing it?
Rick York 3-Jan-22 12:19pm
   
Since you are writing in binary mode why do you need to? Just flush your data and rewrite the file and it's done.

BTW - I never, ever use binary files and I can't remember the last time I did. The reason is they cause much more headache than they are worth as far as I am concerned. Disk space is cheap so there is no good reason for them in anything I do.
k5054 3-Jan-22 11:52am
   
Every time you open a file with ios::out, the file is truncated. If you use the mode ios::app the file is opened in append mode - which appends written data to the end of the file. You may want to take a look at your Save_File function - it may not do what you think it does.
As always I recommand to visit some Learn C++ tutorial to enhance your knowledge and skills like avoiding global data.
Another primary advice is to use class design and functions with speaking names. The first candidate is your Ship like:
C++
class Ship {
  unsigned int number;
  void enter();
  void save(File* f);
}

Take a look at the C++ vector to have some handy tool for managing a bunch of ships.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900