Alphabetzing: Please, Elmo, Please

This code copying the data from two input files onto one output file. I thought I could figure out how to alphabetize, but it's been a couple hours and I've gotten nowhere...

Here's the code error free, as it prints both files onto the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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

class KeepRunning {                            //Prototype needed to keep console from closing.
  public:
    ~KeepRunning() {
      cin.get();}};

//Copies text
int copyLine(ifstream&, ifstream&, ofstream&);

int main()                                                                  
{   
    KeepRunning kr;  
     
    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;
    string lineA;
    string lineB;
    //int lineCount;
    //int index1 = 0; 
    //int index2 = 0;
    //int A = 0;
    //int B = 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 to ourFamily.
    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();
    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;
     }

Copy the contents into a Vector, use Sort to sort them and write the Vector out.

http://www.cplusplus.com/reference/algorithm/sort/

How do you copy the contents into a vector?? Sorry I'm extremely confused :/

Not a problem, an example by MiiNiPaa and myself can be found here: http://www.cplusplus.com/forum/beginner/126134/#msg683199

MiiNiPaa sorts by name, mine by Id but you can sort by anything with very little change. Examples also show how to put data into the vector.

Thank you!!
Insert the rows into a std::set and you don't need to sort.
http://www.cplusplus.com/reference/set/set/
Last edited on
I tried implementing a merge sort. Is there any reason this shouldn't work?
My program has stopped printing the data from the input files onto the output file (it was working before). The console isn't displaying anything whereas it was before. And, the output file is rebooting multiple times per run. I think I'm doing it wrong.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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
#define SIZE1 200
#define SIZE2 200
#define SIZE3 400

//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 insA[SIZE1]; 
    int insB[SIZE2];
    int outs[SIZE3];
    int lineCountA = 0;
    int lineCountB = 0;
    //int ourFamilyArray[400];
    string lineA;
    string lineB;
    int A = 0;
    int B = 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);
    {
         while((A < lineA.length()) && (B < lineB.length()))  
         { if (lineA[A] < lineB[SIZE2])
         { ourFamily << lineA << endl;
           lineCountA++;} 
           else 
         { ourFamily << lineB << endl;
           lineCountB++;}} 
        
    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;
     }


Well what I was referring to was something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Family
{
	std::string familyName;
	int familyAge;
	Family(string name, int age) : familyName(name), familyAge(age) {}
};

// sorting
bool sortByAge(const Family &val1, const Family &val2) { return val1.familyAge < val2.familyAge; }
bool sortByName(const Family &val1, const Family &val2) { return val1.familyName < val2.familyName; }

int main()
{
	// create a vector of type Family for family members.
	std::vector<Family> familyList;

	// you could loop through each file in turn and enter their details in the vector
	// I am just going to enter some dummy data as an example just to show you whats
	// needed.  Below I am passing Name and age (defined in Family structure), if
	// you are just storing names then age can be removed.

	familyList.push_back(Family("Joe Bloggs", 43));
	familyList.push_back(Family("Paul Jones", 21));
	familyList.push_back(Family("Deborah Sanders", 31));
	familyList.push_back(Family("Dylan Wood", 12));

	// display them
	cout << "Before any sorting..." << endl << endl;
	for (int i = 0; i < 4; i++)
		cout << familyList[i].familyName << ", aged " << familyList[i].familyAge << "." << endl;

	// so all your data has been read into the vector from all three files,
	// lets sort it... first we will sort by age.
	sort(familyList.begin(), familyList.end(), sortByAge);

	// display them
	cout << endl << "After sorting by age..." << endl << endl;
	for (int i = 0; i < 4; i++)
		cout << familyList[i].familyName << ", aged " << familyList[i].familyAge << "." << endl;

	// sort by name.
	sort(familyList.begin(), familyList.end(), sortByName);

	// display them
	cout << endl << "After sorting by name..." << endl << endl;
	for (int i = 0; i < 4; i++)
		cout << familyList[i].familyName << ", aged " << familyList[i].familyAge << "." << endl;

	return 0;

}
Last edited on
Topic archived. No new replies allowed.