name count

D

Darren

hi,

i have a file, containing a list of names:

Stuart Byrne
Stuart Byrne
Stuart Byrne
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Richard Wyville
Richard Wyville

how can i write a program to read the files and add up how many each one is
mentioned?

the names are dynamic (i.e. are not a set list), and the file is recreated
each day.

Many thanks,

Darren
 
M

Mike Wahler

Darren said:
hi,

i have a file, containing a list of names:

Stuart Byrne
Stuart Byrne
Stuart Byrne
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Antony Bradshaw
Richard Wyville
Richard Wyville

how can i write a program to read the files and add up how many each one is
mentioned?

the names are dynamic (i.e. are not a set list), and the file is recreated
each day.

Many thanks,

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

int main()
{
std::ifstream input("filename");
if(!input)
{
std::cerr << "Cannot open input\n";
return EXIT_FAILURE;
}

std::map<std::string, unsigned int> names;
std::string name;

while(std::getline(input, name))
++(names[name]);

if(!input.eof())
{
std::cerr << "Error reading input\n";
return EXIT_FAILURE;
}

std::map<std::string, unsigned int>::const_iterator it(names.begin());
std::map<std::string, unsigned int>::const_iterator en(names.end());

for( ; it != en; ++it)
std::cout << "The name '" << it->first
<< "' occurs " << it->second << " time"
<< (it->second == 1 ? "" : "s")
<< '\n';

return EXIT_SUCCESS;
}

-Mike
 
D

Darren

Mike,

I can only say...

WOW!

thank you SO SO SO much!!!!

have a great weekend.

Darren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top