Please help.

This is a discussion on Please help. within the C++ Programming forums, part of the General Programming Boards category; I have an assignment for C++ beginners class. It specifies that my program must read in the names of .txt ...

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

    Please help.

    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Works fine here.

    Make sure you are inputting a boy's name.

    Make sure that the file is in the programs current working directory.

    What about checking whether it manages to read any names from the file? Why not load data in a vector and check whether it is not empty?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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

    Updated.

    I updated the code slightly, and the only issue now is that the program reads in the list of names and if you type a name from rank 1, it outputs correctly. If you enter a boy or girl name from rank 2-1000, it says it isn't ranked. Is there something wrong with the code? Thank you for your 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";
       }
       else if (inputName == girlName)
       {
    	   cout << inputName << " is ranked "
    		   << rank << " in popularity among girls.\n";
       }
       else
       {
    	   cout << inputName << " is not ranked.\n";
       }
       break;
      }
      babyFile.close();
      babyFile.clear();
      cout << "Try another name? ";
      cin >> another;
     }
     return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Wild guess the end of line is causing your program to stop or crash.

    Tim S.

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

    huh?

    It isn't stopping or crashing though. It just acts as if it can't read below the first line of the .txt file it's linked to

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Is there a newline after each group in the file? If so you will have to read past it, before getting your next rank. Try adding another read right before your break statement.


    char trash;
    babyFile >> trash;
    break;

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

    Unsuccessful

    I tried what you suggested, to no avail. When entering "trash" there. The program exits with code "1". Tried having it read in the file again before the break also, and it has the same result as no change in the current program.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I would visually check each of the reads to see what is in the buffer. Read and then print it to your console so you can see what is in the buffer.

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

    In simple terms?

    Sorry, I don't follow. I understand what your saying, just not how I should go about doing that. I'm really new to programming. Thanks Raigne

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Do a loop through the file without the users input, and just output the contents of the file with std::cout. See if you get the amount of reads you expect.

    You could also read the file as a whole, then just tokenize it in memory. This is sometimes easier than dealing with tokenizing through ifstream.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,895
    Let's look closely at this loop:
    Code:
    		while (babyFile >> rank)
    		{
    			babyFile >> boyName;
    			babyFile >> girlName;
    			if (inputName == boyName)
    			{
    				cout << inputName << " is ranked "
    				     << rank << " in popularity among boys.\n";
    			}
    			else if (inputName == girlName)
    			{
    				cout << inputName << " is ranked "
    				     << rank << " in popularity among girls.\n";
    			}
    			else
    			{
    				cout << inputName << " is not ranked.\n";
    			}
    			break;
    		}
    Look at that last else statement. What happens if the name entered is not on the first line in the input file?

    Hint: It will print "cout << inputName << " is not ranked.\n";"

    Next you have a break statement. This statement will end this loop, even though there is more data in the file.

    Is that really what you want?


    Jim

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    wow, i didnt even think to check the if block... wow.

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

    Suggestions?

    Makes since...but how do I make it read past the first line? What
    is the code for that?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,895
    First you only want to leave this loop if you are finished processing the file. Or you find a match.

    What is causing you to leave the loop early?

    How can you exit a loop when the boy's or girl's name is found?

    The only time you should print out the not ranked message is after you process the entire file and don't find a match.

    Give it a try. If you can't figure it out then post specific questions based on your trials.

    Jim

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21