i want to be able to read input from a file and find like entries and then add the corresponding data. i dont know if i explained that well so ill give an example...

the file would be like this (just example numbers)...

111111     15
222222     6
111111     4
333333     2
222222     3

so i want to read that into the program. then i want to program to find that there are two entries for 111111 and add up the 15+4 to get 19 in this example. 222222 would give 0. and 333333 would give 2.

i guess the part where i am getting lost is how to find the like entries. i have never attempted this so a push in the right direction would be a huge help. thanks ahead of time.

I think you may want to use an STL std::map, if that's allowed.

Read the 6-digit numbers into an integer variable such as an "identifier" variable or something of that nature, then read the value into a "value" variable of some sort. Once the data is read, use the std::find() function to determine if you have previously read an identifier/value pair with that same identifier. If the identifier is found, add the new value to the existing value, otherwise add the identifier/value pair to the map.

If you can't use a map, you'll need to use parallel arrays or vectors, then loop through the "identifiers" array/vector to find the identifier. If the identifier is found, use the index of the identifier to access the same element of the "values" array/vector.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.