Jump to content

Help on seach element in array using poitner C++

- - - - -

  • Please log in to reply
5 replies to this topic

#1
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi,

I have a array string such as a set S= {W,P,A,Z}

I only manage to search the first element which is W , but not for others ( it will say not found if i search for P, for instance), here is my code for searching part:
whats wrong with my code?

cout <<"\nWhich element?: ";

						cin >>which;

						

						if((Search(&a[0], size, which)==true))

						{

							cout <<which<<" is in S ."<<endl;

						}

						else

						{

							cout <<which <<" is not in S."<<endl;

						}

						break;

bool Search(char *a, int size, char *which) // Search element 

{

	Ptr c =&which[0];

	Ptr b= &a[0];

	

	for (int i=0;i <size;i++)

	{

	

		if (b[i]==*c)

		{

				

			return true; // true if found

			

		}

			

		else

		{

			return false;

		 // not Found element.

		}

		

	}

	

	

}

Thanks:crying:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You return out of the method on the first comparison, which forces you out of the loop to check the next. You should use a temporary variable to store if you found it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Quote

You return out of the method on the first comparison, which forces you out of the loop to check the next. You should use a temporary variable to store if you found it.
or you can let the loop run until you find your char:
bool Search(char *a, int size, char * which) // Search element 
{
    char * s = a;
    
    for (int i = 0; i < size; i++)        
        if (s[i] == toupper(*which)) //just for case                    
            return true;      //return if found; if not found iterate again               
    return false;        //loop terminates; char still not found
}

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts

WingedPanther said:

You return out of the method on the first comparison, which forces you out of the loop to check the next. You should use a temporary variable to store if you found it.

Hi , Do you mean like this? but it still doesnt work for me..

bool Search(char *a, int size, char *which) // Search element 

{

	

	Ptr b= a;

	int flag =0;

	for (int i=0;i <size;i++)

	{

	

		if (b[i]==toupper(*which))

		

		{

			flag =1;

				

			return true;

		}

			

			if(flag==1)

			{	 

		

			return true;

	

			}

			else

			{

			return false;

			}

	

	}

}

:crying:

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
When you set flag to 1, you also return true. Think how return works/when should you call it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Did you look at the code I posted above. I think what they are saying is you dont need the else condition since all you are going to do is return not found. Look at the algorithm:
as long as size is good
{
    compare the chars for equality
       if they are equal: do something; in your case return true
       else { /*based on what you are doing; the else here can be left out*/ }
        /*back to the top of the loop*/
}
Loop terminated and char not found; now you return false

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users