I have a funny feeling this is a stupid question so bear with me. I'm passing a string array to a function to search for elements in it. I have what is written below, and all it does is print "Not Found" five times. I'd also like it to print out each occurrence of the target. I've been playing with it for too long and am getting nowhere. I've echo printed all of the variables to be sure they're being read but somethings still not right. I'd appreciate any help. Thanks.

void searchString(string A[],int size, string target)
{
	int j;
	{
	for(j=0; j < size; j++)
		if(A[j] == target)
			cout << A[j] << endl;
		else cout << "Not Found";
	}
}

Your function seems correct, as the following full example shows. What is in the array you pass to the function, and how are you passing it?

#include <iostream>
#include <string>
using namespace std;


void searchString(string A[],int size, string target)
{
	int j;
	
	for(j=0; j < size; j++)
		if(A[j] == target)
			cout << A[j] << endl;
		else 
         cout << "Not Found" << endl;
}

int main ( )
{
   string arr[5] = { "goodbye", 
                     "hello", 
                     "smile",
                     "hello",
                     "I quit" };
   string target = "hello";

   searchString( arr, 5, target );

   return 0;
}

passing like this:

searchString (sTemp, 5, answer)

And the array contains the following:

string airLine[5] = {"Nadir", "Acme", "Acme", "Acme", "Nadir"};

sTemp is a copy of the airLine array. I've substituted airLine in the call and had the same results.

And what exactly are you passing for the answer variable? Is it in fact an exact match to any of the strings in the array?

Please post full information - this isn't supposed to be a guessing game.

Program is supposed to be an database that responds to SQL-type commands. So the first choice is what you'd like to see (Flight numbers for example) from an airline(again for example). So choosing flight numbers, and then Nadir, should return
122
129
I've been testing what I have so far so a few option are missing until I get over this hurdle.

Full code:

#include<iostream>
#include<string>

using namespace std;


void searchString(string A[],int size, string answer)
{
	int j;
	
	for(j=0; j < size; j++)
		if(A[j] == answer)
		
			cout << A[j] << endl;
		   
		
		else cout << "Not Found"<< endl;
	
}

int main()
{
	string airLine[5] = {"Nadir", "Acme", "Acme", "Acme", "Nadir"};
	string flightNum[5] = {"122", "221", "122", "323", "199"};
	string gate[5] = {"34", "22", "33", "34", "13"};
	string destination[5] = {"Detroit", "Denver", "Anchorage", "Honolulu", "Detroit"};
	string departTime[5] = {"08:10","08:17", "08:22", "08:30", "08:47"}; 
	string sTemp[5];
	

	int firstChoice;
	int secondChoice;
	string answer;

	cout << "Welcome to the Flight Database" << endl;
	cout << "Please choose the category you'd like to see from the following list." << endl;
	cout << "1. Airline" << endl;
	cout << "2. Flight Number" << endl;
	cout << "3. Gate" << endl;
	cout << "4. Destination" << endl;
	cout << "5. Departure Time" << endl;

	cin >> firstChoice;
	switch(firstChoice)
	{
	case 1:     sTemp[0] = airLine[0];
				sTemp[1] = airLine[1];
				sTemp[2] = airLine[2];
				sTemp[3] = airLine[3];
				sTemp[4] = airLine[4];
				
				break;
		case 2: sTemp[0] = flightNum[0];
				sTemp[1] = flightNum[1];
				sTemp[2] = flightNum[2];
				sTemp[3] = flightNum[3];
				sTemp[4] = flightNum[4];
				break;
		case 3: sTemp[0] = gate[0];
				sTemp[1] = gate[1];
				sTemp[2] = gate[2];
				sTemp[3] = gate[3];
				sTemp[4] = gate[4];
				break;
		case 4: sTemp[0] = destination[0];
				sTemp[1] = destination[1];
				sTemp[2] = destination[2];
				sTemp[3] = destination[3];
				sTemp[4] = destination[4];
				break;
		case 5: sTemp[0] = departTime[0];
				sTemp[1] = departTime[1];
				sTemp[2] = departTime[2];
				sTemp[3] = departTime[3];
				sTemp[4] = departTime[4];
				break;
		default:
			cout << "Invalid Entry" << endl;
			break;
}

	//***************************************************verifying copy
	cout << "You chose: " << endl;
	int i;
	for(i=0;i < 5; i++)
		cout << sTemp[i] << endl;
   //******************************************************************
	
	cout << "Now please choose the criteria you'd like to retrieve from that category." << endl;
	
	cout << "1. Airline" << endl;
	cout << "2. Flight Number" << endl;
	cout << "3. Gate" << endl;
	cout << "4. Destination" << endl;
	cout << "5. Departure Time" << endl;

	cin >> secondChoice;

	switch (secondChoice)
	{
		case 1:     cout << "Which airline would you like to view"<< endl;
					cin >> answer;
					searchString(sTemp, 5, answer);
					break;
		case 2: 
			cout << "What Flight Number would you like to view?" << endl;
			cin >> answer;
			searchString(sTemp, 5, answer);
	}
	return 0;
}

Well, as far as this goes, if you enter a correct input string, the search function is doing its job.

Now, to be useful, that function should return an indication of whether or not the target was found - I'd go with a bool return type.

Can you help me to print a full line if i find a match with the 1st name??

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string Y;
    string  X[10]={"Muhamad Husin, A123456",
                  "Nor Asma, A155743",
                  "Raja Kumar, A18723",
                  "Muhamad Ali, A123908",
                  "Ali Shah, A145234",
                  "Siti Sarah, A120945",
                  "Siti kahadijah, A154237",
                  "Syed Azman, A276456",
                  "Chong Wei, A156278"};

    cout<<"Searching Student's Info \n"
        <<"------------------------ \n"
        <<"Enter the name or partial name, \n"
        <<"you would like to search for: ";

    cin>>Y;

    cout<<"\n\nSearch Result: \n";

    for(int i=0; i<10;i++)
    {
    if (X[i]==Y)
    {
    cout<<X[i]<<endl;
    }
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.