title: searching through an array of class objects c++
body: <p>I need help trying to search an array of class objects. I have a class called users and have an array where 3 players are stored. I would like to be able to show the information of a particular player when his both names are typed in and delete his records when is name is typed in.</p>
  
  <p>I understand that I could use a vector list to make this easier, but I have limitations set. I also think a linear search could be implemented but i don't know if that is efficient enough.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  void storeinfo() ;
  void showinfo() ;
  void menu() ;
  
  
  class user 
  {
      string firstname, lastname, currentteam, position, status ;
      int age ;
  public:
      user() {};
      user(string fname, string lname, string cteam, string pos, string stat, int age) 
      {
          setFirstName(fname);
          setLastName(lname);
          setCurrentTeam(cteam);
          setPosition(pos);
          setStatus(stat);
          setAge(age);
      } ;
      void setFirstName(string fname)
          {firstname = fname;}
      void setLastName(string lname)
          {lastname = lname;}
      void setCurrentTeam(string cteam)
          {currentteam = cteam;}
      void setPosition(string pos)
          {position = pos;}
      void setStatus(string stat)
          {status = stat;}
      void setAge(int _age)
          {age = _age;}
  
      string getFirstName()
          {return firstname ;}
      string getLastName()
          {return lastname ;}
      string getCurrentTeam()
          {return currentteam ;}
      string getPosition()
          {return position ;}
      string getStatus()
          {return status ;}
      int getAge()
          {return age ;}
  };
  
  user player[20] ;
  
  int main()
  {
      menu() ;
  
  
  
      cin.get() ;
      return 0 ;
  
  }
  
  void storeinfo()
  {
      string firstname ;
      string lastname ;
      string currentteam ;
      string position;
      string status ;
      int age ;
  
      for (int i=0; i &lt; 3; i++)
      {
          cout &lt;&lt; "Enter First Name : " ; 
          cin &gt;&gt; firstname ;
          player[i].setFirstName(firstname) ;
          cout &lt;&lt; "Enter Last Name : " ; 
          cin &gt;&gt; lastname ;
          player[i].setLastName(lastname) ;
          cout &lt;&lt; "Enter Player's Age : " ; 
          cin &gt;&gt; age;
          player[i].setAge(age) ;
          cout &lt;&lt; "Enter Current Team : " ; 
          cin &gt;&gt; currentteam ;
          player[i].setCurrentTeam(currentteam) ;
          cout &lt;&lt; "Enter Position : " ; 
          cin &gt;&gt; position ;
          player[i].setPosition(position) ;
          cout &lt;&lt; "Enter Status : " ; 
          cin &gt;&gt; status ;
          player[i].setStatus(status) ;
  
          cout &lt;&lt; "\n\n\n" ;
      }
  
      /*cout &lt;&lt; string(50, '\n');*/
  
      menu() ;
  
  }
  
  void showinfo()
  {
      for (int i=0; i &lt; 3; i++)
      {
          cout &lt;&lt; "First Name : " &lt;&lt; player[i].getFirstName() &lt;&lt; "\n" &lt;&lt; "Last Name : " &lt;&lt; player[i].getLastName() &lt;&lt;
              "\n" &lt;&lt; "Age : " &lt;&lt; player[i].getAge() &lt;&lt; "\n" &lt;&lt; "Current Team : " &lt;&lt; player[i].getCurrentTeam() &lt;&lt; 
              "\n" &lt;&lt; "Position : " &lt;&lt; player[i].getPosition() &lt;&lt; "\n" &lt;&lt; "Status :  " &lt;&lt; player[i].getStatus()  &lt;&lt; "\n\n";
      }
  
      cin.get() ;
  
      menu() ;
  }
  
  void menu()
  {
      cout &lt;&lt; "\n MENU" &lt;&lt; "\n" ;
      cout &lt;&lt; "\n 1. Store Player Information" ;
      cout &lt;&lt; "\n 2. Show Player Informaton" ;
      cout &lt;&lt; "\n 0. Exit \n \n" ;
  
      string x =  "";
      cin &gt;&gt; x ;
  
      if (x=="a")
      { 
          storeinfo() ;
      }
      else if (x=="b")
      {
          showinfo() ;
      }
      else if (x=="c")
      {
          exit(0) ;
      }
      else
      {
          cout &lt;&lt; "Invalid Choice" ;
          menu() ;
      }   
  }
  </code></pre>
  
  <p>i have done a linear search algorithm and seems to be working but the output i am getting is not correct below is the code for both functions, thank you again</p>
  
  <pre><code>int linsearch(string val)
  {
      for (int j=0; j &lt; 3; j++)
      {
          if  (player[j].getLastName()==val)
          {
              return j;
          }
          else 
          {
              return 1;
          }
      }
  
  
  
  
  void showinfo()
  {
      string search;
      int found ;
  
  
      cout &lt;&lt; "Please Enter The Player's Last Name : " ;
      cin &gt;&gt; search ;
  
      found=linsearch(search);
  
      if (found== 1)
      {
          cout &lt;&lt; "\n There is no player called " &lt;&lt; search ;
      }
      else
      {
          cout &lt;&lt; "\n First Name : " &lt;&lt; player[found].getFirstName() &lt;&lt; "\n" &lt;&lt; "Last Name : " &lt;&lt; player[found].getLastName() &lt;&lt;
              "\n" &lt;&lt; "Age : " &lt;&lt; player[found].getAge() &lt;&lt; "\n" &lt;&lt; "Current Team : " &lt;&lt; player[found].getCurrentTeam() &lt;&lt; 
              "\n" &lt;&lt; "Position : " &lt;&lt; player[found].getPosition() &lt;&lt; "\n" &lt;&lt; "Status :  " &lt;&lt; player[found].getStatus()  &lt;&lt; "\n\n";
      }
  
      cin.get() ;
  
      menu() ;
  
  }
  </code></pre>