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...
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...
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...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would like to find another way to compile the program without using static_cast in my code. Thank you #include <iostream> #include <iomanip> using namespace std; class Population { private: int population, birth, death; public: Population() { population = 2; birth = 0; death = 0; } Population(int x, int y, int z) { if (x < 2) population = 0; else population = x; if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT