954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

figuring out how to use a map

so i am trying to write a program that reads some data in from a text file and performs an operation on that data. the text file is as follows...

611111     5
765676     4
876787     4
987897     2
611111     4


now i want the program to read that and see that 611111 is there twice and will add its corresponding values so 611111 will then have a corresponding value of 9. i did some research and found out a map is probably what i need to do this but thats where i am getting confused. im not exactly sure how to read the data into the map from a text file and then how to pick out if there are any similar numbers in the first column.

im not even sure why i am posting what i have so far since i know it is probably embarressingly wrong (especially since i have been working on this for a couple days now). thank you ahead of time!!!

oh and i am using dev-c++ so i cant use strings

char accountNum[6];
int amount;

map<char,int , ltstr> report;

while (!inLN.eof())                      

{             
    inLN.getline(accountNum,6);
    report[accountNum] = amount;
                            
}
helpme87
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

@OP >oh and i am using dev-c++ so i cant use strings

You should really consider using something besides dev-c++. Code::Blocks is a common suggestion. You should also know that dev-c++ isn't a language or a standard, so, aside from convenience for the classroom, it has no crucial place in the world of c++ programming.

@Narue >std::pair is smart about initializing its members
Hmm...true, but I think you should say std::map. This is a bit confusing for the OP.

Also, I would like to emphasize that it is important to declare amount as an int or other numeric type. If you declare it a string because you split a getline() instead of using the istream >> operator, saying [icode] report[account] += amount will give you concatenations of the inputs.

dusktreader
Posting Whiz in Training
259 posts since Jan 2010
Reputation Points: 152
Solved Threads: 41
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: