Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
>char accountNum[6];
If you're using std::map, there's probably no restriction on using std::string as well. I'd highly recommend avoiding C-style strings because they're just too awkward. While C++ isn't known for it's string handling capabilities, std::string is still a vast improvement.
>map report;
Assuming std::string: std::map<std::string, int> report; . You don't need to provide a comparator because std::string already supports operator<.>while (!inLN.eof())
*sigh* I can't believe I'm still seeing this bug. getline returns the stream, and you can test the stream directly to get the state. Not only is is correct, it's SHORTER! No excuses.
>report[accountNum] = amount;
Close. According to your requirements, you want to add all of the amounts for matching account numbers together. It's just as intuitive as one would hope, because std::pair is smart about initializing its members:
std::map<std::string, int> report;
std::string account;
while (std::getline(inLN, account)) {
int amount;
inLn>> amount; // Assuming a correct file format
// Add to whatever was there, even a new addition
report[account] += amount;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Step 1: Declare variables
std::map<long,long> report;
std::pair<long,long> info;
std::ifstream file("data.txt");
Step 2: Read in data
while( file >> info.first >> info.second ){
/ * do stuff*/
}
Step 3: Apply algorithm
while( file >> info.first >> info.second ){
std::map<long,long>::iterator itr = report.find( info );
if(itr == report.end() ){ /* if not found then add it */
report.insert(info);
}
else{ //else there is a match
/* do logic here if match occurs, I'll leave you to implement this */
}
}
Step 4: Give me $100 through paypal
Nice doing business with you.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608