The names are in a txt. file and are categorized as 1. malename femalename.
One being the number symbolizing their popularity in 2004. What I've been attempting to program is a program that will read a requested baby name from the user. Scan through all the baby names using an Array and return what popularity the baby name has. Also what I don't have integrated into my programming is the fact that the female and male names are supposed to come up seperately in rankings. ie "Jacob is number 1 in boy name popularity" and "Jacob is not ranked in the girls names."

Edited 5 Years Ago by mcclainra: n/a

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int declared_size =1000;
void fill_array(char n[],int size, int& number_used);

int search(const char n[], int number_used,  char target);

int main ( )
{
    
char arr[declared_size];
int list_size;
fill_array(arr, declared_size, list_size);
char ans,target;
int result;

do
  {
     cout << "Enter the first name that you would like to find the\n";
     cout << "popularity of from the baby names of 2004 \n";
     cout << "Be sure to capitilize the first letter of the name ";
     cin >> target;
     result = search(arr, list_size, target);
     if (result == -1 )
               cout << target << "is not on list";
     else 
               cout << target << " is ranked " <<(result +1 ) << " in popularity";
     
     cout << " search again?:\n";
     cin >> ans;     
     }   
  while  ((ans == 'y' || ans == 'Y' ));
  system ("pause");
  return 0;
}

void fill_array(char n[ ], int size, int& number_used)
{
     ifstream in_stream;
    in_stream.open("babynames2004.txt");
    
    if(in_stream.fail( ))
        {
            cout << "Out Program fail.\n";
            exit (1);
        }
char boy, girl;
int place;
int index =0;
in_stream >>place >> boy >> girl;
while ((place <= 1000) && (index < size))
      {
              n[index]= place;
              index ++;
              in_stream >> place;
      }
}     

int search (const char n[ ], int number_used, int target)
{
int index = 0;
bool found = false;
while (( !found) && (index < number_used))
      if(target == n[index])
                found = true;
      else
          index ++;
if (found)
      return index;
   else
      return -1;
}

Edited 5 Years Ago by Narue: added code tags

Let's start with your data structure:
so far you have a single array of characters; I think you need at least an array of strings. In fact, to store girls' names and boys' names, you probably want two arrays of strings, one called girls_names and one called boys_names. You should at least be using the std::string class to manipulate strings in C++. Depending on where you are in your learning-process, you may also want to consider using the std::vector container instead of C-style arrays. And you may want a higher-level data structure than just two separate lists of names.

Now, looking at your fillArray() function, you have declared boy and girl as a single individual char each. Again, you want each of these to be a string (preferably a std::string, or at least an array of chars). Then, it should be obvious to you, looking at your own code, that you don't actually store either boy or girl into your array.

So start with getting your data-types right, defining useful data structures (useful to solving your problem, not just to holding the data), and getting your data structures loaded from your input file. Once you have that much working, it will be easier to see how to update your search function, and then how to tell if it is doing what it needs to.

This article has been dead for over six months. Start a new discussion instead.