Solved

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

Posted on 2006-06-21
2
196 Views
Last Modified: 2010-04-01
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
}

Please help - Thanks
0
Comment
Question by:justinY
2 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 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

by:justinY
ID: 17004970
Thank you so much. It works
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 121
Getting IP address 8 78
How to convert MFC APP to Win32 APP. 19 79
trigs fail! I thought I knew how to do trignometry 3 65
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question