Searching Problem

This is a discussion on Searching Problem within the C++ Programming forums, part of the General Programming Boards category; In my other subroutine, I am inputting a person's first and last name. Here, I am merely searching for the ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    Searching Problem

    In my other subroutine, I am inputting a person's first and last name. Here, I am merely searching for the last name. Whenever I input Snum that is supposed to be someone's last name, it gives me the "This person does not have this last name."

    I have tried re-editing and fixing, but to no prevail, can anyone tell me what the problem is?


    Code:
    void searchlast()
    {
    	int I, found,f;
    	char Snum[25][25];
    
    	clrscr();
    	do{ 
    	cout<<"Last Name You Are Searching For: ";
    	cin>>Snum[I];
    	found=0;
    
    	for(I=1; I<=u; I++)
    	{
    		if(namel[I]==Snum[I])
    			{    cout<<"The Last Name " <<Snum<<" Was Found ";
    				cout<<"In: "; cout<<""<<namef[I]; cout<<" "<<namel[I];cout<<"\n";
    				cout<<"This person has an Average Score of: "<<avg[I];cout<<"\n";
    				found=1;
    				
    				}
    
    		if(namel[I]!=Snum[I])
    		{ cout<<"No Student has this Last Name. \n";
    		  }
    
    
    	}
    	cout<<"Search a new #? 1=Yes | 2=No: ";
    	cin>>f;
    	clrscr();		 
            }while(f!=2);

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, arrays start at index 0, not 1. Also, strings, especially C-style strings are not compared using ==. If you're using C-style strings, use strcmp().

    In reality, you should be using C++ strings.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char Snum[25][25];
    Make this:
    Code:
    	std::string Snum;
    And include <string>.
    > cin>>Snum[i];
    Make this:
    Code:
    	cin>>Snum;
    > if(namel[i]==Snum[i])
    Make this:
    Code:
    		if (namel[i]==Snum)

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    swoopy isnt the better way to compare
    Code:
    if (name1[i].compare(Snum) == 0)
    return values for compare()
    0 == exact match
    1 == same length, but other differences may apply (case, spelling)
    -1 == not the same at all
    Not trying to argue, but someone flamed me for it a while back.
    Then again I guess == and compare() are probably the same thing.
    Last edited by Raigne; 05-29-2007 at 11:40 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    std::string::compare returns 0 if the strings are equal... and since 0 == false, your suggestion actually compares if the strings are not equal. In this case, I think that it is better to just use operator ==
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    11
    Fixed:

    Code:
    void searchlast()
    {
    	int I, found,f,ptr;
    	char Sname[25];
    	ptr=0;
    
    	clrscr();
    	do{ 
    	cout<<"Last Name You Are Searching For: ";
    	gets (Sname);
    	cout<<"\n";
    	found=0;
    	
    	for(I=1; I<=u; I++)
    	{       ptr=strcmpi(Sname, MyArr[I].namel);
    		if (ptr==0)
    		{
    			    cout<<"The Last Name " <<Sname<<" Was Found ";
    				cout<<"In: "; cout<<""<<MyArr[I].namef; cout<<" "<<MyArr[I].namel;cout<<"\n";
    
    				found++;
    
    		}
    
    
    
    	}
            	if (found==0)
                    { cout<<"No one has this last name.\n\n";}
    	
    	cout<<"Search a new #? 1=Yes | 2=No: ";
    	cin>>f;
    	clrscr();		 
            }while(f!=2);
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

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