Thread: Help on seaching element in array using poitner C++

  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
    28,413
    What is Ptr? which probably should not be a pointer.
    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

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    &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,&dwBytesOverwritten);

  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
    28,413
    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.
    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
    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
    28,413
    You need to move that return false; outside of the loop.
    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

  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, 11:36 PM
  2. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  3. pointer to first element in array
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02: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, 04:08 PM
  5. FILE:string seaching how many times occur
    By Munisamy in forum C Programming
    Replies: 3
    Last Post: 02-26-2005, 02:14 AM