Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# comparing 2 columns of two files, then do substraction if 2 columns equal

Posted on 2006-06-21
Medium Priority
202 Views
Hi experts,

Two files, file1, file2

file1
Store id          sale amt
123               100
234               200
345               300

file2
store id          sale amt
345               50
123               60
234               70

What I want to do is comparing the store id,
if (file1.store id == file2.store id)
then file1.sale amt - file2.sale amt

so the correct results should be
store id   sale increase
123         40
234         130
345         250

But I got wrong results like
store id   sale increase
123        150
234        140
345        230

I use
set<string> stores
string file1storeid=file1.store id
string file2storeid=file2.store id
stores.insert(file2storeid)
if (stores.find(file1storeid) == stores.end()))
{
file1.sale amt - file2.sale amt
}

0
Question by:justinY
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 17

Accepted Solution

rstaveley earned 500 total points
ID: 16964860
Try using the store id  as a key to a map.

e.g.

typedef string StoreId;
typedef double SaleAmount;
typedef map<StoreId,SaleAmount> Database;

Database db;

// Process file 2 first
while (/* read record from file */) {

StoredId storeId = /* */;
SaleAmount saleAmount = /* */;
db[storeId] = saleAmount;
}

// Then process file 1
while (/* read record from file */) {

StoredId storeId = /* */;
SaleAmount saleAmount = /* */;
db[storeId] -= saleAmount;  // Subtract the sale amount - database now stores the difference
}

// Then generate the report, by iterating through the database

for (Database::const_iterator itr = db.begin();itr != db.end();++itr) {

const StoredId& storeId = itr->first;
const SaleAmount& saleDifference = itr->second;
cout << /* */;
}
0

Author Comment

ID: 17004970
Thank you so much. It works
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll