"foker" <br************@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
:I havent learned STL containers or maps yet. Still having a few
: problems.
:
: while(inFile >> target)
: for(int k = 0; k < SIZE; k++)
: {
: if(keywords[k] == target)
: {
: temp = k;
: num_of_times_read[temp] += counter++;
You probably want to increment this only once,
not by the total previous count of all hits.
Therefore:
num_of_times_read[temp] += 1; // or just ++notr[k];
: }
: }
: for(int index = 0; index < SIZE; index++)
: cout << num_of_times_read[temp];
Did I not warn you, in my previous post, that you should
not declare variables at the beginning of your function,
but as late as possible (and in the innermost scope) ?
Here you are misusing the variable 'temp' within the second
loop, instead of index.
: return 0;
: }
:
: What I want it to do is read in every word from the document and test
: it against every element in keywords array, if it matches i want the
: index # from keywords assigned to a temp variable, then i want
: num_of_times_counted[temp] to hold the number of times it was counted
: each time it hits that keyword in the document. To me it makes complete
: sense but it doesn't work haha, what am I doing wrong here?
:
: say, target matches index #6 in keywords array. I want temp = 6, so
: num_of_times_read[6] = count++. can I do this?
Yes. Actually "noone"'s reply hinted towards something closer to what
you are looking for.
Here's a quick example thrown together:
#include <fstream>
#include <string>
#include <map>
using namespace std;
void printCounts(ifstream& inFile, string keywords[], int size)
{
typedef map<string,int> Cnts; // sorted keyword -> its count
Cnts cnts;
// fill up a map for faster search than with array
for( int i = 0 ; i != size ; ++i )
cnts[keywords[i]] = 0;
// read file, for each word check if it is within our map
string target;
while( inFile>>target ) {
Cnts::iterator const pos = cnts.find( target );
if( pos != cnts.end() ) { // actually found
++pos->second; // increment the count
}
}
// now print keyword counts in original order
for( int i = 0 ; i != size ; ++i )
cout<< keywords[i] << " -> " << cnts[keywords[i]] <<endl;
}
Not tested, but will hopefully at least compile.
The best way to write such a function depends on what exactly
you want to do...
: btw, counter += std::count( keywords, keywords+SIZE, target );
: gave a ton of errors.
Well, there are many reasons for which this might have happened.
Good luck,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form