I'm sorry, I didn't realize I couldn't upload the exe file - but I have it ready to email.

I'm having a big problem understanding how to complete this assignment and would really appreciate someone's help in getting me started.

Basically, we are taking baby names from a file and opening the file up in our program. we are using arrays (I believe a 2 dimensional array) and taking it from a file named babynames2004.txt and putting that data into the array.

this program is supposed to output to the screen:
"Enter the first name that you would like to find the
popularity of from baby names in 2004.
Be sure to capitalize the first letter of the name."

if you enter Melissa, for example, it will output:

"Melissa is not ranked among the top 1000 boy names.
Melissa is ranked 100 in popularity among girls
Press any key to continue..."

I have attached the text file fo anyone willing to help me continue the project.

I have to load the boy names and the girl names from the file into separate arrays and search for the target name from the arrays, not directly from the file.

here is what I have so far, someone pleeeease help me :)

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





int main
{
    ifstream in_put;
    char enter_name
    
    in_put.open("babynames2004.txt");
    if (in_put.fail())
    {
       cout << "Input file opening failed.\n";
       exit(1)
    }
    
    cout << "Enter the first name that you would like to find the\n;"
         << "popularity of from baby names in 2004.\n;"
         << "Be sure to capitalize the first letter of the name.\n"
    cin >> enter_name;
    
    int index = 0, count = 0;
    char boys_name[1000][10], girls_name[1000][10];
    char data;
    
    while (in_put.get(data))
    {     
          boys_name[index][count] = data;
          girls_name[index][count] = data;
          
          for (count += 1; count < 10; count ++)
          {
              char boys_name[index][count] = input.get(data);
              if (data == ' ')
                 char girls_name [index][count] = in_put.get(data);
                 
    }
    
    
    
    
    
    
    system("Pause");
    
    return 0;
    
}

Dani AI

Generated

was right that the file is arranged as rank + boy-name + girl-name on each line, and correctly pointed out reading the three fields into a record is the right idea. For the assignment (load boy/girl names into separate containers and search those containers), a simple, robust pattern is:

  • parse the file once and store names with their rank (fast lookup),
  • normalize the queried name (capitalize first letter, lower the rest) to match the file formatting,
  • look up the name in the boy and girl containers and print the appropriate message.

Using a map makes the lookup trivial and keeps the code short and clear; if the instructor strictly requires arrays, the same parse step can fill an array of structs and a linear search can be used instead.

Example parsing + lookup (keeps parsing defensive against blank lines and CRLF):

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::ifstream in("babynames2004.txt");
    if (!in) { std::cerr << "Cannot open file\n"; return 1; }

    std::map<std::string,int> boys, girls;
    std::string line;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::istringstream iss(line);
        int rank; std::string b, g;
        if (!(iss >> rank >> b >> g)) continue;
        if (!b.empty() && b[b.size()-1] == '\r') b.resize(b.size()-1);
        if (!g.empty() && g[g.size()-1] == '\r') g.resize(g.size()-1);
        boys[b] = rank; girls[g] = rank;
    }

    std::string name; std::cout << "Enter name: ";
    if (!(std::cin >> name)) return 0;
    for (size_t i = 0; i < name.size(); ++i) name[i] = std::tolower((unsigned char)name[i]);
    if (!name.empty()) name[0] = std::toupper((unsigned char)name[0]);

    std::map<std::string,int>::iterator itb = boys.find(name);
    if (itb != boys.end()) std::cout << name << " is ranked " << itb->second << " among boys.\n";
    else std::cout << name << " is not ranked among the top 1000 boy names.\n";

    std::map<std::string,int>::iterator itg = girls.find(name);
    if (itg != girls.end()) std::cout << name << " is ranked " << itg->second << " among girls.\n";
    else std::cout << name << " is not ranked among the top 1000 girl names.\n";
}

Troubleshooting notes drawn from the thread: avoid reading char-by-char or using raw char arrays for whole words; use std::string. Do not try to compare or assign the stream object to a string (streams and strings are different types). Watch for missing semicolons and other small syntax errors. If the assignment forces use of fixed arrays, fill an array of structs (rank, boy, girl) during parsing and run a simple loop to compare each stored name to the normalized query.

Recommended Answers

All 6 Replies

>>I'm sorry, I didn't realize I couldn't upload the exe file - but I have it ready to email.
That's ok We don't want you uploading exe files because (1) they can contain viruses and worms and (2) we can compile the code ourselves on our local computer to test it out.

line 13: you should declare that as std::string not char. What you declared is just one single character and I doubt very seriously that anyone would name their baby a single character.

line 29: like line 13 all you declared was a single character. Why read the file one character at a time when you could read it one word or line at a time.

I see that each line of that text file contains 3 words: a number indicating rank and two names. That means what you need to do is to create an array of structures that contain those three items.

struct line
{
     int rank;
     std::string name1;
     std::string name2;
};

now you can create an array of the above structures struct line names[1000]; Now to read those structures into the array

std::string name1, name2;
int rank;
int count = 0;
while(count < 1000 &&  in_put >> rank >> name1 >> name2)
{
    names[count].rank = rank;
    names[count].name1 = name1;
    names[count].name2 = name2;
    ++ count;
}

thank you very much for your help.

so should I delete what I've written and replace it with what you've typed? I'm not sure what to take out and what to leave in.

nvm I commented out what I was doing and fixed something, and it compiled :)

now I'm trying to get it to display some results, and it isn't working -

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





int main()
{
    ifstream in_put;
    std::string enter_name;
    char data;
    
    in_put.open("babynames2004.txt");
    if (in_put.fail())
    {
       cout << "Input file opening failed.\n";
       exit(1);
    }
    
    cout << "Enter the first name that you would like to find the\n"
         << "popularity of from baby names in 2004.\n"
         << "Be sure to capitalize the first letter of the name.\n";
    cin >> enter_name
    


struct line
{
     int rank;
     std::string name1;
     std::string name2;
};

struct line names[1000];

std::string name1, name2;
int rank;
int index = 0, count = 0;
while(count < 1000 &&  in_put >> rank >> name1 >> name2)
{
    names[count].rank = rank;
    names[count].name1 = name1;
    names[count].name2 = name2;
    ++ count;
}

if (in_put = name1)
	cout << in_put << " is ranked " << rank <<  " in popularity among boys.\n";
else 
	cout << in_put << " is not ranked among the top 1000 boy names.\n";

if (in_put = name2)
	cout << in_put << " is ranked << rank << " in popularity among girls.\n";
else 
	cout << in_put << " is not ranked among the top 1000 girl names.\n";
    
    
    
    
    
    
    system("Pause");
    
    return 0;
    
}

lines 51 and 52: you can't use an ifstream (in_input) like that.

How do you distinguish boy names from girl names? Or is one colums boys and the other girls? After reading the file your program needs to scan through the list and count the number of times each name appears.

[edit]I went ahead and wrote the program (I'm not going to post it though). I found out that text file does not contain any duplicate names, unless of course my program is wrong. So I don't know how you can find the most popular girs and boys names [/edit]

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.