Question

In: Computer Science

Below is my source code for file merging. when i run the code my merged file...

Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf

#include
#include
#include
using namespace std;

int main()
{
ifstream inFile1;
ifstream inFile2;
ofstream outFile1;
int mClientNumber, fClientNumber;
string mClientName;
   string fClientName;
bool atLeastOneFileNotAtEnd = true;
bool inFile1Written = false;
bool inFile2Written = false;
  
cout << "File merge processing starting." << endl;
  
inFile1.open("MaleClients.rtf");
inFile2.open("FemaleClients.rtf");
outFile1.open("MergedClients.rtf");
  
inFile1 >> mClientNumber;
   inFile1 >> mClientName;
inFile2 >> fClientNumber;
   inFile2 >> fClientName;
  
   while (atLeastOneFileNotAtEnd == true)
{
   if(inFile1.eof())
{
   if(inFile2Written == false)
{
   outFile1 << fClientNumber << " " << fClientName << endl;
inFile2Written = true;
}
   }
else if(inFile2.eof())
{
if(inFile1Written == false)
{
outFile1 << mClientNumber << " " << mClientName << endl;
inFile1Written = true;
}
}
else if (mClientNumber < fClientNumber)
{
outFile1 << mClientNumber << " " << mClientName << endl;
inFile1Written = true;
}
else
{
outFile1 << fClientNumber << " " << fClientName << endl;
inFile2Written = true;
}
  
if ((!inFile1.eof()) && (inFile1Written == true))
{
   inFile1 >> mClientNumber >> mClientName;
inFile1Written = false;
}
  
if ((!inFile2.eof()) && (inFile2Written == true))
{
inFile2 >> fClientNumber >> fClientName;
inFile2Written = false;
}
  
if ((inFile1.eof()) && (inFile2.eof()))
{
   atLeastOneFileNotAtEnd = false;
   }
  
  
}
  
   inFile1.close();
inFile2.close();
outFile1.close();
  
cout << "Merging Complete." << endl;
  
   return 0;
}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
   ifstream inFile1;
   ifstream inFile2;
   ofstream outFile1;
   int mClientNumber, fClientNumber;
   string mClientName;
   string fClientName;
   bool atLeastOneFileNotAtEnd = true;
   bool inFile1Written = false;
   bool inFile2Written = false;

   cout << "File merge processing starting." << endl;
   inFile1.open("MaleClients.rtf");
   inFile2.open("FemaleClients.rtf");
   outFile1.open("MergedClients.rtf");
   if (!inFile1.is_open() || !inFile2.is_open()){
       cout<<"Errro!!! File not found!!!";
   }
   inFile1 >> mClientNumber;
   inFile1 >> mClientName;
   inFile2 >> fClientNumber;
   inFile2 >> fClientName;

   while (atLeastOneFileNotAtEnd)
   {
       if(inFile1.eof())
       {
           if(inFile2Written == false)
           {
               outFile1 << fClientNumber << " " << fClientName << endl;
               inFile2Written = true;
           }
       }
       else if(inFile2.eof())
       {
           if(inFile1Written == false)
           {
               outFile1 << mClientNumber << " " << mClientName << endl;
               inFile1Written = true;
           }
       }
       else if (mClientNumber < fClientNumber)
       {
           outFile1 << mClientNumber << " " << mClientName << endl;
           inFile1Written = true;
       }
       else
       {
           outFile1 << fClientNumber << " " << fClientName << endl;
           inFile2Written = true;
       }

       if ((!inFile1.eof()) && (inFile1Written == true))
       {
           inFile1 >> mClientNumber >> mClientName;
           inFile1Written = false;
       }

       if ((!inFile2.eof()) && (inFile2Written == true))
       {
           inFile2 >> fClientNumber >> fClientName;
           inFile2Written = false;
       }

       if ((inFile1.eof()) && (inFile2.eof()))
       {
           atLeastOneFileNotAtEnd = false;
       }


   }

   inFile1.close();
   inFile2.close();
   outFile1.close();

   cout << "Merging Complete." << endl;

   return 0;
}

make sure the files are present and should be available for open.

MaleClients.rtf

1
2
3
4
5
4
5
6
7
8
9

FemaleClients.rtf

10 20
12 23
1 2
3 4
44 44
88 88
90 10

output


Related Solutions

Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
This code needs to run a working Reverse Polish Calculator, but when I input my commands...
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero. Here is my code. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int element; struct Node *next; }; //Global variable struct Node *top = NULL; void push(int ele) { struct Node *newNode; newNode = (struct Node *) malloc(sizeof(struct Node)); newNode->element = ele; newNode->next = top; top = newNode; }...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
I have attempted to implement some changes to my source code and was unable to succefully...
I have attempted to implement some changes to my source code and was unable to succefully implement there are (two) changes. I am in the learning process so comments help in the learning curve so if possible leave comments. PLEASE DONT CHANGE ANY OF THE SOURCE CODE - unless needed to implement the changes. * Change the program so that the user can choose to either check if a password is valid or have the program randomly generate a password...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I cannot get this code to run on my python compiler. It gives me an expected...
I cannot get this code to run on my python compiler. It gives me an expected an indent block message. I do not know what is going on. #ask why this is now happenning. (Create employee description) class employee: def__init__(self, name, employee_id, department, title): self.name = name self.employee_id = employee_id self.department = department self.title = title def __str__(self): return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)    def main(): # Create employee list emp1...
I have a problem with my code. It does not run. Please can someone check the...
I have a problem with my code. It does not run. Please can someone check the code and tell me where I went wrong? This is the question: Program Specification: Write a C program that takes the length and width of a rectangular yard, and the length and width of a rectangular house (that must be completely contained in the yard specified) as input values. Assuming that the yard has grass growing every where that the house is not covering,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT