Phonebook multidimensional array from file?

Honestly I think I'm having a huge brainfart because I swear I've done this before but cannot think of how I did it. My goal right now is to put a list of information from a file into a multidimensional array. The file looks like this:

1
2
3
4
5
6
7
5
Jerry-Fischer 693-661-4427 office
Aria-Plumb 673-821-6123 office
Castro-Fyre 643-789-9876 office
Trisha-Sawyer 617-678-4567 office
Kyle-Landers 217-971-5678 office

The number at the top of the file is for number of entries, so I can go through the file will a for loop that iterates up to that number to store the data.
I want to store the variables because I will be searching them later, so storing name as string name, number as string number(yes, also a string) and type of contact as string contactType.
I want the array to look like this:

string array[][] = {Jerry-Fischer, 693-661-4427, office
Aria-Plumb, 673-821-6123, office
Castro-Fyre, 643-789-9876, office
Trisha-Sawyer, 617-678-4567, office
Kyle-Landers, 217-971-5678, office
}

So basically a 3 by 5 array. Idk why my brain isn't working with me today. Any help is appreciated.
Hello simulated sushi ,

If your intent is to initialize the array:
1
2
3
4
5
6
7
8
9
10
constexpr int MAXROWS{ 5 }, MAXCOLS{ 3 };

std::string array[MAXROWS][MAXCOLS]
{ 
    { "Jerry - Fischer", "693 - 661 - 4427", "office" },
    { "Aria - Plumb", "673 - 821 - 6123", "office" },
    { "Castro - Fyre", "643 - 789 - 9876", "office" },
    { "Trisha - Sawyer", "617 - 678 - 4567", "office" },
    { "Kyle - Landers", "217 - 971 - 5678", "office" }
};

Try this.

Andy
Given a file:
ifstream file("input.txt");

First, you get the size shown:
1
2
int size;
file >> size;


With this, you can now produce a vector (arrays would be problematic here because you don't know the size of the file at compile-time).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector> 

// ...

struct Employee {
    string name;
    string phone_number;
    string location;
};

// ...

int size;
if (!(file >> size)) {
   cout << "Error reading number of employees\n";
   return 1;
}
vector<Employee> employees(size);


Then, you get each token one by one.

1
2
3
4
5
6
7
string name;
string phone_number;
string location;
while (file >> name >> phone_number >> location)
{
    employees[i] = Employee { name, phone_number, location };
}
Last edited on
Using a vector, you actually don't need to know the number of lines - but could save re-allocation.

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

struct Employee {
	std::string name;
	std::string phone_number;
	std::string location;
};

std::istream& operator>>(std::istream& is, Employee& emp)
{
	return is >> emp.name >> emp.phone_number >> emp.location;
}

std::ostream& operator<<(std::ostream& os, const Employee& emp)
{
	return os << emp.name << "  " << emp.phone_number << "  " << emp.location;
}

int main()
{
	std::ifstream ifs("employees.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	size_t size {};

	if (!(ifs >> size))
		return (std:: cout << "Error reading number of employees\n"), 2;

	std::vector<Employee> employees;
	employees.reserve(size);			// Not actually required but could save re-allocation

	for (Employee emp; ifs >> emp; employees.push_back(emp));

	for (const auto& e : employees)
		std::cout << e << '\n';
}




Jerry-Fischer  693-661-4427  office
Aria-Plumb  673-821-6123  office
Castro-Fyre  643-789-9876  office
Trisha-Sawyer  617-678-4567  office
Kyle-Landers  217-971-5678  office

If you need to use a 2d string array, then 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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
	const size_t MaxEmp {10};

	std::ifstream ifs("employees.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	size_t size {};

	if (!(ifs >> size))
		return (std:: cout << "Error reading number of employees\n"), 2;

	std::string employees[MaxEmp][3];

	for (size_t empno = 0; empno < MaxEmp && empno < size && ifs >> employees[empno][0] >> employees[empno][1] >> employees[empno][2]; ++empno);

	for (size_t eno = 0; eno < size; ++eno) {
		const auto& e = employees[eno];
		std::cout << e[0] << "  " << e[1] << "  " << e[2] << '\n';
	}
}

Last edited on
Topic archived. No new replies allowed.