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 22274

This 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!

Before you start storing and analysing, make sure you can actually read the file successfully.

Say by being able to print out the following as each line is read.
Rank = 1, bName=Michael, count=33316, gName=Amanda, count=23744

And for each invalid line (you might have to add some yourself to test with), print out a suitable message.

When that's done, then you can think about storing the data, then implementing the search/lookup functions.

Thanks, Salem. I still am having a hard time beating out how that would translate into code. Would I be using something like

inFile.substr(

to return a substring of the array? Thanks for any help.

Sorry to bump, but could any one guide me further? I'm stuck. :P

Files don't have substr methods.

Have you done the first step - reading in the file?

Post some code.

I am sorry I am not sure how to read it in.

Okay, I am able to read into the file and pull out the contents of the entire file by using this code:

// Include Libraries
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;


int main () 
  {
  // This will contain the data read from the file
  string line; 
  // Opening the file
  ifstream myfile ("NamesOf1980s.txt"); 
  
   //if the file is open
  if (myfile.is_open()) 
  
    {
      //while the end of file is NOT reached
      while (! myfile.eof() ) 
      {
        // Get one line from the file
        getline (myfile,line); 
        //and output it
        cout << line << endl; 
      }
    //closing the file
    myfile.close(); 
    
    }
  //if the file is not open output this:
  else cout << "Unable to open file"; 
  
  
  return 0;
  
  }

How would I proceed? Thanks so much for the help - I really appreciate it!

First, a small mistake

while (! myfile.eof() ) 
      {
        // Get one line from the file
        getline (myfile,line);

Should just be

// Get each line from the file
      while ( getline (myfile,line) ) 
      {

Next thing you need is to store the lines.
I would suggest std::vector< std::string > lines; http://www.cppreference.com/wiki/stl/vector/start
Look up the push_back() initially.

When you've read the file, practice printing out the lines by accessing the vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.