Help on seaching element in array using poitner C++

This is a discussion on Help on seaching element in array using poitner C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a array string such as a set Code: S= {W,P,A,Z} I only manage to search the first ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9

    Unhappy Help on seaching element in array using poitner C++

    Hi,


    I have a array string such as a set
    Code:
    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?

    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;

    Code:
    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    What is Ptr? which probably should not be a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    &a[0] would be the first thing in an array of (char*)s which... doesn't exist? Stop using pointers. char* is already a pointer
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    Hi,
    I changed a litte bit, but it doesnt work for me ...still only found the first element...

    Code:
    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;
    			}
    	
    	}
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Indent your code properly, e.g.,
    Code:
    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;
            }
        }
    }
    Now, it is clear that your code is equivalent to:
    Code:
    bool Search(char *a, int size, char *which) // Search element
    {
        Ptr b = a;
        for (int i = 0; i <size; i++)
        {
            if (b[i] == toupper(*which))
            {
                return true;
            }
            return false;
        }
    }
    Hence the loop only iterates once, at most.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    Hi ,
    I'm not sure how to proceed next . Can you enlighten me?

    Thankss

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    You need to move that return false; outside of the loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    Thanks, my mistake lol...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing an array in an element?
    By bluej322 in forum C Programming
    Replies: 4
    Last Post: 02-19-2011, 10:36 PM
  2. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-23-2010, 11:09 PM
  3. pointer to first element in array
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 01:52 AM
  4. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 03:08 PM
  5. FILE:string seaching how many times occur
    By Munisamy in forum C Programming
    Replies: 3
    Last Post: 02-26-2005, 01:14 AM

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