Vector Not Found Error! (sorting txt files)

I've spent the last 12 hours writing a code that is supposed to take two text files, one containing names and the other numbers, and merge them into one text file. The code then needs to sort the given text file alphabetically and display the first alphabetically displayed lines.

I've successfully managed to merge the files but I am struggling to organize the file in alphabetical order. The best answer I've found was converting the lines of the file into vectors and then sort using that. However, every time I do so I get a big error saying that the vector can't be found. I've yet to find any other solutions outside of vectors. Any input is welcome because I'm tired of this project.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	cout << "=========== ORGANIZED PHONE BOOK ===========\n";
	cout << "============ by: Jack LASTNAME ============\n\n";

	ifstream ifs1("C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNames.txt");
	ifstream ifs2("C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNums.txt");
	fstream ofs("C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneCombined.txt");
	string s1, s2;
    int x = 1;

	while (getline(ifs1, s1) && getline(ifs2, s2))
	{
		if (x <= 2000)
		{
			ofs << left << setw(32) << s1 << " " << s2 << endl;
		}
		x++;
	}

    vector<string> words;
    string oneword;

    while(getline(ofs, oneword));
	{
		words.push_back(oneword);
		sort(words.begin(), words.end());
	}
	for (int i = 0; i <= 50; i++)
	{
		cout << words[i] << endl;
	}

    return 0;
}


I know the code looks rather sketch and messy, but right now it works. Though if anyone could help me improve it would be greatly appreciated. I'm using visual studio and I know it can be touchy sometimes. I'm near the end of the semester so use any code you feel fit to help.
> while(getline(ofs, oneword));
That trailing ; is a doozy. You made the body of the loop into a do-nothing loop.
1
2
3
4
5
6
7
8
9
10
   while(getline(ofs, oneword))
   {
        ;  // this is the long form of what you wrote
   }
   // so this happens only once.
   {
        words.push_back(oneword);
        // you probably want to sort them after you've read them all
        sort(words.begin(), words.end());
   }


Say
1
2
3
4
5
6
    while(getline(ofs, oneword))
    {
        words.push_back(oneword);
    }
    // you probably want to sort them after you've read them all
    sort(words.begin(), words.end());


> for (int i = 0; i <= 50; i++)
Use words.size(), not 50.

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
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

int main()
{
    const std::string in_file1_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNames.txt" ;
    const std::string in_file2_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneNums.txt" ;
    const std::string out_file_name = "C:/Users/Jack/Downloads/Class Material/Computer Science/Asn Five/phoneCombined.txt" ;

    if( std::ifstream names_file{in_file1_name} ) // if the names file was opened
    {
        // read the names into a vector
        std::vector<std::string> vec ;
        std::string name ;
        while( std::getline( names_file, name ) ) vec.push_back(name) ;

        // open the numbers file and append the numbers to the string after the corresponding name
        if( std::ifstream numbers_file{in_file2_name} )
        {
            std::string number ;
            std::size_t index = 0 ; // index into the vector, we start with the name at the front
            for( ; index < vec.size() && numbers_file >> number ; ++index )
            {
                // pad the name with spaces on the right (to get the effect of << left << setw(32))
                vec[index].resize( 32, ' ' ) ;
                vec[index] += "  | " + number ; // append the phone number
            }
        }

        // sort the lines in the vector
        std::sort( vec.begin(), vec.end() ) ;

        // write the lines in the sorted vector to the output file
        if( std::ofstream output_file{out_file_name} )
        {
            // range based loop: https://www.stroustrup.com/C++11FAQ.html#for
            for( const std::string& line : vec ) output_file << line << '\n' ;
        }
    }
}
It's easier to use std::map then vectors. For C++17 consider:

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
#include <fstream>
#include <map>
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
	std::ifstream pnamfs("phonenames.txt");
	std::ifstream pnumfs("phonenums.txt");
	std::ofstream pcomfs("phonecombined.txt");

	if (!pnamfs.is_open() || !pnumfs.is_open() || !pcomfs.is_open())
		return (std::cout << "Cannot open files\n"), 1;

	std::map<std::string, std::string> comb;

	for (std::string nam, num; (pnamfs >> nam) && (pnumfs >> num); comb[nam] = num);

	for (const auto& [nam, num] : comb)
		pcomfs << std::left << std::setw(32) << nam << " " << num << '\n';

	pcomfs.close();

	std::ifstream pcomifs("phonecombined.txt");

	for (std::string line; std::getline(pcomifs, line); std::cout << line << '\n');
}


Given
phonenames.txt

name3
name4
name2
name1
name5


phoenums.txt

11
22
33
44
55


displays the contents of phonecombined.txt

name1                            44
name2                            33
name3                            11
name4                            22
name5                            55

Last edited on
salem C

When I used your solution I came up with the same error that I usually get:

Unhandled exception at 0x00CBF2F6 (ucrtbased.dll) in Sorted Phone List.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

If I knew what this meant then maybe I could fix it, but sadly I don't. Do any of you guys happen to know what this means?

The program organizes the words in an odd way that I didn't expect. The way in which I've merged the numbers is:
1
2
3
4
5
name1         11
name2         22
name3         33
name4         44
name5         55
Last edited on
> while(getline(ofs, oneword))
Yeah, the other problem being you're trying to read from a file that's still open for OUTPUT.

You need to close it first, then open it again for input.
I managed to get it going using you all as a reference. Thank you all for your help. I love this site. There was a lot of things wrong with my program so I needed to take a little bit from all of you plus just a little bit more outside help.
Last edited on
Topic archived. No new replies allowed.