This has really been frustrating me for hours. I have included useful comments in the program.
All the descent examples i've come across were using char instead of string.

:icon_cry:

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

class Celeb
{ 
	private: 
		string name; //Name of celebrity folowed by decade they were born in, E.g. Reba McEntire 50s, 2nd e.g. Gene Hackman 30s
	
	public: 
		Celeb();	

		void  setName(string);

		/*	bool foundDecade(string decade) //;
			{
			
			must use
			string::npos

			}*/	
		
		//a member function should retun sting::npos if it can't find user input in string

		string getName() ; 
		
};

Celeb::Celeb()
{
	name = "No person";
}

void Celeb::setName(string celebName)
{
	name = celebName;	
}

string Celeb::getName()   //; 
{
	return name;	
} 


void InputCelebs(Celeb * celbPtr, int size);

int main()
{
	Celeb search;

	string searchCelebs;

	int numCelebs;
	
	cout  <<  "How many celebs do you want to enter: " ; 
	cin  >>  numCelebs;
	
	Celeb * celebList;
	celebList = new Celeb[numCelebs];

	InputCelebs(celebList, numCelebs);  //User will then proceed to enter
										   //list of cities and their coordinates
	
	cout << "\nEnter last 2 number of decades you'd like to search for, ";
	cout << "\n, and make sure its ends with an s, please: ";
	
	cin >> searchCelebs;
	//search.foundDecade(searchCelebs);	 --->>>> Call bool function

	
	delete [] celebList;
	celebList = 0;

	system("pause");
	return 0;
}

void InputCelebs(Celeb * celebPtr, int size)
{
	string celeb_Name;  //Name of of celeb followed by decade they were born on; e.g., Reba McEntire 50s
		
	for (int index = 0; index < size; index++) 
	{

		cout << endl;
		cout << "Enter name of celebrity followed by decade they were,"; 
		cout << "\nborn on ending with an s (like 80s, for 1980): ";
		cin.ignore();
		getline(cin, celeb_Name);
		celebPtr[index].setName(celeb_Name);

		cout << endl;		
	}

}

>> foundDecade(string decade)

what have you tried ? Since each instance of Celeb::name contains both name and decade just a simple search for some name will not work. To make searches less complicated you might want to split Celeb::name into two separate items: name and decade.

class Celeb
{
private:
   std::string name;
   std::string decade;
...
...

With that change the foundDecade() method just becomes

bool foundDecade(string dec)
{
  for(int i = 0; i < numCelebs; i++)
  {
     size_t pos = celebs[i].decade.find(dec);
     if( pos != std::npos)
          return true;
  }
  return false;
}

Edited 5 Years Ago by Ancient Dragon: n/a

Direction do, call for the search to be performed on a single string statement. I guess because later it gets more unique, like letting the user enter in the celebs nationality like USA or GBR.

bool foundDecade(string dec)
{
  for(int i = 0; i < numCelebs; i++)
  {
     size_t pos = celebs[i].decade.find(dec);
     if( pos != std::npos)
          return true;
  }
  return false;
}

As for
i < numCelebs; // in the for statement

Should i redeclare it in the the bool function? because the compiler says it is undefined.
Please forgive me, but it seems i may not be permitted to utilize size_t, this early in the session. It's new to me and probably fellow pupils as well.

This article has been dead for over six months. Start a new discussion instead.