CS 200 - Lab 24

Baby Name Database

In this lab, you will write a program to perform queries on a file containing baby name popularity data from the Social Security Administration.

Getting Started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download lab24.zip.  Import it into Visual Studio (File->Import...).

Add your code to the source file called BabyNames.c.

Your task

This is the input file:

babynames.txt

Save it in the root folder of your H: drive.

Here is an example run of the program (user input in bold red):

Input file: H:/babynames.txt
Name? Hubert
Male (M) or Female (F): M
Decade? 1900
In the 1900s, the name Hubert was ranked 122

Another run:

Input file: babynames.txt
Name? Harriet
Male (M) or Female (F): F
Decade? 1940
In the 1940s, the name Harriet was ranked 183

One more:

Input file: babynames.txt
Name? Cadwalader
Male (M) or Female (F): M
Decade? 1900
I did not find the name Cadwalader in the 1900s

Hints

Each line of the file represents the popularity of a particular name (male or female) in a particular decade of the 20th century.  (Here is the first line of the file:

1900 1 M John 84602

This line indicates that John was ranked number 1 for male baby names in the 1900s.  (The last field is the number of babies recorded as named John in the 1900s.)

You can read an entire line of input from the file as follows:

int decade, rank, count;
char sex;
char name[200];

...

if (fscanf(in, "%i %i %c %s %i", &decade, &rank, &sex, name, &count) == 5) {
/* one line from the file was read successfully */
}

This assumes in is a variable containing the file handle of the file you're reading from.

You can stop reading lines from the file once the file handle that is opened to read from the baby name database file has reached end-of-file.  (I.e., when passing the file handle to the feof function returns true.)