Two-Dimensional Array Duplicate Values

Howdy,

I am attempting to create a program for costing allocations to different projects.

I would like to ask for the number of inputs to determine a 2D array size.

I would like to input the project number, followed by the number of hours charged to the project. I want the program to then find duplicate projects that have been entered into the array and add the hours together.

Eventually I need to know the percentage of each project but I'm not there yet. For now I'm just trying to figure out how I can get the program to read the duplicate project numbers and then add the hours together. Here is what I have so far:

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

int main()
{
string EmployeeFirst;
string EmployeeLast;
string Project;
string Hours;
int Entries;

cout << "Welcome to ___ Costing Allocations\n" << endl;
cout << "Employee First Name: ";
cin >> EmployeeFirst;
cout << "Employee Last Name: ";
cin >> EmployeeLast;
cout << "Number of Entries?: ";
cin >> Entries;

string ProjName[Entries];
int ProjHours[Entries];

cout << "-------------------------";
cout << "\n";

for (int a = 0; a < Entries; a++) {
cout << "Project: ";
cin >> ProjName[a];
cout << "Hours: ";
cin >> ProjHours[a];
}

cout << "\n";
cout << "--------------------------";
cout << "\n";

cout << "Employee: " << EmployeeFirst << " " << EmployeeLast << endl;
cout << "\n";
for (int b = 0; b < Entries; b++) {
cout << "Project: " << ProjName[b] << " " << "Hours: " << ProjHours[b] << endl;
}

}

Here is the output I have:

Welcome to ___ Costing Allocations

Employee First Name: Test
Employee Last Name: Subject
Number of Entries?: 3
-------------------------
Project: 11400
Hours: 8
Project: 11500
Hours: 4
Project: 11400
Hours: 2

--------------------------
Employee: Test Subject

Project: 11400 Hours: 8
Project: 11500 Hours: 4
Project: 11400 Hours: 2


I need the program to recognize that there are two entries for 11400 and add them together. Would anyone be able to help with this? Thanks in advance!!

I think a map is a more appropriate data structure.
http://www.cplusplus.com/reference/unordered_map/unordered_map/

1
2
3
4
5
6
7
8
9
10
11
#include <unordered_map>
...
std::unordered_map <string, int> table;
for (int i = 0; i < entries; i++)
{
    cout << "Project: ";
    cin >> ProjName;
    cout << "Hours: ";
    cin >> ProjHours;
    table[ProjName] += ProjHours;
}


This code gets a project name and number of hours from the user, then checks if table contains the key ProjName. If it doesn't it creates an entry with key ProjName and an hours value of zero. The number of project hours is then added.
Last edited on
Topic archived. No new replies allowed.