Code for merging isn't doing anything
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Code for merging isn't doing anything

  1. #1
    Join Date
    May 2014
    Posts
    5

    Code for merging isn't doing anything

    'm trying to finish this project which is supposed to read two files, and merge them alphabetically. I've written this code, and it compiles. But it isn't actually doing anything?? I'm not really sure what I'm supposed to be seeing since I'm so new, so I don't know what I'm doing wrong. The console opens and closes immediately upon running. Any help would be GREATLY appreciated!!

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string> 
    using namespace std;
    
    #define hisFamily "The Adopted.txt"            //His family
    #define herFamily "The Originals.txt"          //Her family
    #define ourFamily "The Big Picture.txt"        //Our family
    
    //Function Prototypes
    //Copies text
    int copyLine(ifstream&, ifstream&, ofstream&);
    
    int main()                                                                  
    {   
        ifstream theAdopted("The Adopted.txt");
        ifstream theOriginals("The Originals.txt");
        ofstream theBigPicture("The Big Picture.txt");
        
        int SIZE1 = 200, SIZE2 = 200;  
        int insA[SIZE1]; 
        int insB[SIZE2];
        int outs[SIZE1 + SIZE2];
        int lineCount;
        int index1 = 0; 
        int index2 = 0;
        int A = 0;
        int B = 0;
        
       
        //Retreive his family's grades.
        theAdopted.open(hisFamily);
        if(theAdopted.fail())
        {
            cerr << "ERROR: Cannot open" << theAdopted << ". \n";
            return EXIT_FAILURE;
        } 
        //Retreive her family's grades.
        theOriginals.open(herFamily);
        if(theOriginals.fail())
        {
            cerr << "ERROR: Cannot open" << theOriginals << ". \n";
            return EXIT_FAILURE;
        }
        //Call theBigPicture.
        theBigPicture.open(ourFamily);
        if(theBigPicture.fail())
        {
            cerr << "ERROR: Cannot open" << theBigPicture << ". \n";
            return EXIT_FAILURE;
        }        
        //Copy data hisFamily to ourFamily.
        string line;
        lineCount = 0;
        getline(theAdopted, line);
        while(line.length() != 0)
        {
            lineCount++;
            theBigPicture << line << endl;
            getline(theAdopted,line);
        
        cout << "Input data mergered to file 'The Big Picture.exe'." << endl;
        //Close files.
        theAdopted.close();
        theOriginals.close();
        return 0;
    }  
    int copyLine
        (ifstream& theAdopted,
         ifstream& theOriginals,
         ofstream& theBigPicture);
         {
             const char NWLN = '\n';
             char nextCh;
             int charCount = 0;
             
             //Merge
             theAdopted.get(nextCh);
             while ((nextCh != NWLN) && !theAdopted.eof())
             {
                 theBigPicture.put(nextCh);
                 charCount++;
                 theAdopted.get (nextCh);
             }
             if(!theAdopted.eof())
             {
                 theBigPicture.put(NWLN);
                 charCount++;
             }
             return charCount;
         }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,309

    Re: Code for merging isn't doing anything

    Try setting some breakpoints and step through the code in a debugger.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Code for merging isn't doing anything

    Lets take this one step at a time. To stop the console closing put
    Code:
    system("pause");
    before a return. This will show a message and require an input from the keyboard before the program continues. This will allow you to see any messages displayed before they disappear.

    Code:
    ifstream theAdopted("The Adopted.txt");
    This will open the file "The Adopted.txt" for input so there is no need to use the .open method later. One way to check that a file is opened is to use the .isopen() method. So to open and check a file an example would be
    Code:
    ifstream theAdopted(hisFamily);
    
       if (!theAdopted.isopen())
       {
            cerr << "ERROR: Cannot open " << hisFamily << ". \n";
            return EXIT_FAILURE;
       }
    and similarly for the other files.

    For reading a file line by line, one way is
    Code:
    string line;
    while (getline(theAdopted, line)) {
        //process line
    }
    Try these changes and see if by using the debugger you can get a working program. If you need further guidance, post your changed code.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    May 2014
    Posts
    5

    Re: Code for merging isn't doing anything

    Thanks for responding. I was able to fix my code before, but I'm struggling to understand how to alphabetize the merged data. I thought it'd be simple, but for some reason it's blowing my mind! If you could point me in the right direction that would be reallllly amazing. I tried creating an ourFamilyArray to copy the data into that array first, then sort it. But it didn't work. I also tried converting the strings into vectors so I could use sort::, but I just can't seem to make anything work :/

    Code:
    #include <algorithm>
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string> 
    using namespace std;
    
    #define theAdopted "The Adopted.txt"            //His family
    #define theOriginals "The Originals.txt"        //Her family
    #define theBigPicture "The Big Picture.txt"     //Our family
    
    //Function Prototypes
    
    //Copies text
    int copyLine(ifstream&, ifstream&, ofstream&);
    
    int main()                                                                  
    {        
        ifstream hisFamily("The Adopted.txt");
        ifstream herFamily("The Originals.txt");
        ofstream ourFamily("The Big Picture.txt");
        
        int SIZE1 = 200, SIZE2 = 200;  
        int insA[SIZE1]; 
        int insB[SIZE2];
        int outs[SIZE1 + SIZE2];
        int lineCountA = 0;
        int lineCountB = 0;
        int ourFamilyArray[400];
        string lineA;
        string lineB;
        int indexA = 0; 
        int indexB = 0;
        
        //Retreive his family's grades.
        hisFamily;
        if(hisFamily.fail())
        {
            cerr << "ERROR: Cannot open " << theAdopted << ". \n";
            return EXIT_FAILURE;
        } 
        //Retreive her family's grades.
        herFamily;
        if(herFamily.fail())
        {
            cerr << "ERROR: Cannot open " << theOriginals << ". \n";
            return EXIT_FAILURE;
        }
        //Call theBigPicture.
        ourFamily;
        if(ourFamily.fail())
        {
            cerr << "ERROR: Cannot open " << theBigPicture << ". \n";
            return EXIT_FAILURE;
        }        
        //Copy data hisFamily and herFamily to array.
        getline(hisFamily, lineA);
        getline(herFamily, lineB);
         while(lineA.length() != 0 && lineB.length() != 0)
        {
            lineCountA++;
            lineCountB++;
            ourFamily << lineA << endl;
            ourFamily << lineB << endl;
            getline(hisFamily,lineA); 
            getline(herFamily,lineB);  
        } 
        cout << "Input data mergered to file 'The Big Picture.exe'." << endl;
        //Close files.
        hisFamily.close();
        herFamily.close();
        ourFamily.close();
        system("pause");
        return 0;
    }  
    int copyLine
        (ifstream& hisFamily,
         ifstream& herFamily,
         ofstream& ourFamily)
         {
             const char NWLN = '\n';
             char nextCh;
             int charCount = 0;
             
             //Merge
             hisFamily.get(nextCh);
             while ((nextCh != NWLN) && !hisFamily.eof())
             {
                 ourFamily.put(nextCh);
                 charCount++;
                 hisFamily.ignore(nextCh);
                 hisFamily.get (nextCh);
             }
             if(!hisFamily.eof())
             {
                 ourFamily.put(NWLN);
                 charCount++;
             }
             return charCount;
         }

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Code for merging isn't doing anything

    I tried creating an ourFamilyArray to copy the data into that array first, then sort it. But it didn't work.
    There is no reason why this shouldn't work. Instead of outputing lineA and lineB to the output file, add them to an array of string. Then use a simple sort (bubble, insert or selection etc) to sort the array. Once sorted output the sorted contents to the output file.

    Your code for reading assumes that both the input files have the same number of lines. What about if the one file is horter (or longer) than the other file?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    May 2014
    Posts
    5

    Re: Code for merging isn't doing anything

    I don't think that I know how to properly transpose the data into an array of string. I declared
    Code:
    string ourFamilyArray [400];
    and
    Code:
    int ourFamilyArray[400];
    , neither worked. Are they both wrong? When I had these arrays declared, I changed the term "ourFamily" to respective "ourFamilyArray". Compiled with errors everytime which implied I wasn't allowed to do what I was doing :/. I'm trying to figure out how to implement bubble right now, but I don't know if I can with my code as is and no array to read from...

  7. #7
    Join Date
    Aug 2009
    Posts
    439

    Re: Code for merging isn't doing anything

    How are you trying to use the array? And what do you mean by not working?

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Code for merging isn't doing anything

    I don't think that I know how to properly transpose the data into an array of st
    Something like this (untested)
    Code:
    string family[400];
    int fampos = 0;
    ...
    while (getline(hisFamily, lineA))
         family[fampos++] = lineA;
    
    while (getline(herFamily, lineB))
         family[fampos++] = lineB;
    this will create a string array (family) with entries first of his and then her family. All you now need to do is to sort this string array. Something like this (not tested)
    Code:
    sort(family, family + fampos);
    then just output to file
    Code:
    copy(family, family + fampos, ostream_iterator<string>(ourFamily, "\n"));
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center