Need Help Sorting Two Different "txt" Files + Display

I was tasked with writing a program that took two texts, one with full names and another with phone numbers, and organizes the names alphabetically, and displays the corresponding phone numbers. If I explained the task poorly here are the instructions I was given for clarity:

Write a program that prints a sorted phone list from a database of names and phone numbers. The data is contained in two files named "phoneNames.txt" and "phoneNums.txt". The files may contain up to 2000 names and phone numbers. The files have one name or one phone number per line. To save paper, only print the first 50 lines of the output.
Note: The first phone number in the phone number file corresponds to the first name in the name file. The second phone number in the phone number file corresponds to the second name in the name file. etc

Anyway, I've managed to organize the output and get the full names to be displayed in alphabetical order; however, I'm struggling to figure out how to display organized names with their corresponding numbers in the separate text files. I've been on this for about 4 hours and yet to find a solution. I don't usually go to this forum unless I've looked over all my options and it looks like nobody else has addressed something like this on the page. Anyway, here I am asking for any input.

Here's what I have so far:
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {

	cout << "=========== ORGANIZED PHONE BOOK ===========\n";
	cout << "============ by: FIRST LASTNAME ============\n\n";

	vector<string> names;
	vector<string> nums;

	ifstream namesFile("C:/Users/NAME/Downloads/Class Material/Computer Science/Asn Five/phoneNames.txt");
	ifstream numsFile("C:/Users/NAME/Downloads/Class Material/Computer Science/Asn Five/phoneNums.txt");


	if (!namesFile.is_open()) 
	{
		cout << "Unable to open file \n";
	}

	string name;
	string num;

	while (getline(namesFile, name))
	{
		names.push_back(name);
	}
	sort(names.begin(), names.end());

	while (getline(numsFile, num))
	{
		nums.push_back(num);
	}

	for (int i = 0; i <=50; i++)
	{
		cout << left << setw(32) << names[i] << nums[i] << endl;
	}

	return 0;
}


I would attach a link to the text files if I knew how, but it doesn't look like I have the ability.
Last edited on
Then post small samples of the text files into a post.

Edit:

Have you studied classes/structures yet?

You probably need to read both files into your arrays before you try to sort the names. When sorting you will need to "swap" the names and the numbers at the same times.

Last edited on
Topic archived. No new replies allowed.