Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have code that reads and writes sstrings to file but I am having trouble meeti

ID: 3930467 • Letter: I

Question

I have code that reads and writes sstrings to file but I am having trouble meeting these requirements. My program writes the string names into the file and not the number of bags. I think a for loop is needed to keep track of how many times their name is written and then check their on board status but I'm not sure. This is the code I have

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int openOutputFile();
int openInputFile();
void readInputRecord(ifstream& inpt);
void writeOutputRecord(ofstream& opt);
int count_b = 0;
int count_p = 0;
string PassengerArray[100];
string BaggageArray[300];
int main()
{
   openInputFile();
   openOutputFile();
   system("pause");


}
void readInputRecord(ifstream& inpt)
{

   string line;
   int i = 0;
   //read passanger from file
   while (getline(inpt, line))
   {
       PassengerArray[i] = line;
       i++;
   }
   cout << "Passanger Array " << endl;
   for (int k = 0; k<i; k++)
       cout << PassengerArray[k] << endl;
   count_p = i;
   //read baggage from file
   while (getline(inpt, line) && line != "")
   {
       BaggageArray[i] = line;
       i++;
   }
   count_b = i;
   cout << "Baggage Array" << endl;
   for (int k = 0; k<i; k++)
       cout << BaggageArray[k] << endl;
   i = 0;
  
}
//write to file
void writeOutputRecord(ofstream& opt)
{
   for (int i = 0; i<count_p; i++)
       opt << PassengerArray[i] << endl;
   for (int i = 0; i<count_b; i++)
       opt << BaggageArray[i] << endl;
  
  

}
int openOutputFile()
{
   ofstream outputFile;
   const string OUTPUT_FILE = "flightoutput.txt";

   outputFile.open(OUTPUT_FILE, std::ofstream::out);
   //check to make sure the file opened successfully
   if (!outputFile)
   {
       //Display an error if the open failed
       cout << "Error opening the output file: " << OUTPUT_FILE << endl;
       return EXIT_FAILURE;
   }
   writeOutputRecord(outputFile);
}
//open output file
int openInputFile()
{
   ifstream inputFile;
   const string INPUT_FILE = "flightinput.txt";

   inputFile.open(INPUT_FILE);
   //check to make sure the file opened successfully
   if (!inputFile)
   {
       //Display an error if the open failed
       cout << "Error opening the input file: " << INPUT_FILE << endl;
       return EXIT_FAILURE;
   }
   readInputRecord(inputFile);
}

my output....

PassengerArray

smith robert samuel

fellows mary louise

harwell herman allan

Baggage Array

fellows mary louise

jones sally

fellows mary louise

smith robert samuel

Correct Output

jones sally............................. 01 WARNING: passenger not on board

smith robert Samuel..................... 01 OK

fellows mary louise..................... 02 OK

harwell herman allan.................. 00 OK

Explanation / Answer

while (getline(inpt, line) && line != "")
   {
       BaggageArray[i] = line;
       i++;
   }
   count_b = i;
   cout << "Baggage Array" << endl;
   for (int k = 0; k<i; k++)
       cout << BaggageArray[k] << endl;

} // i=0 should not be there

//write to file
void writeOutputRecord(ofstream& opt)
{
   for (int i = 0; i<count_p; i++)
       opt << PassengerArray[i] << endl;
   for (int i = 0; i<=count_b; i++) // includes all the records of bags in array
       opt << BaggageArray[i] << endl;
  
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote