Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how do you get the while loop to let you input either first or last and first an

ID: 3765576 • Letter: H

Question

how do you get the while loop to let you input either first or last and first and last and get the data you want and not just first and last?

while(!input.eof() )
{

   input>>first>>last>>phone;
   if
       ((f==first) && (l==last))
       cout<<f<<" "<<l<<" "<<phone<<endl;
   else if
       ((f==first) && (l==""))
       cout<<f<<" "<<l<<" "<<phone<<endl;
   else if
       ((f=="")&&(l==last))
       cout<<f<<" "<<l<<" "<<phone<<endl;
   done=true;
  
}

Explanation / Answer

You are almost there, but with a little bit of modification:

while(!input.eof() ) //Till you reach the end of file.
{

   input>>first>>last>>phone; //Reads the 3 fields of each line into first, last, and phone.
   if ((f==first) && (l==last) //If first and last both are entered, check with both the values, and if matches print all data.
       cout<<f<<" "<<l<<" "<<phone<<endl;
   else if(f==first) //If not both the values, if first is entered, check with first, and if matches print all data.
       cout<<f<<" "<<l<<" "<<phone<<endl;
   else if(l==last) //If second is entered, check with second, and if matches print all data.
       cout<<f<<" "<<l<<" "<<phone<<endl;
   done=true;
  
}

If you have any further queries, just get back to me.