Hi guys Im trying to do a program from class. The program includes classes, so t
ID: 3765294 • Letter: H
Question
Hi guys Im trying to do a program from class. The program includes classes, so the problem Im having is that the array used for the program its declared in the header file(.h file) and the functions are in my class .cpp (Starfleet.cpp)
Now the problem is that when Im reading a file I need to use #include <fstream > and also need to use the array created in the header file so the program can start building a database of stuff that later we will add more so I will cotinue using the header array
at the end of the program I need to use ofstream to output all the stuff I inputed to the file and also all the stuff I added to the file and the file must be formated the same as when it was read before so when you read it again dont break the program
Please help me to figure how can I add the read of the file and write of the file into the class starfleet.cpp because Im only aloud to use the array created in the header in the class !!!
my header file (starfleet.h)
***************************************************************************
#ifndef STARFLEET_h
#define STARFLEET_h
#include <iostream>
using namespace std;
const int S_SIZE = 256;
const int FLEET_SIZE = 30;
struct Ship
{
char name[S_SIZE];
int registry;
char type[S_SIZE];
char position[S_SIZE];
char condition[S_SIZE];
char captain[S_SIZE];
};
class Starfleet
{
//public so main can use the class otherwise use private!
public:
void searchData(Ship my_Array[], int countS);
void showArray(Ship my_Array[], int count);
void AddaShip(Ship my_my_Array[], int count);
void SavetoFile(Ship my_my_Array[], int count);
//variables
private: ////////////////////////////////////////////////////////////////
Ship my_Array[FLEET_SIZE];//This is my array used for the program //
int registry; ////////////////////////////////////////////////////////////////////
};
#endif //starFleetClass
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Now This is my class .cpp(starfleet.cpp)
*****************************************************************
# include <iostream>
# include "Starfleet.h"
# include <iomanip>
#include <fstream>
using namespace std;
//Search Data Function
void Starfleet::searchData(Ship my_Array[], int countS)
{
long keyInput;
cout << "what is the registry of the ship? ";
cin >> keyInput;
bool flag = false;
for (int j = 0; j < countS; j++)
{
if (keyInput == my_Array[j].registry)
{
cout << endl;
cout << " Ships Information: " << " "
<< "-----------------------" << " "
<< " Name: " << my_Array[j].name << " "
<< " Registry: " << my_Array[j].registry << " "
<< " Type: " << my_Array[j].type << " "
<< " Position: " << my_Array[j].position << " "
<< " Condition: " << my_Array[j].condition << " "
<< " Captain: " << my_Array[j].captain<< “ ”;
flag = true;
break;
}
}
if (!flag)
cout << "Error. " << keyInput << " was not found in the database" << endl;
}
//Function to show the ships in the database!
void Starfleet::showArray(Ship my_Array[], int count)
{
cout << "Successfully loaded " << count << " ships in the database." << endl;
cout << endl;
cout << left << setw(12) << "Name";
cout << left << setw(12) << "Registry";
cout << left << setw(12) << "Type";
cout << left << setw(18) << "Position";
cout << left << setw(10) << "Condition";
cout << right << setw(10) << "Captain" << ' ';
cout << "---------------------------------------------------------------------------" << endl;
for (int k = 0; k < count; k++)
{
cout << left << setw(12) << my_Array[k].name;
cout << left << setw(10) << my_Array[k].registry;
cout << left << setw(14) << my_Array[k].type;
cout << left << setw(18) << my_Array[k].position;
cout << left << setw(10) << my_Array[k].condition;
cout << right << setw(10) << my_Array[k].captain << endl;
}
cout << endl;
}
//Add a ship
void Starfleet::AddaShip(Ship my_Array[], int count){
int counter = 0, tester1 = 0;
cout << "What is the name of the ship?: ";
cout << " >>";
cin >> my_Array[counter].name;
cout << "What is the registry of the ship?: ";
cout << " >>";
if (cin >> tester1)
{
my_Array[counter].registry = tester1;
}
else
{
while (!(cin >> tester1))
{
cout << "Please enter an integer!!" << endl;
cout << " >>";
cin >> tester1;
cin.clear();
cin.ignore(1000, ' ');
}
my_Array[counter].registry = tester1;
}
cout << "What is the type of the ship?: ";
cout << " >>";
cin >> my_Array[counter].type;
cout << "What is the position of the ship?: ";
cout << " >>";
cin >> my_Array[counter].position;
cout << "Condition of the Ship ?: ";
cout << " >>";
cin >> my_Array[counter].condition;
cout << "Who is the captain of the ship ?: ";
cout << " >>";
cin >> my_Array[counter].captain;
counter++;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Here Im trying to put the read of the file because Im using the array from the header file!!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void LookforFile(Ship my_Array[], int count){
int counter = 0;
ifstream inputfile;
char myfile[FLEET_SIZE];
cin.get(myfile, FLEET_SIZE);
inputfile.open(myfile);
cin.clear();
cin.ignore(100, ' ');
while (!inputfile.is_open())
{
cout << "Unable to open file" << endl;
cout << "What is the name of the Ship's data input file? ";
cout << " >>";
cin.get(myfile, FLEET_SIZE);
inputfile.open(myfile);
cin.clear();
cin.ignore(100, ' ');
}
if (inputfile.is_open())
{
cout << endl;
cout << " File Open Successfully!!" << endl;
while (!inputfile.eof())
{
inputfile.get(my_Array[counter].name, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile >> my_Array[counter].registry;
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].type, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].position, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].condition, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].captain, S_SIZE, ' ');
inputfile.ignore(S_SIZE, ' ');
counter++;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Here Im trying to write the file out because Im using the array from header file!!!
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void SavetoFile(Ship my_Array[], int count){
inputfile.close();
ofstream savefile(myfile, 'w');
//void SavetoFile();
for (int i = 0; i < count; i++)
{
savefile << my_Array[i].name << ";";
savefile << my_Array[i].registry << ";";
savefile << my_Array[i].type << ";";
savefile << my_Array[i].position << ";";
savefile << my_Array[i].condition << ";";
savefile << my_Array[i].captain << endl;
}
savefile.close();
}
And here is my main file.cpp(main.cpp)
****************************************************
#include "Starfleet.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
//Main Function
int main()
{
// int tester1 = 0; //To check foe the integer when inputing the registry number!
Starfleet ship1; // to create the object to use funtion
//Variable for switch menu!
char choice = '';
//my_Array of structs
Ship my_Array[FLEET_SIZE];
//Variables for Menu!
int counter = 0;
cout << "Computer. Working. What is the name of the Ship's data input file? ";
cout << " >> ";
void LookforFile();
do
{
cout << endl;
cout << "What would you like to do? " << endl;
cout << "(F)ind a ship by registry number and display it's information, (E)nter a new ship into the database, (D)isplay all of the ships in the datbase, (Q)uit? ";
cout << endl << " >> ";
cin >> choice;
switch (choice)
{
case 'F'://Search for a ship !
case 'f':
ship1.searchData(my_Array, counter);
break;
case 'E'://Add a ship!
case 'e':
ship1.AddaShip(my_Array, counter);
break;
case 'D'://List all ships in the Data base!
case 'd':
ship1.showArray(my_Array, counter);
break;
case 'q':
case 'Q':
break;
default:
cout << "Invalid entry. " << endl;
break;
}
}
while (choice != 'q' && choice != 'Q');
getchar();
getchar();
return 0;
}
Explanation / Answer
You need to add the function definitions to header file
Add the following in starfleet.h public methods
#include <fstream>
void LookforFile(Ship my_Array[], int count);
void SavetoFile(Ship my_Array[], int count, ifstream inputfile,char myfile[]);
following code in starfleet.cpp
void Starfleet::LookforFile(Ship my_Array[], int count){
int counter = 0;
ifstream inputfile;
char myfile[FLEET_SIZE];
cin.get(myfile, FLEET_SIZE);
inputfile.open(myfile);
cin.clear();
cin.ignore(100, ' ');
while (!inputfile.is_open())
{
cout << "Unable to open file" << endl;
cout << "What is the name of the Ship's data input file? ";
cout << " >>";
cin.get(myfile, FLEET_SIZE);
inputfile.open(myfile);
cin.clear();
cin.ignore(100, ' ');
}
if (inputfile.is_open())
{
cout << endl;
cout << " File Open Successfully!!" << endl;
while (!inputfile.eof())
{
inputfile.get(my_Array[counter].name, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile >> my_Array[counter].registry;
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].type, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].position, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].condition, S_SIZE, ';');
inputfile.ignore(S_SIZE, ';');
inputfile.get(my_Array[counter].captain, S_SIZE, ' ');
inputfile.ignore(S_SIZE, ' ');
counter++;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Here Im trying to write the file out because Im using the array from header file!!!
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//In this method you ar e using inputfile and myfile from lookup file, so you need to pass them. otherwise you have
to define private variables in starfleet.h
void Starfleet::SavetoFile(Ship my_Array[], int count, ifstream inputfile,char myfile[]){
inputfile.close();
ofstream savefile(myfile, ofstream::out | ofstream::app);
//void SavetoFile();
for (int i = 0; i < count; i++)
{
savefile << my_Array[i].name << ";";
savefile << my_Array[i].registry << ";";
savefile << my_Array[i].type << ";";
savefile << my_Array[i].position << ";";
savefile << my_Array[i].condition << ";";
savefile << my_Array[i].captain << endl;
}
savefile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.