473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

name count

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
Jul 19 '05 #1
2 1976

"Darren" <fo*****@world.com> wrote in message
news:EA*****************@newsfep3-gui.server.ntli.net...
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
Jul 19 '05 #2
Mike,

I can only say...

WOW!

thank you SO SO SO much!!!!

have a great weekend.

Darren

#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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
1
by: discomiller | last post by:
Mario Mueller: Hello *, radiobuttons belong to other radiobuttons by the "name="any_value"" attribut. Thats a fakt. I got the following XML:...
18
by: war_wheelan | last post by:
I am very new to Transact-SQL programming and don't have a programming background and was hoping that someone could point me in the right direction. I have a SELECT statement SELECT FIXID,...
5
by: Theresa Hancock via AccessMonster.com | last post by:
I have an Excel table I need to import into Access. The name is entered into one field "Name". I'd like to have two fields in Access, FirstName and LastName. How do I do this. -- Message posted...
7
by: Anushya | last post by:
Hi How to get the id of a name in contact items in outlook. How to do it thru redemption in .net?? i tried the code below. but it shows the error. pls have a look at the code ...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
2
by: John | last post by:
Hello, I need some help with something simple. How would I be able to display two or three names in an array? So, if I have an array calle Names = { 10, 10, 10 }; //Names can hold up to three...
4
by: Wayne Shu | last post by:
Hey, guys. There is a problem. e.g. template <typename T> class foo { public: foo();
6
by: inamul | last post by:
Hi Guys, I am am trying to validate multiple check box. It works fine when checkbox name is like chkSectionInterested but when i add to the name chkSectionInterested, then i can validate any...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.