string array search

This is a discussion on string array search within the C++ Programming forums, part of the General Programming Boards category; Hi to all C++ programmers, I am at collage and studding computer science. I have problem to solve but my ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    string array search

    Hi to all C++ programmers, I am at collage and studding computer science. I have problem to solve but my code have a error. Basically I have to read a file with 1000 boys and girls names being ranked , load them in to arrays and then after user type a name the program have to search those arrays and find the name and display it along with its ranking. File:
    1 Jacob Emily
    2 Michael Emma
    3 Joshua Madison
    4 Matthew Olivia
    5 Ethan Hannah
    6 Andrew Abigail
    7 Daniel Isabella
    8 William Ashley
    9 Joseph Samantha
    10 Christopher Elizabeth

    When I compile my code assigning tagetName as char it compiles but no output, when I assign it as string it gives error that it can transfer int to char in that line ; if(targetName == boys[i])
    I am caind of stcuk at this point, I will be thankfull if someone can give me some tip or if I need to change the way I have done the code.
    Thanks
    code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    int main( )
    {
    	char boys[1000], girls[1000];
    	int count = 0, boyRank = 0, girlsRank = 0, num;
    	char targetName[16];
    cout <<"Type name to search\n";
    cin >> targetName;
    
    	ifstream inFile;
    	inFile.open ("babynames2004.txt");
    
    		if (inFile.fail( )) 
    		{ 
    			cout << "Input file opening failed.\n"; 
    			exit(1); 
    		} 
    
    while(! inFile.eof( ))
    {
    	inFile >> num;
    	inFile >> boys[count];
    	inFile >> girls[count];
    	count++;
    }
    for (int i = 0; i < 1000; i++)
    {
    	{
    	if(targetName == boys[i])
    		boyRank = i+1;
    	break;
        }
    	{
    	if(targetName == girls[i])
    		girlsRank = i+1;
    	break;
    	}
    }
        if(boyRank != 0 && girlsRank !=0)
    cout << targetName <<" is ranked "<< boyRank <<"among boys.\n";
    cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n";
    	else if(boyRank != 0 && girlsRank = 0)
    cout << targetName <<" is ranked "<< boyRank <<"among boys.\n";
    cout << targetName <<" is not ranked among to 1000 girl names.\n";
    	else if(boyRank = 0 && girlsRank != 0)
    cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n";
    cout << targetName <<" is not ranked among to 1000 boy names.\n";
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You need a 2D array to hold an array of names. One dimension is the number of names (1,000 in this case) and the other dimension is the max length (+1) you expect for a name.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    I am not allowed to use 2d arr at this point, any way I found the problem, it was in the declaration of targetname and my brackets of the if loops were off place, working now.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    #1
    Code:
    while(! inFile.eof( ))
    {
    	inFile >> num;
    	inFile >> boys[count];
    	inFile >> girls[count];
    	count++;
    }
    Don't use end-of-file testing to control a loop in such a way. Instead it should be this:
    Code:
    while(count < 1000 && inFile >> num boys[count] >> girls[count])
    {
    	count++;
    }
    When your last line is read from the file, the eof test will still return false and you will therefore enter the loop even though you're at the physical end of file. Once you're in the loop it's already too late. Your code is already attempting to process data... the reads at this point would fail since there is no more to read but you still increment count and so when the loop does then exit your code is likely in a state where you're left thinking that there is one more element than there truly is (provided you actually used the count variable in such a way... see #2 below).

    The code above effectively tests the state of the stream (inFile) as it attempts to read from the file. Any problems encountered during this read will cause this test to return false and cause the loop to exit.

    The count < 1000 part makes sure that you stop after reaching 1000 names since your array cannot handle more than that number. Otherwise you run the risk of a crash because you overrun the bounds of your arrays.



    #2
    Code:
    for (int i = 0; i < 1000; i++)
    {
    	{
    	if(targetName == boys[i])
    		boyRank = i+1;
    	break;
        }
    	{
    	if(targetName == girls[i])
    		girlsRank = i+1;
    	break;
    	}
    }
    You have this handy count variable that has the number of records read, you don't need to search through more array slots than you have values read from the file. No point searching 1000 array elements if you've only read 10 rows from the file... it's inefficient.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Hi, thanks for the fast response, actually this 10 rows are only 10 of 1000 rows I have in a file, that’s way I have the 1000, but you were right for the < 1000 part, it fixed one of the critical errors. Working now after some more changes…

  6. #6
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    I was just wondering why not to use std::vector of std::strings here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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