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...
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...
This is my code I want the average temp for the week to be printed when...
This is my code I want the average temp for the week to be printed when the user types : 'week' currently when the user types  'week' it only prints  Monday - Sunday and the average temp for each day. import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; public class weeklytemps {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);                  ArrayList Day = new ArrayList(Arrays.asList(    "Monday","Tuesday","Wednesday","Thurday","Friday","Saturday","Sunday")); // Stores days of the week...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
How can I refactor my current code to Reverse engineer an unknown file format containing the...
How can I refactor my current code to Reverse engineer an unknown file format containing the same data fields but stored using a different representation in C. My Code: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{    double slope;    char spade;    int water;    short wood;    char afternoon;    float daughter;    unsigned long beginner;    char scissors;    char structure;    char competition;    char force;    char mark[11];    short copy;    double grain;...
VHDL Code will not run simulation. What is the problem with my code?? --VHDL Code library...
VHDL Code will not run simulation. What is the problem with my code?? --VHDL Code library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.NUMERIC_STD.ALL; entity DataMemory16Bits is Port ( Address_DM : in STD_LOGIC_VECTOR(15 downto 0); Data_In_DM : in STD_LOGIC_VECTOR(15 downto 0); Clock : in STD_LOGIC; We_DM : in STD_LOGIC; Re_DM : in STD_LOGIC; Data_Out_DM : out STD_LOGIC_VECTOR(15 downto 0)); end DataMemory16Bits; architecture Behavioral of DataMemory16Bits is Type DataMemory16Bits is array(0 to 31) of STD_LOGIC_VECTOR(15 downto 0); signal memory: DataMemory16Bits; begin process...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT