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

}

Dani AI

Generated

Short answer: split the data and pass the size into the search routine (or use a container). As pointed out, keeping name and decade as separate members makes searching far simpler and less error-prone — it also makes adding nationality or other fields trivial.

Design notes and common pitfalls:

  • Return type: decide whether the search should answer "found or not" (use a boolean) or return a position/index (use an unsigned index type and a special "not found" sentinel). std::string::npos is the sentinel used by find() and only makes sense when the function returns a size_type/size_t, not a bool.
  • Scope: the numCelebs variable in main is local to main. To use it elsewhere either pass the count into the search function (or pass a container that knows its own size), or make the search a method on a collection object. A cleaner modern approach is std::vector<Celeb> so the loop uses vec.size() and there is no manual new[]/delete[].
  • Searching strategy: if decade is stored separately, do an equality or normalized-equality check. If keeping a single combined string, extract the trailing token (or compare with find()), but beware of false positives and spacing/case issues. Normalizing (trim + lowercase) both stored data and search input avoids mismatches.

Input and debugging tips:

  • After reading numCelebs with cin >>, clear the leftover newline before the first getline. A robust pattern is to discard to end-of-line once (or use getline with std::ws) rather than calling cin.ignore() unconditionally inside each loop iteration.
  • Prefer std::vector to manual arrays, avoid system("pause"), and normalize user input before comparisons.
  • If the compiler complains about size_t or std::string::npos, std::string::size_type or auto can be used as an easy, portable alternative.

Applying these changes will remove the undefined numCelebs error, make the search logic clear, and make future extensions (nationality, multiple keys) straightforward.

Recommended Answers

All 2 Replies

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

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.

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.