Thread: BabyNames

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    BabyNames

    I have an assignment for C++ beginners class. It specifies that my program must read in the names of .txt file. The program is called BabyNames and upon prompting the user to enter a name, it will take in the name, compare it to the .txt file and either find a match among the boy and girl names (or both). If it finds a match it should output the rank of the name. it should also indicate if there is no match. the .txt file looks like this....
    1 Jacob Emily
    2 Michael Emma
    3 Joshua Madison
    4 Matthew Olivia
    5 Ethan Hannah
    and so on and so forth for 1,000 total lines.
    Im using visual studio 2010 as my compiler, and here is what I have so far. Need help.


    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <cstdlib> 
    
    int main ( )
    {
     using namespace std; 
    
     string inputName, boyName, girlName;
     ifstream babyFile;
     int rank;
     char another = 'y';
     
     while (another =='y')
     {
      babyFile.open("babynames2004.txt");
      // verify that it opened
      if (babyFile.fail( ))
      {
       cout << "Can't open babynames2004.txt\n";
       exit(1);
      } 
    
      cout << "Enter the name to search for: ";
      cin >> inputName; 
    
      while (babyFile >> rank)
      {
       babyFile >> boyName;
       babyFile >> girlName;
       if (inputName == boyName)
       {
        cout << inputName << " is ranked "
         << rank << " in popularity among boys.\n";
       }
      }
      babyFile.close();
      babyFile.clear();
      cout << "Try another name? ";
      cin >> another;
     }
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    .........

    I guess nobody can figure out why this program just keeps asking me to enter another name?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are impatient. That is all I have to say.

    EDIT:
    You will have to be more specific about what is not working.
    Enter a male name does indeed work.
    Entering a female name does not.
    Nor does it work if it doesn't find a match.
    Last edited by Elysia; 04-06-2011 at 01:03 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Tachyon22 View Post
    I guess nobody can figure out why this program just keeps asking me to enter another name?
    I don't know what you mean. This is what I get:

    Code:
    Enter the name to search for: Jacob
    Jacob is ranked 1 in popularity among boys.
    Try another name? y
    Enter the name to search for: Michael
    Michael is ranked 2 in popularity among boys.
    Try another name? y
    Enter the name to search for: Emma
    Try another name? n
    It asks for names until something other than 'y' is the response to "Try another name?". Seems functional to me. Of course, it's not outputting the popularity of girls' names or indicating no match was found, but it looks like that part hasn't been attempted yet.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pretty much the same as this other thread
    Please help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed