Hello everyone.
I am working on my lab assignment and am somewhat stuck on how to proceed. We are studying using the book Starting out with C++: From Control Structures through Objects by Gaddis.
The lab assignment is as stated:
NamesOf1980s.txt is a text file from the SSA detailing the baby names. After the first three lines (which provide unimportant header information), each line details a rank and a pair of names with frequency: One for the boy name, one for the girl name. For example, the first two lines of data (the 4th–5th lines of the file) read as so (tabs separate the entries):
1 Michael 33316 Amanda 23744
2 Christopher 27697 Jennifer 22274This indicates that the most popular boy name was Michael with 33,316 boys named that decade while the number 2 boyʼs name was Christopher with 27,697. Similarly, the most girls name was Amanda with 23,744 names with Jennifer following with 22,274.
For your assignment, you will create a program that will read all the names, ask a user for the beginning of a name (such as “R”, “Ro”, and “Rob”) and a gender, then print out all the names that start with that prefix and match the gender from most to least frequent (the order the file uses). You must validate all input (so no numbers or punctuation in a name, names must start with an uppercase letter, choose only valid gender). You may use either C-strings or the C++ string class.
The text file has over 1000 rows in it, and as you can see, about 5 columns (see attached file). I am certain that I will need to use a 2D array, and am thinking the easiest thing to use would be the C++ string class.
Here is what I have so far:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// Main function
int main()
{
// Read from the file
ifstream inFile; // Create an input file stream.
inFile.open("NamesOf1980s.txt"); // Use it to read from a file
in >> rank; // Read the first item from the file into an integer variable x.
in >> nameBoy;
in >> numberBoy;
in >> nameGirl;
in >> numberGirl;
const int SIZE=30;
char name[SIZE];
char gender[SIZE];
char array[1004][5]; // Text file has 1004 rows, 5 columns
inFile.getline(name, SIZE);
The example output screenshot shows the following:
Please enter the start of a name or full name: Ron
Please enter a (b)oy or a (g)irl: b
Ronald (2332)
Ronnie (538)
Ron (116)
Ronny (47)
I am unsure of how to proceed with the setup of this assignment. We have also only recently touched on the C++ string class, so I am not strong in this area.
Thanks for the help!