A naive implementation would look something like this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
cout<<"Enter a file to open: ";
getline ( cin, line );
ifstream in ( line.c_str() );
if ( in ) {
int count = 0;
while ( getline ( in, line ) ) {
ifstream markstream ( "MarkScheme.txt" );
string mark;
while ( getline ( markstream, mark ) ) {
if ( mark == line )
++count;
}
}
cout<<"The total count is "<< count <<endl;
}
}
A better implementation would look more like this:
#include <fstream>
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> load_file ( const string& filename )
{
ifstream in ( filename.c_str() );
if ( !in )
return set<string>();
string line;
set<string> ret;
while ( getline ( in, line ) )
ret.insert ( line );
return ret;
}
int main()
{
string filename;
cout<<"Enter a file to open: ";
getline ( cin, filename );
set<string> user = load_file ( filename );
set<string> mark = load_file ( "MarkScheme.txt" );
int count = 0;
for ( set<string>::const_iterator it = user.begin(); it != user.end(); ++it ) {
if ( mark.find ( *it ) != mark.end() )
++count;
}
cout<<"The total count is "<< count <<endl;
}
And there would be degrees of improvement as you went away from the manual version to an even more abstracted version.
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004